示例#1
0
QVector<CommPortPtr>
D2XX::myListCommPorts(QString &err)
{
    QVector<CommPortPtr> result;
    if (!lib) {
        lib = new D2XXWrapper;
        if (!lib->init(err)) {
            delete lib;
            lib = NULL;
            return result;
        }
    }
    DWORD numDevs;
    FT_STATUS ftStatus = lib->create_device_info_list(&numDevs);
    if(ftStatus != FT_OK) {
        err = QString("FT_CreateDeviceInfoList: %1").arg(ftStatus);
        return result;
    }
    FT_DEVICE_LIST_INFO_NODE *devInfo = new FT_DEVICE_LIST_INFO_NODE[numDevs];
    ftStatus = lib->get_device_info_list(devInfo, &numDevs);
    if (ftStatus != FT_OK)
        err = QString("FT_GetDeviceInfoList: %1").arg(ftStatus);
    else {
        for (DWORD i = 0; i < numDevs; i++)
            result.append(CommPortPtr(new D2XX(devInfo[i])));
    }
    delete [] devInfo;
    // If we can't open a D2XX device, it's usually because the VCP drivers
    // are installed, so it should also show up in the list of serial devices.
    for (int i = 0; i < result.size(); ++i) {
        CommPortPtr dev = result[i];
        QString tmp;
        if (dev->open(tmp))
            dev->close();
        else
            result.remove(i--);
    }
    return result;
}