Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. try to read data from QSerialPort
Qt 6.12 LTS is out! See what's new in the release blog

try to read data from QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.9k Views
  • 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.
  • F Offline
    F Offline
    fehe
    wrote on last edited by
    #1

    Hi there,
    I'm new to QT and try to read data from an serial port. I use the QSerialPort.

    I have a connect statement like that:

    Comcontroller::Comcontroller(QSerialPort *serialPort, QObject *parent)
        : QObject(parent)
        , m_serialPort(serialPort){
    
        connect(m_serialPort,SIGNAL(readyRead()),this, SLOT(ReceiveData()));
        qDebug() << "Start reading COM:" << m_serialPort->portName();
        if (!m_serialPort->open(QIODevice::ReadOnly)) {
                qDebug() << QObject::tr("Failed to open port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
        }
    }
    

    and my SLOT (ReceiveData()) is never reached..

    I know that the data is coming in because when i try to do it synchronously with code like that:

        QByteArray readData = serialPort.readAll();
            while (serialPort.waitForReadyRead(5000))
                readData.append(serialPort.readAll());
    
            if (serialPort.error() == QSerialPort::ReadError) {
                qDebug() << QObject::tr("Failed to read from port %1, error: %2").arg(serialPort.portName()).arg(serialPort.errorString()) << endl;
            } else if (serialPort.error() == QSerialPort::TimeoutError && readData.isEmpty()) {
                qDebug() << QObject::tr("No data was currently available for reading from port %1").arg(serialPort.portName()) << endl;
                       }
    
           qDebug() << QObject::tr("Data successfully received from port %1").arg(serialPort.portName()) << endl;
           qDebug() << readData << endl;
    

    the data is shown correctly. And i really dont know why ReceiveData() is never reached..

    Maybe someone of you have an idea why its like that.

    If you need further information please let me know!

    Greetings
    Felix

    JonBJ 1 Reply Last reply
    0
    • F fehe

      Hi there,
      I'm new to QT and try to read data from an serial port. I use the QSerialPort.

      I have a connect statement like that:

      Comcontroller::Comcontroller(QSerialPort *serialPort, QObject *parent)
          : QObject(parent)
          , m_serialPort(serialPort){
      
          connect(m_serialPort,SIGNAL(readyRead()),this, SLOT(ReceiveData()));
          qDebug() << "Start reading COM:" << m_serialPort->portName();
          if (!m_serialPort->open(QIODevice::ReadOnly)) {
                  qDebug() << QObject::tr("Failed to open port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
          }
      }
      

      and my SLOT (ReceiveData()) is never reached..

      I know that the data is coming in because when i try to do it synchronously with code like that:

          QByteArray readData = serialPort.readAll();
              while (serialPort.waitForReadyRead(5000))
                  readData.append(serialPort.readAll());
      
              if (serialPort.error() == QSerialPort::ReadError) {
                  qDebug() << QObject::tr("Failed to read from port %1, error: %2").arg(serialPort.portName()).arg(serialPort.errorString()) << endl;
              } else if (serialPort.error() == QSerialPort::TimeoutError && readData.isEmpty()) {
                  qDebug() << QObject::tr("No data was currently available for reading from port %1").arg(serialPort.portName()) << endl;
                         }
      
             qDebug() << QObject::tr("Data successfully received from port %1").arg(serialPort.portName()) << endl;
             qDebug() << readData << endl;
      

      the data is shown correctly. And i really dont know why ReceiveData() is never reached..

      Maybe someone of you have an idea why its like that.

      If you need further information please let me know!

      Greetings
      Felix

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @fehe
      Is your Comcontroller object kept in existence? Your slot is in the instance.

      F 1 Reply Last reply
      0
      • JonBJ JonB

        @fehe
        Is your Comcontroller object kept in existence? Your slot is in the instance.

        F Offline
        F Offline
        fehe
        wrote on last edited by
        #3

        @JonB
        Hi,

        i call it in my mainwindow like that:

         Comcontroller controller(&serialPort);
        

        so i guess it should be kept in existence

        JonBJ 1 Reply Last reply
        0
        • F fehe

          @JonB
          Hi,

          i call it in my mainwindow like that:

           Comcontroller controller(&serialPort);
          

          so i guess it should be kept in existence

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @fehe
          Well from that I can see it's a local variable. But where is that code?

          F 1 Reply Last reply
          0
          • JonBJ JonB

            @fehe
            Well from that I can see it's a local variable. But where is that code?

            F Offline
            F Offline
            fehe
            wrote on last edited by
            #5

            @JonB
            Well in my MainWindow.. so not in my "Comcontroller".. but maybe thats the problem, that its no member of it, so it will be created and terminated right away?

            JonBJ 1 Reply Last reply
            0
            • F fehe

              @JonB
              Well in my MainWindow.. so not in my "Comcontroller".. but maybe thats the problem, that its no member of it, so it will be created and terminated right away?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @fehe
              It's a bit hard to be sure without seeing the code, but if you mean in your QMainWindow instance then that sounds OK. What you cannot do is put it in some function which has exited, like:

              func()
              {
                  ...
                  Comcontroller controller(&serialPort);
                 ...
              }
              

              After you have called func(), you're not left with a Comcontroller to have slots for signals from the serial port. So long as you understand that. Other questions I have seen let the local variable go out of scope without realising that will lose their slot code.

              The other thing is, unlike the synchronous code, you must allow the main Qt event loop to be running for the readyRead() to be signalled. I take it you are doing that?

              1 Reply Last reply
              3

              • Login

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