Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue adding incoming serial data in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Issue adding incoming serial data in QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 771 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew23
    wrote on 8 Apr 2021, 10:22 last edited by
    #1

    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?

    J 1 Reply Last reply 8 Apr 2021, 10:31
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 8 Apr 2021, 11:09 last edited by
      #6

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply 8 Apr 2021, 12:20
      4
      • A Andrew23
        8 Apr 2021, 10:22

        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?

        J Offline
        J Offline
        JonB
        wrote on 8 Apr 2021, 10:31 last edited by JonB 4 Aug 2021, 10:32
        #2

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

        A 1 Reply Last reply 8 Apr 2021, 10:55
        1
        • J JonB
          8 Apr 2021, 10:31

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

          A Offline
          A Offline
          Andrew23
          wrote on 8 Apr 2021, 10:55 last edited by Andrew23 4 Aug 2021, 11:01
          #3

          @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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 8 Apr 2021, 11:00 last edited by
            #4

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            A 1 Reply Last reply 8 Apr 2021, 11:04
            0
            • C Christian Ehrlicher
              8 Apr 2021, 11:00

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

              A Offline
              A Offline
              Andrew23
              wrote on 8 Apr 2021, 11:04 last edited by
              #5

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 8 Apr 2021, 11:09 last edited by
                #6

                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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                A 1 Reply Last reply 8 Apr 2021, 12:20
                4
                • C Christian Ehrlicher
                  8 Apr 2021, 11:09

                  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.

                  A Offline
                  A Offline
                  Andrew23
                  wrote on 8 Apr 2021, 12:20 last edited by
                  #7

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

                  J 1 Reply Last reply 8 Apr 2021, 13:24
                  0
                  • A Andrew23
                    8 Apr 2021, 12:20

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 8 Apr 2021, 13:24 last edited by
                    #8

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

                    :)

                    1 Reply Last reply
                    0

                    1/8

                    8 Apr 2021, 10:22

                    • Login

                    • Login or register to search.
                    1 out of 8
                    • First post
                      1/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved