我正在尝试使用用C#编写的动态链接库来与激光通信。我成功地使用ctype模块加载了DLL函数。我想使用的函数有一个如下所示的声明:
LONG LJV7IF_GetStorageData( LONG lDeviceId,
LJV7IF_GET_STORAGE_REQ* pReq,
LJV7IF_STORAGE_INFO* pStorageInfo,
LJV7IF_GET_STORAGE_RSP* pRsp,
DWORD* pdwData,
DWORD dwDataSize );我想通过dword指针pdwData访问数据。激光通过如下结构发送其存储的数据:
Bytes | Meaning | Types
0-3 | time | dword
4 | judgment | byte
5 | meas info | byte
... | ... | ...
8-11 | data | float我就是这样使用这个函数的:
self.dll = WinDLL( "LJV7_IF.dll" )
self._getStoredData = self.dll.LJV7IF_GetStorageData
self._getStoredData.restype = c_int32
self._getStoredData.argstypes = [ c_int32,
POINTER( GET_STORAGE_REQ ),
POINTER( STORAGE_INFO ),
POINTER( GET_STORAGE_RSP ),
POINTER( c_uint32 ),
c_uint32 ]
dataSize = 132
dataBuffer = c_uint32 * ( dataSize / 4 )
outputData_p = POINTER( c_uint32 )( dataBuffer() )
self._getStoredData( deviceID,
byref( myStruct ),
byref( storageInfo ),
byref( storageResponse ),
outputData_p ),
dataSize )对于简洁性,myStruct、storageInfo和storageResponse没有详细说明(它们在其他DLL函数调用中使用,而且它们似乎工作得很好)。
我的问题是,当我试图访问outputData_p[ 2 ]时,python会返回一个int,比如1066192077。这正是我问他的。但是我希望这个int被解释/转换为一个浮点数,它应该是1.1或者类似的东西(不记得确切的值)。使用hex() -> bytes() -> struct.unpack( )将其转换为浮点不起作用(我得到1066192077.00 )。我能做什么??
发布于 2015-12-24 01:28:07
注意:如果您是在搜索如何将整数转换为单精度浮点数时到达这里的,则忽略其余的答案,只需使用J.F.Sebastian注释中的代码即可。它使用struct模块而不是ctype,后者更简单,而且总是可用的,而ctype则可选地包含在Python的标准库中:
import struct
def float_from_integer(integer):
return struct.unpack('!f', struct.pack('!I', integer))[0]
assert float_from_integer(1066192077) == 1.100000023841858可以使用ctype from_buffer方法将数组解释为不同的类型。通常,您可以将具有可写缓冲区接口的任何对象传递给该方法,而不仅仅是ctype实例--例如bytearray或NumPy数组。
例如:
>>> from ctypes import *
>>> int_array = (c_int * 4)(1, 2, 3, 4)
>>> n_doubles = sizeof(int_array) // sizeof(c_double)
>>> array_t = c_double * n_doubles
>>> double_array = array_t.from_buffer(int_array)它仍然是相同的字节,只是重新解释为两个8字节的双倍:
>>> bytes(double_array)
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00'
>>> double_array[:]
[4.2439915824e-314, 8.4879831653e-314]由于这个数组是通过调用from_buffer (而不是from_buffer_copy )创建的,因此它实际上是一个与原始数组共享相同缓冲区的视图。例如,如果将最后2个整数移到前面,double_array中的值也会被交换:
>>> int_array[:] = [3, 4, 1, 2]
>>> double_array[:]
[8.4879831653e-314, 4.2439915824e-314]注意,您的示例代码有一个错误。定义函数参数类型的属性名是argtypes,而不是argstypes
self._getStoredData.argstypes = [ c_int32,
^^^^^^^^^定义这个原型并不是绝对必要的,但它是推荐的。当调用函数时,它使ctype为每个参数调用相应的from_param方法。如果没有原型,默认参数处理将接受ctype实例,并自动将字符串转换为char *或wchar_t *,将整数转换为C int值;否则将引发ArgumentError。
您可以按以下方式定义打包(即不对齐填充)数据记录:
class LaserData(Structure):
_pack_ = 1
_fields_ = (('time', c_uint),
('judgement', c_byte),
('meas_info', c_byte * 3),
('data', c_float))下面是一个示例类,它将泛型数据参数类型定义为POINTER(c_byte),并将结果作为由设备实例确定的记录数组返回。显然,这只是如何实际定义类的要点,因为我对这个API几乎一无所知。
class LJV7IF(object):
# loading the DLL and defining prototypes should be done
# only once, so we do this in the class (or module) definition.
_dll = WinDLL("LJV7_IF")
_dll.LJV7IF_Initialize()
_dll.LJV7IF_GetStorageData.restype = c_long
_dll.LJV7IF_GetStorageData.argtypes = (c_long,
POINTER(GET_STORAGE_REQ),
POINTER(STORAGE_INFO),
POINTER(GET_STORAGE_RSP),
POINTER(c_byte),
c_uint)
def __init__(self, device_id, record_type):
self.device_id = device_id
self.record_type = record_type
def get_storage_data(self, count):
storage_req = GET_STORAGE_REQ()
storage_info = STORAGE_INFO()
storage_rsp = GET_STORAGE_RSP()
data_size = sizeof(self.record_type) * count
data = (c_byte * data_size)()
result = self._dll.LJV7IF_GetStorageData(self.device_id,
byref(storage_req),
byref(storage_info),
byref(storage_rsp),
data,
data_size)
if result < 0: # assume negative means an error.
raise DeviceError(self.device_id) # an Exception subclass.
return (self.record_type * count).from_buffer(data)例如:
if __name__ == '__main__':
laser = LJV7IF(LASER_DEVICE_ID, LaserData)
for record in laser.get_storage_data(11):
print(record.data)https://stackoverflow.com/questions/34402334
复制相似问题