devanl
devanl

Reputation: 1322

ctypes function return double value mangled

I have a shared library function which I am loading through 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;
}

When I call this in python I get an integer value returned:

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)

output:

5117992 <type 'int'>

Is there a way to force the typecasting?

Thanks

Upvotes: 0

Views: 2479

Answers (1)

devanl
devanl

Reputation: 1322

from the tutorial provided by eryksun, docs.python.org/2/library/ctypes.html#return-types

"By default functions are assumed to return the C int type. Other return types can be specified by setting the restype attribute of the function object."

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)

Upvotes: 2

Related Questions