Search code examples
python.netftdipython.netpythonnet

Python for .NET: How to provide a variable of special type (FTDI.FT_DEVICE_INFO_NODE[]) as method parameter


I want to use a DLL with the help of Python for .NET (pythonnet). The methods in this DLL require (pointer to?) variables as parameters.

I could figure out how to call a method and to provide an integer variable as parameter.

I could not figure out how to provide such a parameter variable if a special type is required (here: FTD2XX_NET.FTDI.FT_DEVICE_INFO_NODE[]).

When playing around with different variants of calling FTDI.GetDeviceList (some of them technically the same), I get different kind of type errors:

TypeError: No method matches given arguments

  • 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_)

TypeError: unindexable object

  • FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE()[:])

TypeError: type(s) expected

  • FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE[:])

  • FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE[1])

  • FTDI.GetDeviceList(ftdi.FT_DEVICE_INFO_NODE[0])

Here is a code summary, and the results:

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 I found some c#-code where FT_DEVICE_INFO_NODE[] and GetDeviceList are used in a method:

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;
}

What am I missing about providing FTDI.FT_DEVICE_INFO_NODE[] as a parameter to FTDI.GetDeviceList?

The USB driver from FTDI was downloaded here: http://www.ftdichip.com/Drivers/D2XX.htm, and the .Net wrapper was downloaded from here: http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/CSharp.htm


Solution

  • The source code is expecting a list containing FT_DEVICE_INFO_NODE. Here is the function declaration:

    public FTDI.FT_STATUS GetDeviceList(FTDI.FT_DEVICE_INFO_NODE[] devicelist)
    

    This code worked for me:

    ft_device_info_node = ftdi.FT_DEVICE_INFO_NODE()
    ft_status = ftdi.GetDeviceList([ft_device_info_node])
    print(ft_status)
    

    In the end I gave up on this function because I couldn't get it to return what I wanted, and instead used the "GetDeviceID", "GetDescription", and "GetSerialNumber" functions separately after opening the device I wanted to get info from.

    Example:

    # 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")