Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QtSerialPort problem (C++) (QML)

QtSerialPort problem (C++) (QML)

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 777 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MonoFoxM Offline
    MonoFoxM Offline
    MonoFox
    wrote on last edited by
    #1

    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();
    }
    
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      MonoFoxM 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        MonoFoxM Offline
        MonoFoxM Offline
        MonoFox
        wrote on last edited by
        #3

        @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";
                     }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved