Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSerialPort cannot read Asynchronously
Qt 6.11 is out! See what's new in the release blog

QSerialPort cannot read Asynchronously

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 3 Posters 1.6k 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.
  • A Offline
    A Offline
    Afif Zaki
    wrote on last edited by Afif Zaki
    #1

    Hi,
    I just tried a simple qt console program (in Qt 5.9.5) which is only reading data from virtual serial port with command socat -d -d pty,raw,echo=0 pty,raw,echo=0 in my bash terminal. So the input portName is "/dev/pts/1" (for writing data) and output is "/dev/pts/2" (for reading data).

    The program is totally same with QSerialPort Example: Command Line Reader Async Example. However, I changed several codes for the input so that I can manually configure the port inside the program.

    This is a the constructor of serialportreader class:

    #include "serialportreader.h"
    SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent) :
        QObject(parent),
        m_serialPort(serialPort),
        qtout(stdout)
    {
        qtout << m_serialPort->isOpen() << endl;
        connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::ReadHandler);
        connect(m_serialPort, &QSerialPort::errorOccurred, this, &SerialPortReader::ErrorHandler);
        connect(&timer, &QTimer::timeout, this, &SerialPortReader::TimeOutHandler);
        timer.start(5000);
    }
    

    That program cannot reading / receiving the data I sent even serial.isOpen() && serial.isReadable is always true. And i found the problem is at line connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::ReadHandler); which signal readyRead cannot emitted while data is already sent to reader. The reason I used that is I want to reading data from serial port asyncronously which is more comfortable for receiving data from many serial port simultaneously.

    Here my writer program:

    #include <QCoreApplication>
    #include <QTextStream>
    #include <QtSerialPort/QSerialPort>
    #include <QByteArray>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QTextStream qtin(stdin);
        QTextStream qtout(stdout);
    
        QSerialPort *serialPort;
        serialPort = new QSerialPort("/dev/pts/1");
    
        serialPort->setBaudRate(QSerialPort::Baud9600);
        serialPort->setFlowControl(QSerialPort::NoFlowControl);
        serialPort->setParity(QSerialPort::NoParity);
        serialPort->setDataBits(QSerialPort::Data8);
        serialPort->setStopBits(QSerialPort::OneStop);
    
        serialPort->open(QIODevice::WriteOnly);
    
        if (serialPort->isOpen() && serialPort->isWritable())
            qtout << "Port is Available" << endl;
        else
            qtout << "Port is Not Available" << endl;
    
        // Write Data //
        qtout << "Writting Message..." << endl;
        serialPort->write("asd");
        //serialPort->flush();
        serialPort->waitForBytesWritten(10000);
        qtout << "Finished" << endl;
    
        serialPort->close();
        a.exit(1);
        //return a.exec();
    }
    

    What should I do? or Do you have any advice for my problem? (sorry for my bad English)

    aha_1980A 1 Reply Last reply
    0
    • A Afif Zaki

      Hi,
      I just tried a simple qt console program (in Qt 5.9.5) which is only reading data from virtual serial port with command socat -d -d pty,raw,echo=0 pty,raw,echo=0 in my bash terminal. So the input portName is "/dev/pts/1" (for writing data) and output is "/dev/pts/2" (for reading data).

      The program is totally same with QSerialPort Example: Command Line Reader Async Example. However, I changed several codes for the input so that I can manually configure the port inside the program.

      This is a the constructor of serialportreader class:

      #include "serialportreader.h"
      SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent) :
          QObject(parent),
          m_serialPort(serialPort),
          qtout(stdout)
      {
          qtout << m_serialPort->isOpen() << endl;
          connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::ReadHandler);
          connect(m_serialPort, &QSerialPort::errorOccurred, this, &SerialPortReader::ErrorHandler);
          connect(&timer, &QTimer::timeout, this, &SerialPortReader::TimeOutHandler);
          timer.start(5000);
      }
      

      That program cannot reading / receiving the data I sent even serial.isOpen() && serial.isReadable is always true. And i found the problem is at line connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::ReadHandler); which signal readyRead cannot emitted while data is already sent to reader. The reason I used that is I want to reading data from serial port asyncronously which is more comfortable for receiving data from many serial port simultaneously.

      Here my writer program:

      #include <QCoreApplication>
      #include <QTextStream>
      #include <QtSerialPort/QSerialPort>
      #include <QByteArray>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          QTextStream qtin(stdin);
          QTextStream qtout(stdout);
      
          QSerialPort *serialPort;
          serialPort = new QSerialPort("/dev/pts/1");
      
          serialPort->setBaudRate(QSerialPort::Baud9600);
          serialPort->setFlowControl(QSerialPort::NoFlowControl);
          serialPort->setParity(QSerialPort::NoParity);
          serialPort->setDataBits(QSerialPort::Data8);
          serialPort->setStopBits(QSerialPort::OneStop);
      
          serialPort->open(QIODevice::WriteOnly);
      
          if (serialPort->isOpen() && serialPort->isWritable())
              qtout << "Port is Available" << endl;
          else
              qtout << "Port is Not Available" << endl;
      
          // Write Data //
          qtout << "Writting Message..." << endl;
          serialPort->write("asd");
          //serialPort->flush();
          serialPort->waitForBytesWritten(10000);
          qtout << "Finished" << endl;
      
          serialPort->close();
          a.exit(1);
          //return a.exec();
      }
      

      What should I do? or Do you have any advice for my problem? (sorry for my bad English)

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Afif-Zaki

      The program is totally same with QSerialPort Example: Command Line Reader Async Example, but only I changed the input so that I can configure the port inside the program.

      does that mean, the Command Line Reader Example does also not work?

      I'm not sure these kind of virtual ports are supported, @kuzulis can say more to this topic

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by kuzulis
        #3

        Just use tty0tty as a kernel module, instead of socat.

        PS: I'm don't care about pseudo terminals, as it is not a serial ports.

        A 1 Reply Last reply
        2
        • K kuzulis

          Just use tty0tty as a kernel module, instead of socat.

          PS: I'm don't care about pseudo terminals, as it is not a serial ports.

          A Offline
          A Offline
          Afif Zaki
          wrote on last edited by
          #4

          Hi @kuzulis,
          I just tried using tty0tty with /dev/tnt0 as input and /dev/tnt1 as output. Still, the reader program cannot read/receive data.

          So, the readyRead signal won't able to emitted if data sent from the virtual port?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by
            #5

            @Afif-Zaki said in QSerialPort cannot read Asynchronously:

            So, the readyRead signal won't able to emitted if data sent from the virtual port?

            No, this means, that you something doing wrong.

            A 1 Reply Last reply
            0
            • K kuzulis

              @Afif-Zaki said in QSerialPort cannot read Asynchronously:

              So, the readyRead signal won't able to emitted if data sent from the virtual port?

              No, this means, that you something doing wrong.

              A Offline
              A Offline
              Afif Zaki
              wrote on last edited by
              #6

              Hi @kuzulis,
              Thank you very much for suggesting me using tty0tty instead of socat.
              My program won't read serial data because I accidentally close port after I declare the SerialRead class.

              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