使用d2xx.dll文件在python中使用ctypes

2024-10-03 02:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用d2xx.dll文件这是一个连接FTDI的USB控制器芯片的库。我想在python代码中使用这个库。我决定用python中的ctypes库来调用dll函数。我未能正确地从dll文件中调用函数,并且我认为传递给dll库中函数的变量类型存在一些问题。我不是一个专业的程序员,所以对我放轻松。:D个

这是迄今为止的尝试

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)

我只是想检测有多少D2XX设备与FT\u createDeviceInfo()函数连接。FT\u createDeviceInfo列表在第6页的d2xx.dll documentation中描述。该函数有一个输入参数,它是指向无符号long的指针,并返回USB设备的状态。我用“numDevs=ctypes.c\ulong()”声明了一个无符号long,并用“p\u numDevs=pointer(numDevs)”声明了它的指针,运行代码时出现以下错误。你知道吗

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

我还尝试使用byref传递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)


这一次我得到了以下信息:

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

如何传递输入参数?我知道d2有些包装xx.dll文件在python中,比如pyusb,但是由于某种原因我不能让它们工作。我用d2写了一些C代码xx.dll文件他们干得很出色。现在我正在寻找一种让它们在python中工作的方法。你知道吗

提前谢谢。你知道吗


Tags: 文件函数importtypestatusfunctionctypesdll