I am trying to work with a 3rd party DLL file to control a hardware (translation stage in my case). I have windows 10, 64 bit python (with Jupyter notebook).
I have imported the correct version (64-bit) of dll file like this:
import ctypes, sys
PiUsbDll = ctypes.WinDLL("PiUsb")
I have a copy of C++ header file for the DLL. Therefore, I know what is the list of functions and what arguments they take. I have defined the restype and argtype for each of the functions in the DLL file like this (# lines show the function def as per C++ header file)
# void * __stdcall piConnectMotor(int * ErrorNum, int SlideSerialNum);
PiUsbDll.piConnectMotor.restype = ctypes.c_void_p
PiUsbDll.piConnectMotor.argtypes = (ctypes.POINTER(ctypes.c_int),ctypes.c_int)
# void __stdcall piDisconnectMotor(void * devicePtr);
PiUsbDll.piDisconnectMotor.restype = None
PiUsbDll.piDisconnectMotor.argtypes = ctypes.c_void_p, #, defines a tuple
# int __stdcall piHomeMotor(int Velocity, void * devicePtr);
PiUsbDll.piHomeMotor.restype = ctypes.c_int
PiUsbDll.piHomeMotor.argtypes = (ctypes.c_int, ctypes.c_void_p)
# int __stdcall piRunMotorToPosition( int Position, int Velocity, void * devicePtr);
PiUsbDll.piRunMotorToPosition.restype = ctypes.c_int
PiUsbDll.piRunMotorToPosition.argtype = (ctypes.c_int, ctypes.c_int, ctypes.c_void_p)
.
.
.
I have defined/initialized variables with ctypes:
SlideSerialNum = ctypes.c_int(123) #serial number provided from manufacturer
Velocity = ctypes.c_int(10) # Allowed range 1-12
Position = ctypes.c_int(500) #Allowed range 0-1900
devicePtr = ctypes.c_void_p()
ErrorNum = ctypes.c_int()
.
.
.
And then I call the functions like this (have omitted few intermediate lines of codes)
# Connect to motor
devicePtr = PiUsbDll.piConnectMotor(ctypes.byref(ErrorNum),SlideSerialNum)
# Homing the motor
ErrorNum.value = PiUsbDll.piHomeMotor(Velocity,devicePtr)
# Run to a position
ErrorNum.value = PiUsbDll.piRunMotorToPosition(Position,Velocity,devicePtr)
.
.
.
Here, connecting and homing work fine all the times but run to a position fails with the following error:
ArgumentError: argument 3: <class 'OverflowError'>: int too long to convert
So, looks like devicePtr (a void*) is having inconsistent conversion between python type and ctypes. I hoped that defining argtype would take care of this but does not look so. One solution would be to fall back to 32-bit python version (alongwith 32-bit DLL files) where this problem does not appear but I would love to stick to 64 bit if possible.