I'm trying to use d2xx.dll which is a library for interfacing FTDI's USB controller chips. I want to use this library in my python code. I decided to use ctypes library in python to call dll functions. I failed to call function from dll file appropriately and I think I have some problems with variable types that I pass to the functions in the dll library. I am not a professional programmer, so take it easy on me. :D
Here is what tried so far,
import ctypes
from ctypes import *
d2xxdll = ctypes.cdll.LoadLibrary('ftd2xx.dll') #Load the dll library
d2xxdll.FT_CreateDeviceInfoList.argtypes =[ctypes. c_ulong] # specify function's input type
d2xxdll.FT_CreateDeviceInfoList.restype = ctypes.c_ulong # specify function's return type
numDevs = ctypes.c_ulong()
p_numDevs = POINTER(numDevs)
FT_STATUS = d2xxdll.FT_CreateDeviceInfoList(p_numDevs)
print(FT_STATUS)
print(numDevs)
I am just trying to detect how many D2XX devices are connected with FT_CreateDeviceInfoList() function. FT_CreateDeviceInfoList is described in d2xx.dll documentation page 6. The function has one input parameter which is a pointer to an unsigned long and it returns the state of the USB device. I declare an unsigned long with "numDevs = ctypes.c_ulong()" and declare its pointer in "p_numDevs = POINTER(numDevs)", and I get the following error when I run the code.
Traceback (most recent call last):
File "C:\Users\asus\Desktop\dokümanlar\Python_Ogren\Ctypes_cll\d2xx_dll.py", line 9, in <module>
p_numDevs = ctypes.POINTER(numDevs)
TypeError: must be a ctypes type
I also tried using byref to pass the address of numDevs:
import ctypes
from ctypes import *
d2xxdll = ctypes.cdll.LoadLibrary('ftd2xx.dll') #Load the dll library
d2xxdll.FT_CreateDeviceInfoList.argtypes =[ctypes. c_ulong] # specify function's input type
d2xxdll.FT_CreateDeviceInfoList.restype = ctypes.c_ulong # specify function's return type
numDevs = ctypes.c_ulong()
FT_STATUS = d2xxdll.FT_CreateDeviceInfoList(ctypes.byref(numDevs))
print(FT_STATUS)
print(numDevs)
This time I get the following:
Traceback (most recent call last):
File "C:\Users\asus\Desktop\dokümanlar\Python_Ogren\Ctypes_cll\d2xx_dll.py", line 10, in <module>
FT_STATUS = d2xxdll.FT_CreateDeviceInfoList(ctypes.byref(numDevs))
ArgumentError: argument 1: <type 'exceptions.TypeError'>: wrong type
How should I pass the input parameter? I know there are some wrappers for d2xx.dll in python like pyusb, but for some reason I couldn't got them working. I wrote some C code with d2xx.dll they worked like charm. Right now I am looking for a way to get them working in python.
Thanks in advance.
The C definition of the function is:
ctypeshas a library of Windows types to help you map them correctly. The parameter type is not ac_ulongbut aPOINTER(c_ulong). Thewintypesmodule definesLPDWORDcorrectly as this type. Then you create aDWORDand pass itbyref().Below is a complete wrapper: