Cannot write a data to a virtual serial port ?
-
@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