Issue adding incoming serial data in QTextEdit
-
I made a serial terminal interface with Qt Creator 4.14.2 and Qt 5.15.2 . I need to read the incoming serial data both in ASCII and HEX format. Following my code where connect the serial signal to my read slot:
connect(serial, &QSerialPort::readyRead, this, &Terminal::readData); void Terminal::readData() { QByteArray data = serial->readAll(); ui->readHEXTextEdit->insertPlainText(data.toHex()); ui->readASCIITextEdit->insertPlainText(data); }The Application seems to work nice about the HEX, but I have the ASCII string truncated randomly. I should have
! INFO: packet START received INFO: device startedbut I have

The first QTextEdit object is about the ASCII representation, the second one about HEX. Why this behaviour?
-
I don't see what this has to do with Qt nor with LabView - it's plain C. A string ends with
\0. If you want to display special unprintable characters you have to convert them by your own the way you want them. -
I made a serial terminal interface with Qt Creator 4.14.2 and Qt 5.15.2 . I need to read the incoming serial data both in ASCII and HEX format. Following my code where connect the serial signal to my read slot:
connect(serial, &QSerialPort::readyRead, this, &Terminal::readData); void Terminal::readData() { QByteArray data = serial->readAll(); ui->readHEXTextEdit->insertPlainText(data.toHex()); ui->readASCIITextEdit->insertPlainText(data); }The Application seems to work nice about the HEX, but I have the ASCII string truncated randomly. I should have
! INFO: packet START received INFO: device startedbut I have

The first QTextEdit object is about the ASCII representation, the second one about HEX. Why this behaviour?
@Andrew23 said in Issue adding incoming serial data in QTextEdit:
serial->readAll();You seem to be assuming when
readyReadcallsreadDataall your data will be retrieved by onereadAll(). It (probably) won't. You will receive multiple calls toreadyReadwith more data. In some shape or form you must buffer the data received to accumulate it.If that isn't your issue, I can't work out from the hex what characters come after the
receivedword (but you can!). I think I can see you might be putting a\00character in the middle of the string passed toinsertPlainText(data), that may not be good? -
@Andrew23 said in Issue adding incoming serial data in QTextEdit:
serial->readAll();You seem to be assuming when
readyReadcallsreadDataall your data will be retrieved by onereadAll(). It (probably) won't. You will receive multiple calls toreadyReadwith more data. In some shape or form you must buffer the data received to accumulate it.If that isn't your issue, I can't work out from the hex what characters come after the
receivedword (but you can!). I think I can see you might be putting a\00character in the middle of the string passed toinsertPlainText(data), that may not be good?@JonB said in Issue adding incoming serial data in QTextEdit:
@Andrew23 said in Issue adding incoming serial data in QTextEdit:
serial->readAll();You seem to be assuming when
readyReadcallsreadDataall your data will be retrieved by onereadAll(). It (probably) won't. You will receive multiple calls toreadyReadwith more data. In some shape or form you must buffer the data received to accumulate it.If that isn't your issue, I can't work out from the hex what characters come after the
receivedword (but you can!). I think I can see you might be putting a\00character in the middle of the string passed toinsertPlainText(data), that may not be good?Yes I have special ASCII characters (as \00 or \02 and so on). But the problem is that sometimes I have the words truncated in the middle, so I think the problem is not about the special characters. The HEX strig (I'm sure that it's complete and always the same!) is
022100040a494e464f3a207061636b657420535441525420726563656976656403021a00040a494e464f3a20646576696365207374617274656403but in this case the application get me
! device startedThe result is never the same
-
@Andrew23 said in Issue adding incoming serial data in QTextEdit:
but in this case the application get me
I would be surprised if anything else would be displayed... 0x21 is
!and then 0x00 terminates the string. -
@Andrew23 said in Issue adding incoming serial data in QTextEdit:
but in this case the application get me
I would be surprised if anything else would be displayed... 0x21 is
!and then 0x00 terminates the string.@Christian-Ehrlicher said in Issue adding incoming serial data in QTextEdit:
@Andrew23 said in Issue adding incoming serial data in QTextEdit:
but in this case the application get me
I would be surprised if anything else would be displayed... 0x21 is
!and then 0x00 terminates the string.I get your point but with other environment (like Labview) I never had this issue. I'm newbie with Qt and maybe I'm using the QTextEdit in a wrong way.
-
I don't see what this has to do with Qt nor with LabView - it's plain C. A string ends with
\0. If you want to display special unprintable characters you have to convert them by your own the way you want them. -
I don't see what this has to do with Qt nor with LabView - it's plain C. A string ends with
\0. If you want to display special unprintable characters you have to convert them by your own the way you want them.@Christian-Ehrlicher said in Issue adding incoming serial data in QTextEdit:
I don't see what this has to do with Qt nor with LabView - it's plain C. A string ends with
\0. If you want to display special unprintable characters you have to convert them by your own the way you want them.Thanks for suggestion. I've modified my code
QByteArray data = serial->readAll(); QString dataAscii = data.replace(0x00, 0x20); <---- added replace QByteArray dataHex = data.toHex();and now seems to work like a charm.
-
@Christian-Ehrlicher said in Issue adding incoming serial data in QTextEdit:
I don't see what this has to do with Qt nor with LabView - it's plain C. A string ends with
\0. If you want to display special unprintable characters you have to convert them by your own the way you want them.Thanks for suggestion. I've modified my code
QByteArray data = serial->readAll(); QString dataAscii = data.replace(0x00, 0x20); <---- added replace QByteArray dataHex = data.toHex();and now seems to work like a charm.