我有一个共享库函数,我通过ctypes加载:
double volts_USB1608G(usb_dev_handle *udev, const __u8 gain, __u16 value)
{
double volt = 0.0;
switch (gain) {
case BP_10V:
volt = (value - 32768.)*10./32768.;
break;
case BP_5V:
volt = (value - 32768.)*5./32768.;
break;
case BP_2V:
volt = (value - 32768.)*2./32768.;
break;
case BP_1V:
volt = (value - 32768.)*1./32768;
break;
}
return volt;
}
当我在python中调用它时,我得到一个返回的整数值:
mcc_lib = cdll.LoadLibrary("libmcchid.so")
value = 0x80d5
gain = 1 #BP_5V
volts = mcc_lib.volts_USB1608G(daq.udev, gain, value)
print volts, type(volts)
输出:
5117992 <type 'int'>
有没有办法强制进行类型转换?
由于
答案 0 :(得分:1)
来自eryksun提供的教程,docs.python.org/2/library/ctypes.html#return-types
&#34;默认情况下,假定函数返回C int类型。可以通过设置函数对象的restype属性来指定其他返回类型。&#34;
mcc_lib = cdll.LoadLibrary("libmcchid.so")
value = 0x80d5
gain = 1 #BP_5V
mcc_lib.volts_USB1608G.restype = c_double
volts = mcc_lib.volts_USB1608G(daq.udev, gain, value)
print volts, type(volts)