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. Problem transmitting '0x20' via serial port
Qt 6.11 is out! See what's new in the release blog

Problem transmitting '0x20' via serial port

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 3.5k Views 1 Watching
  • 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 armorex

    Hi. I am developing an application to update the firmware of an embedded device. It has a custom bootloader which requires an encrypted file to be sent via serial. The process works with TeraTerm when i submit a binary file at 9600 baud rate and updates the firmware on the device.

    Problem:

    When I try sending the same file via code through a GUI qt interface, the resulting file isn't the same. I have used a logic analyzer to determine that it is skipping all the 0x20 values, which equal to a space in ASCII. The resulting file that is transmitted is EXACTLY the same number of bytes less as the number of 0x20 values in the original file.

    if(!firmwareFile.open(QIODevice::ReadOnly)){
            qDebug() << "Cannot open file";
        }
    
     QByteArray firmwareData;
     firmwareData = firmwareFile.readAll();
     qDebug()<< portName.write(firmwareData);
     portName.waitForBytesWritten(-1);
    qDebug() <<portName.bytesToWrite();
     portName.close();
    
    

    Serial port parameters:

    setBaudRate(QSerialPort::Baud9600);
    setStopBits(QSerialPort::OneStop);
    setParity(QSerialPort::NoParity);
    setFlowControl(QSerialPort::NoFlowControl);
    setDataBits(QSerialPort::Data8);

    The received resulting file has exactly 104 bytes less. And there are 104 times 0x20 values in the original binary file. I have tried to use QByteArray::toHex as well but that doesn't work too.

    alt text

    CP71C Offline
    CP71C Offline
    CP71
    wrote on last edited by
    #2

    @armorex

    QSerialPort mSerial;
    QByteArray data;

    ...
    mSerial.write( data.data(), data.size() );
    …

    Hi.
    I don't know!
    I've never problem to send data with this code, try it.

    A 2 Replies Last reply
    0
    • A armorex

      Hi. I am developing an application to update the firmware of an embedded device. It has a custom bootloader which requires an encrypted file to be sent via serial. The process works with TeraTerm when i submit a binary file at 9600 baud rate and updates the firmware on the device.

      Problem:

      When I try sending the same file via code through a GUI qt interface, the resulting file isn't the same. I have used a logic analyzer to determine that it is skipping all the 0x20 values, which equal to a space in ASCII. The resulting file that is transmitted is EXACTLY the same number of bytes less as the number of 0x20 values in the original file.

      if(!firmwareFile.open(QIODevice::ReadOnly)){
              qDebug() << "Cannot open file";
          }
      
       QByteArray firmwareData;
       firmwareData = firmwareFile.readAll();
       qDebug()<< portName.write(firmwareData);
       portName.waitForBytesWritten(-1);
      qDebug() <<portName.bytesToWrite();
       portName.close();
      
      

      Serial port parameters:

      setBaudRate(QSerialPort::Baud9600);
      setStopBits(QSerialPort::OneStop);
      setParity(QSerialPort::NoParity);
      setFlowControl(QSerialPort::NoFlowControl);
      setDataBits(QSerialPort::Data8);

      The received resulting file has exactly 104 bytes less. And there are 104 times 0x20 values in the original binary file. I have tried to use QByteArray::toHex as well but that doesn't work too.

      alt text

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @armorex said in Problem transmitting '0x20' via serial port:

      I have tried to use QByteArray::toHex as well but that doesn't work too.

      What exactly do you mean by this? It sounds like you are suggesting QByteArray::toHex() doesn't cope with space character in the bytes??

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by kuzulis
        #4

        You can use any of serial port sniffer to see a real output data flow to the serial port driver. Seems, you do something wrong, or, maybe, there are some noise on a line. You should not use serial communication without of CRC calculation.

        A 1 Reply Last reply
        2
        • CP71C CP71

          @armorex

          QSerialPort mSerial;
          QByteArray data;

          ...
          mSerial.write( data.data(), data.size() );
          …

          Hi.
          I don't know!
          I've never problem to send data with this code, try it.

          A Offline
          A Offline
          armorex
          wrote on last edited by
          #5

          @CP71 Ok i will try it and let you know

          1 Reply Last reply
          1
          • K kuzulis

            You can use any of serial port sniffer to see a real output data flow to the serial port driver. Seems, you do something wrong, or, maybe, there are some noise on a line. You should not use serial communication without of CRC calculation.

            A Offline
            A Offline
            armorex
            wrote on last edited by
            #6

            @kuzulis said in Problem transmitting '0x20' via serial port:

            You can use any of serial port sniffer to see a real output data flow to the serial port driver. Seems, you do something wrong, or, maybe, there are some noise on a line. You should not use serial communication without of CRC calculation.

            I have already included a screenshot of a file that was made by the logic analyzer. Which gets gets the output from a physical serial port. You can see that the top file does not contain 0x20. The bottom (original) file, however does. I have tired it multiple times with the same output which eliminates the possibility of noise

            aha_1980A 1 Reply Last reply
            0
            • A armorex

              @kuzulis said in Problem transmitting '0x20' via serial port:

              You can use any of serial port sniffer to see a real output data flow to the serial port driver. Seems, you do something wrong, or, maybe, there are some noise on a line. You should not use serial communication without of CRC calculation.

              I have already included a screenshot of a file that was made by the logic analyzer. Which gets gets the output from a physical serial port. You can see that the top file does not contain 0x20. The bottom (original) file, however does. I have tired it multiple times with the same output which eliminates the possibility of noise

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Hi @armorex,

              99% surely the problem is in your code.

              But please tell us, which operating system, compiler and Qt version you use.

              I'd try to separate the problem in two parts:

              1. Make sure the file is read correctly. Check it before writing it to the serial port. BTW: How big is this file?
              2. Make sure the serial writing is done correctly. Start by writing a small buffer containing spaces and verify they are sent.

              If I were you, I'd not use waitForBytesWritten. It is unrelyable anyway, as the data is still buffered in the OS or driver level when Qt's buffer get empty. A better handshake is to send ACKs from the bootloader when when (a chunk of/all) data arrived.

              Regards

              Qt has to stay free or it will die.

              A 1 Reply Last reply
              2
              • CP71C CP71

                @armorex

                QSerialPort mSerial;
                QByteArray data;

                ...
                mSerial.write( data.data(), data.size() );
                …

                Hi.
                I don't know!
                I've never problem to send data with this code, try it.

                A Offline
                A Offline
                armorex
                wrote on last edited by
                #8

                @CP71 said in Problem transmitting '0x20' via serial port:

                @armorex

                QSerialPort mSerial;
                QByteArray data;

                ...
                mSerial.write( data.data(), data.size() );
                …

                Hi.
                I don't know!
                I've never problem to send data with this code, try it.

                This worked! But now i don't understand why can't we use QByteArray directly and have to use data(). But at least it works now. Thanks!

                CP71C 1 Reply Last reply
                1
                • aha_1980A aha_1980

                  Hi @armorex,

                  99% surely the problem is in your code.

                  But please tell us, which operating system, compiler and Qt version you use.

                  I'd try to separate the problem in two parts:

                  1. Make sure the file is read correctly. Check it before writing it to the serial port. BTW: How big is this file?
                  2. Make sure the serial writing is done correctly. Start by writing a small buffer containing spaces and verify they are sent.

                  If I were you, I'd not use waitForBytesWritten. It is unrelyable anyway, as the data is still buffered in the OS or driver level when Qt's buffer get empty. A better handshake is to send ACKs from the bootloader when when (a chunk of/all) data arrived.

                  Regards

                  A Offline
                  A Offline
                  armorex
                  wrote on last edited by
                  #9

                  @aha_1980 said in Problem transmitting '0x20' via serial port:

                  Hi @armorex,

                  99% surely the problem is in your code.

                  But please tell us, which operating system, compiler and Qt version you use.

                  I'd try to separate the problem in two parts:

                  1. Make sure the file is read correctly. Check it before writing it to the serial port. BTW: How big is this file?
                  2. Make sure the serial writing is done correctly. Start by writing a small buffer containing spaces and verify they are sent.

                  If I were you, I'd not use waitForBytesWritten. It is unrelyable anyway, as the data is still buffered in the OS or driver level when Qt's buffer get empty. A better handshake is to send ACKs from the bootloader when when (a chunk of/all) data arrived.

                  Regards

                  I had to use the blocking function because it wasn't transmitting all the data. It would stop in the middle. The file is 36349 bytes.

                  1 Reply Last reply
                  0
                  • A armorex

                    @CP71 said in Problem transmitting '0x20' via serial port:

                    @armorex

                    QSerialPort mSerial;
                    QByteArray data;

                    ...
                    mSerial.write( data.data(), data.size() );
                    …

                    Hi.
                    I don't know!
                    I've never problem to send data with this code, try it.

                    This worked! But now i don't understand why can't we use QByteArray directly and have to use data(). But at least it works now. Thanks!

                    CP71C Offline
                    CP71C Offline
                    CP71
                    wrote on last edited by
                    #10

                    @armorex

                    You are welcome!
                    It is old code, so I don’t remember why I have used this way instead of .write( QbyteData ), maybe I have had same result! But I can’t say now because I don’t remember ;)

                    I don’t know how .write() functions work at low level, but QByteData is unicode chars container. This call receives a no unicode chars. Maybe the difference is this.

                    aha_1980A 1 Reply Last reply
                    0
                    • CP71C CP71

                      @armorex

                      You are welcome!
                      It is old code, so I don’t remember why I have used this way instead of .write( QbyteData ), maybe I have had same result! But I can’t say now because I don’t remember ;)

                      I don’t know how .write() functions work at low level, but QByteData is unicode chars container. This call receives a no unicode chars. Maybe the difference is this.

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @CP71

                      I don’t know how .write() functions work at low level, but QByteData is unicode chars container.

                      No, that's wrong. QByteArray just holds Bytes without interpreting them.

                      So I really wonder why using the other write makes a difference.

                      Still the OP didn't answer my questions.

                      Regards

                      Qt has to stay free or it will die.

                      CP71C 1 Reply Last reply
                      2
                      • aha_1980A aha_1980

                        @CP71

                        I don’t know how .write() functions work at low level, but QByteData is unicode chars container.

                        No, that's wrong. QByteArray just holds Bytes without interpreting them.

                        So I really wonder why using the other write makes a difference.

                        Still the OP didn't answer my questions.

                        Regards

                        CP71C Offline
                        CP71C Offline
                        CP71
                        wrote on last edited by
                        #12

                        @aha_1980
                        Yes, sorry!
                        You are right, my mistake.
                        I don’t know why I was thinking at QChar :(

                        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