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. How to edit output of QByteArray?
Forum Updated to NodeBB v4.3 + New Features

How to edit output of QByteArray?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 834 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.
  • eefesafakE eefesafak

    Hello everyone,
    QByteArray which is "response" in this case and my function as follows:

    void checkVersion()
    {
        QByteArray response;
        QByteArray command = QByteArray::fromHex("04A30400FF55");
        m_serialPort->write(command);
        m_serialPort->waitForBytesWritten(500);
    
        while(m_serialPort->waitForReadyRead(500))
        {
            response.append(m_serialPort->readAll());
        }
    
        qDebug() << response;
    }
    
    

    When I qDebug() this "response", the output is like this:

    "H\xA4\x00\x00Product Name: Product ID: Hardware version: Firmware version:1.13.41\xE7\xAF"
    

    And my desire output is:

    "Product Name: Product ID: Hardware version: Firmware version:1.13.41"
    

    Which function should I use or which subject should I research?
    Thanks1

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @eefesafak Do you have a protocol for this communication? How do you send the data? Do you use https://doc.qt.io/qt-6/qdatastream.html to send data?
    And didn't you already ask exact same question in the past? https://forum.qt.io/topic/137606/how-to-delete-hex-numbers-from-qbytearray

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    eefesafakE 2 Replies Last reply
    0
    • jsulmJ jsulm

      @eefesafak Do you have a protocol for this communication? How do you send the data? Do you use https://doc.qt.io/qt-6/qdatastream.html to send data?
      And didn't you already ask exact same question in the past? https://forum.qt.io/topic/137606/how-to-delete-hex-numbers-from-qbytearray

      eefesafakE Offline
      eefesafakE Offline
      eefesafak
      wrote on last edited by eefesafak
      #3

      @jsulm Sorry to do that. I totally forgot that I already asked this question.

      1 Reply Last reply
      0
      • jsulmJ jsulm

        @eefesafak Do you have a protocol for this communication? How do you send the data? Do you use https://doc.qt.io/qt-6/qdatastream.html to send data?
        And didn't you already ask exact same question in the past? https://forum.qt.io/topic/137606/how-to-delete-hex-numbers-from-qbytearray

        eefesafakE Offline
        eefesafakE Offline
        eefesafak
        wrote on last edited by eefesafak
        #4

        @jsulm Communication is via QSerialPort.

        jsulmJ 1 Reply Last reply
        0
        • eefesafakE eefesafak

          @jsulm Communication is via QSerialPort.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @eefesafak said in How to edit output of QByteArray?:

          Communication is via QSerialPort.

          This is not what I asked.
          I asked:

          • Do you have a photocall?
          • How exactly do you send data? Do you use QDataStream?

          The question I want to know is: why do you get all these bytes (H\xA4\x00\x00) - you should know that as you send the data, right?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          eefesafakE 1 Reply Last reply
          0
          • eefesafakE eefesafak

            Hello everyone,
            QByteArray which is "response" in this case and my function as follows:

            void checkVersion()
            {
                QByteArray response;
                QByteArray command = QByteArray::fromHex("04A30400FF55");
                m_serialPort->write(command);
                m_serialPort->waitForBytesWritten(500);
            
                while(m_serialPort->waitForReadyRead(500))
                {
                    response.append(m_serialPort->readAll());
                }
            
                qDebug() << response;
            }
            
            

            When I qDebug() this "response", the output is like this:

            "H\xA4\x00\x00Product Name: Product ID: Hardware version: Firmware version:1.13.41\xE7\xAF"
            

            And my desire output is:

            "Product Name: Product ID: Hardware version: Firmware version:1.13.41"
            

            Which function should I use or which subject should I research?
            Thanks1

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @eefesafak so, you only want the printable part:

            QString qBytearrayToCleanedUpString( const QByteArray &data) {
                QString returnStr;
                for(auto d : data){
                    if(QChar c(d); c.isPrint()){
                        returnStr.append(c);
                    }
                }
                return returnStr;
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            eefesafakE 1 Reply Last reply
            1
            • jsulmJ jsulm

              @eefesafak said in How to edit output of QByteArray?:

              Communication is via QSerialPort.

              This is not what I asked.
              I asked:

              • Do you have a photocall?
              • How exactly do you send data? Do you use QDataStream?

              The question I want to know is: why do you get all these bytes (H\xA4\x00\x00) - you should know that as you send the data, right?

              eefesafakE Offline
              eefesafakE Offline
              eefesafak
              wrote on last edited by
              #7

              @jsulm I am working with qr reader. In the document of the Qr company there was a hexadecimal operation instruction that allowed us to check the software version. I sent it to Qr with Serialport. Briefly, it returns the software version.

              jsulmJ 1 Reply Last reply
              0
              • eefesafakE eefesafak

                @jsulm I am working with qr reader. In the document of the Qr company there was a hexadecimal operation instruction that allowed us to check the software version. I sent it to Qr with Serialport. Briefly, it returns the software version.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @eefesafak Apparently it returns more than just the version. Isn't there any documentation describing what exactly is returned? Or how the protocol looks like? Do you know why there are "H\xA4\x00\x00" prefix and "\xE7\xAF" suffix? Is this part of any data package you get?
                You could simply delete these bytes (assuming you always get 4 bytes at the beginning and 2 bytes at the end of the response which you do not need):

                response = response.sliced(0, 4); // remove first 4 bytes
                response = response.truncate(2); // remove last two bytes
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #9

                  @eefesafak The question is about the protocol. Yes, the device sends a string of characters containing a human-readable version, but it also send the other bytes. The device is not sending random response bytes, they probably mean something. There should be documentation about what those other bytes mean, just like the documentation that tells you what bytes to send to make a request. Rather than blindly removing the non-printable bytes you do not understand (which will not remove the leading H) you could try to understand what is in the response and code intelligently.

                  For example, the first byte 'H' is the number 72, which looks roughly like the number of bytes in the remainder of the message.

                  1 Reply Last reply
                  1
                  • J.HilkJ J.Hilk

                    @eefesafak so, you only want the printable part:

                    QString qBytearrayToCleanedUpString( const QByteArray &data) {
                        QString returnStr;
                        for(auto d : data){
                            if(QChar c(d); c.isPrint()){
                                returnStr.append(c);
                            }
                        }
                        return returnStr;
                    }
                    
                    eefesafakE Offline
                    eefesafakE Offline
                    eefesafak
                    wrote on last edited by
                    #10

                    @J-Hilk Could you explain, what does it use for:

                            if(QChar c(d); c.isPrint()){
                                returnStr.append(c);
                            }
                    

                    If you do not mind?

                    J.HilkJ 1 Reply Last reply
                    0
                    • eefesafakE eefesafak

                      @J-Hilk Could you explain, what does it use for:

                              if(QChar c(d); c.isPrint()){
                                  returnStr.append(c);
                              }
                      

                      If you do not mind?

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #11

                      @eefesafak its a shortened for loop over a container, Qt previously had Q_FOREACH for that, but the c++ standard now has this for loop version.

                      In this case it does go through every "byte" of the QByteArray - from beginning to end - takes that char, converts it to a QChar and then checks if its a printable character. If thats true, it appends the QChar to the local QString variable

                      see there:
                      https://en.cppreference.com/w/cpp/language/if

                      for more info about the used "If statements with initializer"


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      eefesafakE 1 Reply Last reply
                      2
                      • J.HilkJ J.Hilk

                        @eefesafak its a shortened for loop over a container, Qt previously had Q_FOREACH for that, but the c++ standard now has this for loop version.

                        In this case it does go through every "byte" of the QByteArray - from beginning to end - takes that char, converts it to a QChar and then checks if its a printable character. If thats true, it appends the QChar to the local QString variable

                        see there:
                        https://en.cppreference.com/w/cpp/language/if

                        for more info about the used "If statements with initializer"

                        eefesafakE Offline
                        eefesafakE Offline
                        eefesafak
                        wrote on last edited by
                        #12

                        @J-Hilk Thank you so much :)

                        1 Reply Last reply
                        1

                        • Login

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