简体   繁体   中英

In QT how can I detect when a USB port switches from serial mode to DFU when connected to an STM32

I have a Qt5.12.4 MinGw64 app where I want to catch a USB event. In Windows 10 the MS driver for STM/USB emits error messages and I can use that as a trigger. Inelegant but it works, until I try to run it in a Win7-8.1 app where the driver is a third party STM driver with a VCP wrapper. I am thinking I need to adopt libusb to try and catch the ports change of state, but I am at a loss as how to proceed. I can see the port info in Device Manager, I just dont know how to get to it. Some of the questions going through my head....

1) Can I just make an OS call to read the port info? (if so, how?)

2) Can libusb and QSerialport co-exist on the same port?

3) What calls to LibUSB1.0 do I make to query the port status?

4) Is there a Windows cli utility like lsusb (wmic??) where I could scrape the data?

5) Which solution is likely to be the best cross platform solution?

I am using this trigger to start dfuse as a process that does a firmware update automatically on my STM board.

I have looked over the libusb1.0 docs but I do not understand just how I can use it. If that is correct solution, an example of how to query the Com port data and state would be most appreciated.

I tried using qDebug() to print out all of the serialportInfo data, while in serial or DFU state, but there is nothing there that is useful that I can use as a trigger.

USB serial mode = Serial port info is: ("COM3", "USB Serial Device", "Microsoft", "00000000001A", "\\\\.\\COM3", "483", "5740", "1", " no data", "1")

USB DFU mode = Serial port info is: ("COM3", "N/A", "N/A", "N/A", "\\\\.\\COM3", "N/A", "N/A", "no data", " no data", "no data")

I need some direction as to how to grab this port info so I dont really have any code that matters, but I am including an excerpt of my working process function.

This code works just fine to actually perform the firmware load. I just need a way to actually trigger it from a USB port change of state

void updateDevice_Dialog::update_firmware(QString fileName)
{
    qDebug() << "Updating firmware: " << fileName ;

    QDir dir;

    ui->progress_label->setText("Preparing to update Firmware .....");
    if(dir.setCurrent(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)+"/firmware"))
    {
        QSettings settings;
        QString comPort = settings.value("USBPort").toString();
        ui->progress_label->setText("Setting port to: "+comPort+" and starting download .....");
        ui->avr_progressBar->setValue(0);
        ui->avr_progressBar->setRange(0,100);
        ui->avr_progressBar->setHidden(false);

        progress_steps = 0;  //reset avrProcess line output counter;
        qDebug() << "Starting process for stm-dfu on serial port: " << comPort;

        connect(avr_Process,SIGNAL(error(QProcess::ProcessError)),this,SLOT(process_error(QProcess::ProcessError)));
        connect(avr_Process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(process_finished(int,QProcess::ExitStatus)));
        connect(avr_Process,SIGNAL(readyReadStandardOutput()),this,SLOT(process_readLine()));
        connect(avr_Process,SIGNAL(errorOccurred(QProcess::ProcessError)),avr_Process,SLOT(kill()));
        connect(avr_Process,SIGNAL(error(QProcess::ProcessError)),ui->avr_progressBar,SLOT(close()));

        QString dfu_command = "\""+QCoreApplication::applicationDirPath()+"/Tools/\"dfusecommand -c -d --v --fn "
                              "\""+QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)+"/firmware/\""+fileName;

        qDebug().noquote() << "dfu command string is: "<< dfu_command << "  Current dir is: " << dir.currentPath();


        avr_Process->start(dfu_command);
        avr_Process->waitForFinished(20000);
}
}

From the perspective of the host system, rebooting the microcontroller into DFU will look like the original device was disconnected, and a completely different device was plugged in shortly afterwards.

If you need to watch for this, set up a libusb hotplug callback so that you'll be notified when the DFU device is attached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM