Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

STM32 Virtual COM Port and Qt



  • Hey,
    My aim is to enable communication via USB CDC HS on STM32 with Ubuntu 20.04 based PC in Qt app created in QtCreator.

    So far I've managed to run communication via UART and everything is working fine. Then I decided to switch to USB and I still can read incoming data (but only in CuteCom) and in my Qt app nothing appears.

    To be honest I have no idea what is going on and where to look for mistakes. Here I put the code:

        void MainWindow::on_pushButtonConnect_clicked()
    {
     if (ui->comboBoxDevices->count() == 0){
         this->addToLogs("No devices found.");
                 return;
     }
     QString portName = ui->comboBoxDevices->currentText().split(" ").first();
     this->device->setPortName(portName);
     this->device->setBaudRate(QSerialPort::Baud115200);
     this->device->setDataBits(QSerialPort::Data8);
     this->device->setParity(QSerialPort::NoParity);
     this->device->setStopBits(QSerialPort::OneStop);
     this->device->setFlowControl(QSerialPort::NoFlowControl);
    
     if(device->open(QIODevice::ReadWrite)){
         this->addToLogs("Port opened");
         qDebug() << "Writing down the parameters...";
         qDebug() << "Baud rate:" << this->device->baudRate();
         qDebug() << "Data bits:" << this->device->dataBits();
         qDebug() << "Stop bits:" << this->device->stopBits();
         qDebug() << "Parity:" << this->device->parity();
         qDebug() << "Flow control:" << this->device->flowControl();
         qDebug() << "Read buffer size:" << this->device->readBufferSize();
         qDebug() << "Read buffer size:" << this->device->portName();
            if (this->device->canReadLine() == true)  {
                  qDebug() << "Reading lines.";
              }  else  {
                  qDebug() << "Cannot read lines.";
              }
              while (this->device->canReadLine()){
                  qDebug() << "Reading line: " << this->device->readLine();
              }
              connect(this->device, SIGNAL(readyRead()), this, SLOT(readFromPort()));
    

    And the readFromPort() function:

    void MainWindow::readFromPort()
    {
        while(this->device->canReadLine()){
            QString line = this->device->readLine();
            qDebug() << line;
            QString terminator = "\r";
            int pos = line.lastIndexOf(terminator);
            qDebug()<<line.left(pos);
            this->addToLogs(line.left(pos));
        }
    }
    

    Do you have any idea what might be wrong or not set properly? Output:

    Baud rate: 9600
    Data bits: QSerialPort::Data8
    Stop bits: QSerialPort::OneStop
    Parity: QSerialPort::NoParity
    Flow control: QSerialPort::NoFlowControl
    Read buffer size: 0
    Read buffer size: "ttyACM0"
    Cannot read lines.
    

    Would be thankful for all help.



  • Solution found. As it seems, in my code I put commands to read the port in if (with function canReadLine()). When I commented the whole condition out leaving just the reading, everything worked fine.

    Maybe someone will find a solution here. Cheers


Log in to reply