Python for .NET:如何提供特殊类型的变量 (FTDI.FT_DEVICE_INFO_NODE[]) 作为方法参数
Python for .NET: How to provide a variable of special type (FTDI.FT_DEVICE_INFO_NODE[]) as method parameter
我想在 Python 的帮助下使用 DLL for .NET (pythonnet)。
此 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
Here 我发现了一些 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, and the .Net wrapper was downloaded from here: http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/CSharp.htm
源代码需要一个包含 FT_DEVICE_INFO_NODE 的列表。这是函数声明:
public FTDI.FT_STATUS GetDeviceList(FTDI.FT_DEVICE_INFO_NODE[] devicelist)
这段代码对我有用:
ft_device_info_node = ftdi.FT_DEVICE_INFO_NODE()
ft_status = ftdi.GetDeviceList([ft_device_info_node])
print(ft_status)
最后我放弃了这个功能,因为我无法达到我想要的return,而是使用了"GetDeviceID"、"GetDescription"和"GetSerialNumber" 打开我想从中获取信息的设备后单独运行。
示例:
# Open
ftdi.OpenByIndex(0)
print("Opened Index 0")
# Get Device ID
device_id = 0
ft_status, device_id = ftdi.GetDeviceID(device_id)
assert(ft_status == 0)
print(device_id)
# Close
ftdi.Close()
print("Closed")
我知道这个 post 已经过时了,但它仍然适用。对我有用的技巧是创建类型为 FTD2XX_NET.FT_DEVICE_INFO_NODE[] 的数组实例,长度设置为 FTDI 设备数:
# -*- coding: utf-8 -*-
"""
Created on Wed May 13 15:50:22 2020
@author: chogh
"""
# For Windows, from a Anaconda Prompt cmd window:
# conda install -c ipython pythonnet # so import System classes are exposed
# conda install -c ipython ftd2xx # so import FTD2XX_NET classes are exposed
#
#############################################################################
import System # available after running conda install -c ipython pythonnet in Anaconda Prompt cmd window
import clr
from System import Array # to import .Net Array functions for FTD2XX_NET
from System import Byte # to import .Net Array functions for FTD2XX_NET
from array import array
clr.AddReference("System")
clr.AddReference('FTD2XX_NET')
from FTD2XX_NET import FTDI
myFtdiDevice=FTDI()
ft_status = myFtdiDevice.FT_STATUS.FT_DEVICE_NOT_FOUND
DeviceNum=0
ft_status,DeviceNum=myFtdiDevice.GetNumberOfDevices(DeviceNum)
DeviceList=Array.CreateInstance(myFtdiDevice.FT_DEVICE_INFO_NODE, DeviceNum)
ft_status=myFtdiDevice.GetDeviceList(DeviceList)
print("ft_status:", ft_status)
print("Device Description: ",DeviceList[0].Description)
print("Device Serial Number: ",DeviceList[0].SerialNumber)
print("Device Device ID: ",DeviceList[0].ID)
print("Device Device Type: ",DeviceList[0].Type)
这导致以下输出:
Reloaded modules: System, FTD2XX_NET
ft_status: 0
Device Description: Hazelnut
Device Serial Number: FTWLN0XM
Device Device ID: 67330068
Device Device Type: 8
我想在 Python 的帮助下使用 DLL for .NET (pythonnet)。 此 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
Here 我发现了一些 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, and the .Net wrapper was downloaded from here: http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/CSharp.htm
源代码需要一个包含 FT_DEVICE_INFO_NODE 的列表。这是函数声明:
public FTDI.FT_STATUS GetDeviceList(FTDI.FT_DEVICE_INFO_NODE[] devicelist)
这段代码对我有用:
ft_device_info_node = ftdi.FT_DEVICE_INFO_NODE()
ft_status = ftdi.GetDeviceList([ft_device_info_node])
print(ft_status)
最后我放弃了这个功能,因为我无法达到我想要的return,而是使用了"GetDeviceID"、"GetDescription"和"GetSerialNumber" 打开我想从中获取信息的设备后单独运行。
示例:
# Open
ftdi.OpenByIndex(0)
print("Opened Index 0")
# Get Device ID
device_id = 0
ft_status, device_id = ftdi.GetDeviceID(device_id)
assert(ft_status == 0)
print(device_id)
# Close
ftdi.Close()
print("Closed")
我知道这个 post 已经过时了,但它仍然适用。对我有用的技巧是创建类型为 FTD2XX_NET.FT_DEVICE_INFO_NODE[] 的数组实例,长度设置为 FTDI 设备数:
# -*- coding: utf-8 -*-
"""
Created on Wed May 13 15:50:22 2020
@author: chogh
"""
# For Windows, from a Anaconda Prompt cmd window:
# conda install -c ipython pythonnet # so import System classes are exposed
# conda install -c ipython ftd2xx # so import FTD2XX_NET classes are exposed
#
#############################################################################
import System # available after running conda install -c ipython pythonnet in Anaconda Prompt cmd window
import clr
from System import Array # to import .Net Array functions for FTD2XX_NET
from System import Byte # to import .Net Array functions for FTD2XX_NET
from array import array
clr.AddReference("System")
clr.AddReference('FTD2XX_NET')
from FTD2XX_NET import FTDI
myFtdiDevice=FTDI()
ft_status = myFtdiDevice.FT_STATUS.FT_DEVICE_NOT_FOUND
DeviceNum=0
ft_status,DeviceNum=myFtdiDevice.GetNumberOfDevices(DeviceNum)
DeviceList=Array.CreateInstance(myFtdiDevice.FT_DEVICE_INFO_NODE, DeviceNum)
ft_status=myFtdiDevice.GetDeviceList(DeviceList)
print("ft_status:", ft_status)
print("Device Description: ",DeviceList[0].Description)
print("Device Serial Number: ",DeviceList[0].SerialNumber)
print("Device Device ID: ",DeviceList[0].ID)
print("Device Device Type: ",DeviceList[0].Type)
这导致以下输出:
Reloaded modules: System, FTD2XX_NET
ft_status: 0
Device Description: Hazelnut
Device Serial Number: FTWLN0XM
Device Device ID: 67330068
Device Device Type: 8