Solved QSerialPort cannot read Asynchronously
-
Hi,
I just tried a simple qt console program (in Qt 5.9.5) which is only reading data from virtual serial port with commandsocat -d -d pty,raw,echo=0 pty,raw,echo=0
in my bash terminal. So the inputportName
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 alwaystrue
. And i found the problem is at lineconnect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::ReadHandler);
which signalreadyRead
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)
-
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
-
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.
-
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? -
@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.
-
Hi @kuzulis,
Thank you very much for suggesting me usingtty0tty
instead ofsocat
.
My program won't read serial data because I accidentally close port after I declare theSerialRead
class.