我想在 Python for .NET (pythonnet) 的帮助下使用 DLL。此 DLL 中的方法需要(指向?)变量作为参数。
我可以弄清楚如何调用方法并提供一个整数变量作为参数。
如果需要特殊类型,我不知道如何提供这样的参数变量(此处:FTD2XX_NET.FTDI.FT_DEVICE_INFO_NODE[])。
在使用不同的调用变体FTDI.GetDeviceList
(其中一些在技术上相同)时,我得到不同类型的错误:
TypeError:没有方法匹配给定的参数
FTDI.GetDeviceList([ftdi.FT_DEVICE_INFO_NODE()][:])
FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE)
FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE())
FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE(device_count))
FTDI.GetDeviceList([devicelist_])
FTDI.GetDeviceList(devicelist_)
类型错误:不可索引的对象
FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE()[:])
类型错误:预期类型
FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE[:])
FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE[1])
FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE[0])
这是代码摘要和结果:
import clr
import sys
sys.path.append("C:\\Users\\[...]\\FTD2XX_NET_v1.1.0\\") # path to dll
clr.AddReference("System")
clr.AddReference("FTD2XX_NET")
from FTD2XX_NET import FTDI
from System import UInt32
ftdi = FTDI()
if __name__ == '__main__':
# This method requires an integer as parameter, the code is working
# M:FTD2XX_NET.FTDI.GetLibraryVersion(System.UInt32@)
# summary: Gets the current FTD2XX.DLL driver version number.
# returns: FT_STATUS value from FT_GetLibraryVersion in FTD2XX.DLL
# param: name: LibraryVersion. The current library version.
version_ = 0 # empty variable to provide as parameter
ft_status, version = ftdi.GetLibraryVersion(version_)
print('status: {}\nversion: {}'.format(ft_status, version))
# prints
# status: 0
# version: 197140
# This method requires an FT_DEVICE_INFO_NODE[] as parameter, the code is not working
# M:FTD2XX_NET.FTDI.GetDeviceList(FTD2XX_NET.FTDI.FT_DEVICE_INFO_NODE[])
# summary: Gets information on all of the FTDI devices available.
# returns: FT_STATUS value from FT_GetDeviceInfoDetail in FTD2XX.DLL
# param: name: devicelist.
# An array of type FT_DEVICE_INFO_NODE to contain the device information
# for all available devices.
# no idea how to create the fitting parameter FT_DEVICE_INFO_NODE[]
devicelist_ = ftdi.FT_DEVICE_INFO_NODE() # empty variable to provide as parameter
print(devicelist_.Flags, devicelist_.Type, devicelist_.ID,
devicelist_.LocId, devicelist_.SerialNumber,
devicelist_.Description, devicelist_.ftHandle)
# prints
# 0 0 0 0 None None 0
status, devicelist = FTDI.GetDeviceList(devicelist_)
print(devicelist)
# throws a TypeError: No method matches given arguments
在这里,我发现了一些 c# 代码,其中 FT_DEVICE_INFO_NODE[] 和 GetDeviceList 在方法中使用:
private int countDevices()
{
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
ftStatus = myDevice.GetNumberOfDevices(ref ftdiDeviceCount);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
return 0;
if (ftdiDeviceCount > 0)
{
//allocate device info list
ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
//populate list
ftStatus = myDevice.GetDeviceList(ftdiDeviceList);
if (ftStatus == FTDI.FT_STATUS.FT_OK)
{
return (int)ftdiDeviceCount;
}
else
return 0;
}
else
return 0;
}
FTDI.FT_DEVICE_INFO_NODE[]
作为参数提供给我有什么遗漏FTDI.GetDeviceList
?
FTDI 的 USB 驱动程序在此处下载:http ://www.ftdichip.com/Drivers/D2XX.htm,.Net包装器从此处下载:http: //www.ftdichip.com/Support/SoftwareExamples/CodeExamples /CSharp.htm