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

How to send data from QT app to STM32 by serial port communication



  • I am tring to send data from my QT app to the STM32F7 board using USB serial communication.
    from the board side i used STM32 USB CDC class, i connect the card to the PC then i can open the usb port using realterm or herculus, but i can't open it with Qt .
    i get this code error : QSerialPort::SerialPortError(UnsupportedOperationError)
    i am developping on windows 10.
    this is the code:

    QList<QSerialPortInfo> UsbPorts;
    UsbPorts =   QSerialPortInfo::availablePorts();
    
    qDebug() << "Number of serial ports:" << QSerialPortInfo::availablePorts().count();
    
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
                    qDebug()<<"Name: "<<info.portName();
                    qDebug()<<"Description: "<<info.description();
                    qDebug()<<"Manufactures: "<<info.manufacturer();
                    
                    QSerialPort *port = new QSerialPort(info);               
                    if (port->open(QIODevice::ReadWrite)) {
                                qDebug() << "Baud rate:" << port->baudRate();
                                qDebug() << "Data bits:" << port->dataBits();
                                qDebug() << "Stop bits:" << port->stopBits();
                                qDebug() << "Parity:" << port->parity();
                                qDebug() << "Flow control:" << port->flowControl();
                                qDebug() << "Read buffer size:" << port->readBufferSize();
                                qDebug() << "Read buffer size:" <<port->portName();
                                
                                port->close();
    
                        } else {
                                qDebug() << "Unable to open port, error code" << port->error();
                        }
                }
    

    [Edit aha_1980: added code tags]



  • @Hamza-B First there is a memory leak in your code.. I would suggest you not to use pointer there.

    Second, as mentioned in the documentation, try to open a QSerialPort with invalid settings ends with an error:

    Note: The method returns false if opening the port is successful, but could not set any of the port settings successfully. In that case, the port is closed automatically not to leave the port around with incorrect settings.

    So before opening the serial port, setup the communication.

    To resume:

    for(const auto &info : QSerialPortInfo::availablePorts())
    {
        qDebug()<<"Name: "<<info.portName();
        qDebug()<<"Description: "<<info.description();
        qDebug()<<"Manufactures: "<<info.manufacturer();
        
        QSerialPort port(info);
        
        port.setBaudRate (QSerialPort::Baud115200);
        port.setDataBits(QSerialPort::Data8);
        port.setParity (QSerialPort::NoParity);
        port.setStopBits (QSerialPort::OneStop);
        
        port.setFlowControl(QSerialPort::NoFlowControl);
        if (port.open(QIODevice::ReadWrite)) {
            qDebug() << "Baud rate:" << port.baudRate();
            qDebug() << "Data bits:" << port.dataBits();
            qDebug() << "Stop bits:" << port.stopBits();
            qDebug() << "Parity:" << port.parity();
            qDebug() << "Flow control:" << port.flowControl();
            qDebug() << "Read buffer size:" << port.readBufferSize();
            qDebug() << "Read buffer size:" <<port.portName();
            
            port.close();
    
        } else {
            qDebug() << "Unable to open port, error code" << port.error();
        }
    }
    

  • Qt Champions 2020

    i get this code error : QSerialPort::SerialPortError(UnsupportedOperationError)

    If your provided code is an 'actual' code, then this error can be caused if you have not implemented some calls in your USB CDC class.

    Also, you can re-build the QSerialPort yourself, add there the qDebug() output and to see where it fails. Look in the QSerialPort sources on a function QSerialPortPrivate::open: https://github.com/qt/qtserialport/blob/5.13/src/serialport/qserialport_win.cpp#L176


  • Qt Champions 2020

    @KroMignon said in How to send data from QT app to STM32 by serial port communication:

    So before opening the serial port, setup the communication.

    No, that's not true.



  • @kuzulis said in How to send data from QT app to STM32 by serial port communication:

    No, that's not true.

    what is not true?


  • Qt Champions 2020

    @KroMignon said in How to send data from QT app to STM32 by serial port communication:

    what is not true?

    This:

    "So before opening the serial port, setup the communication."



  • @KroMignon what you said was correct, i must set the communication first , then i sent the data and everything worked just fine . thanks



  • @Hamza-B Your welcome


  • Qt Champions 2020

    @Hamza-B said in How to send data from QT app to STM32 by serial port communication:

    , i must set the communication first

    Whats? By default the QSP opens in 9600 8 N 1.. So, it is strange why this defaults does not work for you.. Maybe 9600 baud does not supported by your USB CDC class?



  • @kuzulis i set the baud on 9600


Log in to reply