Python for .NET : How to provide a variable of special type (FTDI. FT_DEVICE_INFO_NODE[]) 作为方法参数

标签 python .net ftdi python.net pythonnet

我想在 Python for .NET (pythonnet) 的帮助下使用 DLL。 此 DLL 中的方法需要(指向?)变量作为参数。

我可以弄清楚如何调用方法并提供整数变量作为参数。

如果需要特殊类型,我无法弄清楚如何提供这样的参数变量(此处:FTD2XX_NET.FTDI.FT_DEVICE_INFO_NODE[])。

当尝试调用 FTDI.GetDeviceList 的不同变体(其中一些在技术上是相同的)时,我收到不同类型的错误:

类型错误:没有方法与给定参数匹配

  • 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 ,.Net 包装器是从这里下载的: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)

最终我放弃了这个函数,因为我无法让它返回我想要的东西,而是在打开我想要的设备后分别使用“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")

关于Python for .NET : How to provide a variable of special type (FTDI. FT_DEVICE_INFO_NODE[]) 作为方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50254745/

相关文章:

python - redis-py ttl 返回无,但 redis 文档引用 -1

mysql - 多行数据读取器

c# - ASP缓存新手-尝试模拟WinForms功能/正确的协议(protocol)

c - 如何避免库查找 CMakeLists 功能

python - 如何验证 namedtuple 值?

python - SQLAlchemy 和多列不区分大小写的查询

python - 理解 Python 中的全局变量

c# - 文件夹选取器在移动设备上引发异常

c - 未定义的引用错误

macos - Arduino 未在 Mac Catalina 10.15.4 上检测到 FTDI 串行