How I listen serial port with writing client code?
-
I am working on communication of virtual serial ports. I opened and wrote the data to the virtual serial port. Then, I listened with using socket.
cat /dev/tnt0
But now, I want to listen the tnt0 serial port over qt client code. How can I do that? If you give some examples, it would be nice.
main.cpp#include <QCoreApplication> #include "myserialport.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MySerialPort s; s.openSerialPort(); return a.exec(); }
myserialport.h
#ifndef MYSERIALPORT_H #define MYSERIALPORT_H #include <QObject> #include <QSerialPort> class MySerialPort : public QObject { Q_OBJECT public: explicit MySerialPort(QObject *parent = nullptr); void openSerialPort(); void closeSerialPort(); void writeData(const QByteArray &); signals: public slots: void readData(); void handleError(QSerialPort::SerialPortError); private: QSerialPort *serial; }; #endif // MYSERIALPORT_H
myserialport.cpp
#include "myserialport.h" #include <QSerialPort> #include <QDebug> #include <QSerialPortInfo> MySerialPort::MySerialPort(QObject *parent) : QObject(parent) { serial=new QSerialPort(this); connect(serial,SIGNAL(readyRead()),this,SLOT(readData())); connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError))); } void MySerialPort::openSerialPort() { serial->setPortName("/dev/tnt0"); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setFlowControl(QSerialPort::NoFlowControl); serial->setStopBits(QSerialPort::OneStop); serial->setParity(QSerialPort::NoParity); if(serial->open(QIODevice::ReadWrite)) { qDebug()<<"opened"; QByteArray data="hello"; writeData(data); } else { qDebug()<<QSerialPort::PermissionError; } } void MySerialPort::closeSerialPort() { if(serial->isOpen()) { serial->close(); } } void MySerialPort::writeData(const QByteArray &data) { qDebug() << "Bytes Written:" << serial->write(data); } void MySerialPort::readData() { serial->readAll(); } void MySerialPort::handleError(QSerialPort::SerialPortError error) { if (error == QSerialPort::NoError) { qDebug()<<"no error"; } }
-
@elypheldia What is the problem with the code you have submitted?
NOTE: I would use new connect syntax to avoid failures about signals/slots not existing:
//OLD syntax connect(serial,SIGNAL(readyRead()), this,SLOT(readData())); connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError))); // NEW syntax connect(serial, &QSerialPort::readyRead, this, &MySerialPort::readData); connect(serial, &QSerialPort::error, this, &MySerialPort::handleError);
-
@KroMignon Thx for editing. There is no problem that I have submitted. I just wanna read tnt0 serial port by writing client code, not using socket. (That's why I submitted the code that I listen with socket.)
-
@elypheldia said in How I listen serial port with writing client code?:
I just wanna read tnt0 serial port by writing client code, not using socket. (That's why I submitted the code that I listen with socket.)
I don't understand this, which socket are you talking about?
You code is base onQSerialPort
, so you are using serial port interface not any socket.void MySerialPort::readData() { QByteArray data = serial->readAll(); QDebug() << "Received" << data.size() << "bytes:" << QString(data.toHex(':')); }
-
@KroMignon Sorry if used the wrong term. I am new on qt. I wanted to indicate that I read the data I wrote to the tnt0 serial port from terminal with using cat /dev/tnt0. But How can I do this with client code instead of reading from terminal. I just wanna open the tnt1 serial port from code., and I wanna read from there.
-
@elypheldia said in How I listen serial port with writing client code?:
Sorry if used the wrong term. I am new on qt. I wanted to indicate that I read the data I wrote to the tnt0 serial port from terminal with using cat /dev/tnt0. But How can I do this with client code instead of reading from terminal. I just wanna open the tnt1 serial port from code., and I wanna read from there.
You are confusing to me.
Do you want to use /dev/tnt0 or /dev/tnt1?With the code example you have provided, you are writing
hello
on serial device /dev/tnt0. Is this working?You have defined a slot
MySerialPort::readData()
, which is called when data are ready to be read from serial port.
I have proposed you some changes to display debug message when slot is called. Is this slot called?I don't know:
- what kind of device is connected to this serial port?
- if the serial port configuration is correct according to attached device?
- the attached device communication protocol: It is a binary or text protocol? Are carriage return or line feed required?