Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 started
    

    but I have

    alt text

    The first QTextEdit object is about the ASCII representation, the second one about HEX. Why this behaviour?


  • Lifetime Qt Champion

    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.



  • @Andrew23 said in Issue adding incoming serial data in QTextEdit:

    serial->readAll();

    You seem to be assuming when readyRead calls readData all your data will be retrieved by one readAll(). It (probably) won't. You will receive multiple calls to readyRead with 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 received word (but you can!). I think I can see you might be putting a \00 character in the middle of the string passed to insertPlainText(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 readyRead calls readData all your data will be retrieved by one readAll(). It (probably) won't. You will receive multiple calls to readyRead with 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 received word (but you can!). I think I can see you might be putting a \00 character in the middle of the string passed to insertPlainText(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

    022100040a494e464f3a207061636b657420535441525420726563656976656403021a00040a494e464f3a20646576696365207374617274656403
    

    but in this case the application get me

    !
     device started
    

    The result is never the same


  • Lifetime Qt Champion

    @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.


  • Lifetime Qt Champion

    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.



  • @Andrew23
    Which is why I wrote:

    I think I can see you might be putting a \00 character in the middle of the string passed to insertPlainText(data), that may not be good?

    :)


Log in to reply