I want to list all COM ports, which are virtual ports of FTDI controllers.

To accopolish that, I tried to use the methods GetDeviceList and OpenBySerialNumber, provided by the official .NET wrapper ("FTD2XX_NET") for the official FTDI library:

List<string> listResult = new List<string>();

FTD2XX.FT_DEVICE_INFO_NODE[] arrInfoNodes =
    new FTD2XX.FT_DEVICE_INFO_NODE[intALotMoreThanExpectedInfoNodeCount];

FTD2XX fObject = createFtdiInstance();

foreach (FTD2XX.FT_DEVICE_INFO_NODE node in arrInfoNodes)
{
    if (node == null)
    {
        break;
    }
    else
    {
        if (fObject.OpenBySerialNumber(node.SerialNumber) ==
            Ftdi.FTD2XX.FT_STATUS.FT_OK)
        {
            fObject.GetCOMPort(out strPortName);
            listResult.Add(strPortName);
        }
    }
}

My Problem with this is now: After the Iteration over all nodes, the ftdi devices have to be unpluged and reconnected until I can use them again (with any Software).

有帮助吗?

解决方案

All I needed to do was to (read my own posting and) add the line

fObject.Close();

after the following one:

fObject.GetCOMPort(out strPortName);

So the complete foreach loop looks like this:

foreach (FTD2XX.FT_DEVICE_INFO_NODE node in arrInfoNodes)
{
    if (node == null)
    {
        break;
    }
    else
    {
        if (fObject.OpenBySerialNumber(node.SerialNumber) ==
            Ftdi.FTD2XX.FT_STATUS.FT_OK)
        {
            fObject.GetCOMPort(out strPortName);
            fObject.Close();  // <<-- New Code here!
            listResult.Add(strPortName);
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top