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

QtSerialPort problem (C++) (QML)



  • Hello,
    I'am trying to read frames from USB CAN UCCB Converter which sends data in hex to the COM port. I have created a counter and a primitive code, which is supposed to read data from the COM port. I have a problem with this, I can send data to the port but when downloading data I have simply '0'
    https://scr.hu/L22RyAZ

    Main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlComponent>
    #include <QTextStream>
    #include <QDebug>
    
    #include <QSerialPort>
    #include <QSerialPortInfo>
    
    #include <QDataStream>
    #include <QQmlProperty>
    #include <QTextStream>
    
    #include <iostream>
    namespace  {
     QSerialPort serialPort;
    
    }
    
    
     void Read_Data();
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
            QGuiApplication app(argc, argv);
    
            // Load gauge UI.
            QQmlEngine engine;
            QQmlComponent component(&engine, "qrc:/main.qml");
            QObject *object = component.create();
    
          object->setProperty("fuel_frame", 31);
           object->setProperty("kph_frame", "150");
            object->setProperty("rpm_frame", "3000");
    
              object->setProperty("rpm_arc", 300);
                object->setProperty("kph_arc", 150);
    
    
    
            serialPort.setPortName("COM2");
            serialPort.setBaudRate(QSerialPort::Baud9600);
            serialPort.setDataBits(QSerialPort::Data8);
                     serialPort.setParity(QSerialPort::NoParity);
                     serialPort.setStopBits(QSerialPort::OneStop);
                     serialPort.setFlowControl(QSerialPort::NoFlowControl);
             serialPort.open(QIODevice::ReadWrite);
            if(serialPort.isOpen())
                     {
    
                         qDebug() << "Port is open: " << serialPort.portName();
                         // Connect framesRecieved signal to slot function for reading frames.
    
                        Read_Data();
    
                     }
                     else
                     {
                         qDebug() << "Port is closed";
                     }
    
        return app.exec();
    }
    
    void Read_Data()
    {
    
        long receive_num = 0;
        bool flag_showInHex = true;
        QByteArray buf;
        buf = serialPort.readAll();
        receive_num += buf.count();
        qDebug() << "R:" + QString::number(receive_num);
        if(!buf.isEmpty())
        {
            if(flag_showInHex == true){
                qDebug() << "buf.count: " << buf.count();
                int i = 0;
                QString str_buf;
                for(i = 0; i < buf.count(); i++){
                    QString s;
                    s.sprintf("%02X ", (unsigned char)buf.at(i));
                    str_buf += s;
                }
                qDebug() << str_buf;
    
    
    
            }else{
    
            }
    
        }
        buf.clear();
    }
    
    
    

  • Lifetime Qt Champion

    Hi,

    Are you really connecting Read_Data or just calling it as in the code you provided ? If the later, then that's normal, a serial port is not a text file that you open and read. Data must have flown to it before you can read something.



  • @SGaist
    Thanks for answer.
    I just calling Read_Data when SerialPort is open.
    To testing i've create com bridge COM2<>COM3 : https://scr.hu/L22Rl8y

    if(serialPort.isOpen())
    {

                qDebug() << "Port is open: " << serialPort.portName();
                
                if(serialPort.bytesAvailable()){
        
                   Read_Data();
                   
                }else{
                    qDebug() << "Null";
                }
    
                
                 }
                 else
                 {
                     qDebug() << "Port is closed";
                 }
    

  • Lifetime Qt Champion

    As written before, it's not how serial ports are working.

    You should take advantage of Qt's asynchronous capabilities like shown in the Terminal example.


Log in to reply