Cannot write a data to a virtual serial port ?
-
I am new in QT developing. I opened a virtual serial port, but I cannot write a data to this. How can I write ?
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(); private: QSerialPort *serial; }; #endif // MYSERIALPORT_H
myserialport.cpp
#include "myserialport.h" #include <QSerialPort> #include <QDebug> MySerialPort::MySerialPort(QObject *parent) : QObject(parent) { serial=new QSerialPort(this); connect(serial,SIGNAL(readyRead()),this,SLOT(readData())); } 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"; } else { qDebug()<<QSerialPort::PermissionError; } } void MySerialPort::closeSerialPort() { if(serial->isOpen()) { serial->close(); } } void MySerialPort::writeData(const QByteArray &data) { serial->write(data); } void MySerialPort::readData() { QByteArray data="hello"; writeData(data); serial->readAll(); qDebug()<<data; }
-
@elypheldia said in Cannot write a data to a virtual serial port ?:
@J-Hilk I already did this. But I get no output. I am very new, sorry. Thx to all for replies.
thats why I said:
make sure you actually write something before you expect readData to be called
change your openSerialPort to this:
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; } }
it will, at the very, least attempt to write something, and should return a bytes written value and/or an error message
-
@elypheldia said in Cannot write a data to a virtual serial port ?:
void MySerialPort::readData()
{
QByteArray data="hello";
writeData(data);
serial->readAll();
qDebug()<<data;
}Why do you write data in a port connected to readyRead signal?
You should at least first read all the data and then write. -
@elypheldia Well, simply change order:
void MySerialPort::readData() { serial->readAll(); QByteArray data="hello"; writeData(data); qDebug()<<data; }
-
@elypheldia What does serial->write(data); return?
Also, connect a slot to https://doc.qt.io/qt-5/qserialport.html#errorOccurred and see whether there es any error. -
@jsulm I added this slot to handle error.
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
void MySerialPort::handleError(QSerialPort::SerialPortError error) { if (error == QSerialPort::NoError) { qDebug()<<"no error"; } }
And, I got "no error " output.
I added to writeData functionqDebug()<<"written";
But, I did not get output. Port is opened, but couldn't write.
-
@elypheldia
@jsulm asked you toWhat does serial->write(data); return?
Can you just confirm/tell us what that
write()
returns? -
@elypheldia
Then we would suggest to you (I believe!) that the bytes are being sent. So what is your "proof" that they are not? For example, for all I know if you expect a "response" your write might not be correct, it might require a\n
or something at the end.Further, you call
serial->readAll();
immediately after executing thewrite()
. I don't know about serial ports, but in general this is not right in Qt, the data sent back may arrive a while later. You are supposed to usereadyRead()
signal for asynchronous operation. [OK, I see you do that. Your protocol first waits for bytes to arrive initially, then writes something back; is your device then supposed to send something back to you? If so, it will then ping-pong endlessly?] Unless you/ @jsulm know better?I know Wireshark can be used on PC to view TCP/IP/socket traffic. Is there a tool you can use to verify whether your bytes are being sent to the Linux serial device?
-
@elypheldia said in Cannot write a data to a virtual serial port ?:
number of written bytes
Could you please tell us the number?
-
@elypheldia said in Cannot write a data to a virtual serial port ?:
@jsulm I cannot. Because, I dont know how to learn number of written bytes.
are you joking ?
you already use qDebug() in your code example, use it some more
qDebug() << "Bytes Written:" << serial->write(data);that said, make sure you actually write something before you expect readData to be called.
-
@elypheldia said in Cannot write a data to a virtual serial port ?:
@J-Hilk I already did this. But I get no output. I am very new, sorry. Thx to all for replies.
thats why I said:
make sure you actually write something before you expect readData to be called
change your openSerialPort to this:
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; } }
it will, at the very, least attempt to write something, and should return a bytes written value and/or an error message