我正在尝试使用用C#编写的DLL与激光通信。我已经成功地使用ctypes模块加载了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返回给我一个整数值,比如1066192077。这正是我所要求的。但是我希望将该整数解释/转换为浮点数,这个浮点数应该是1.1或类似的值(无法记住确切的值)。将其强制转换为浮点数不起作用(我得到了1066192077.00),使用hex() -> bytes() -> struct.unpack( )
也不行。我该怎么办?
outputData_p
替换为dataBuffer = (c_uint32 * 33 )()
并传递给我的函数,以遵守这个错误,一旦我的函数返回,我将把我的_int_缓冲区转换为_float_缓冲区,使用floatBuffer = (c_float * 33).from_buffer(dataBuffer)
? - Nessy W.argstypes
在这个混乱中的作用。 - Nessy W.