Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Not able to pass 0x00 over Qserial port
Qt 6.11 is out! See what's new in the release blog

Not able to pass 0x00 over Qserial port

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
26 Posts 4 Posters 4.4k 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.
  • R Ramarao

    @KroMignon Thanks for your response. MSG_EXT_T is structure which i have framed for sending command to other board.

    typedef struct MSG_EXT {
    uint8_t start_byte;
    uint8_t priority;
    uint16_t uniq_id;
    union {
    uint32_t msg_id; /* pre-defined id used by ui board /
    /
    Break-up of msg_id into therapy internal ids /
    struct {
    uint8_t src_id; /
    LSB of msg_id */
    uint8_t dest_id;
    uint8_t t_cmd;
    uint8_t param;
    };
    };
    union {
    uint8_t ack;
    int8_t notify;
    };
    uint8_t ext_msg_type;
    union
    {
    bool val_bool;
    int8_t val_s8;
    int16_t val_s16;
    int32_t val_s32;
    uint8_t val_u8;
    uint16_t val_u16;
    uint32_t val_u32;
    float val_f32;
    DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(config_data, addr);
    DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(config, val);
    DEFINE_MSG_PAYLOAD_WITH_2_U16_ARGS(valve_move, id, unused);
    DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(soft_assert, id);
    DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(rx_msg, addr);
    DEFINE_MSG_PAYLOAD_WITH_2_U16_ARGS(rx_file, size, chunk_size);
    uint8_t payload[4];
    };
    uint8_t crc;
    uint8_t stop_byte;
    } MSG_EXT_T, *MSG_EXT_PTR;

    my code for transmitting command is below.

    char b[sizeof(omsg)];
    memcpy(b,(char*)&omsg,sizeof(omsg));
    QByteArray ba = QByteArray::fromRawData(b,sizeof(b));
    serial->write((const char *)ba,sizeof(ba)-1);
    qDebug() << ba;

    qDebug() output: "U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA"

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

    @Ramarao
    ...And you use raw qDebug() to visualise the content....

    What is the value of sizeof(MSG_EXT)? What is the declaration of omsg?

    And what is your evidence that any byte of 0 is not actually sent??

    And serial->write((const char *)ba,sizeof(ba)-1);: why do you omit sending the last byte?

    1 Reply Last reply
    0
    • R Ramarao

      @KroMignon Thanks for your response. MSG_EXT_T is structure which i have framed for sending command to other board.

      typedef struct MSG_EXT {
      uint8_t start_byte;
      uint8_t priority;
      uint16_t uniq_id;
      union {
      uint32_t msg_id; /* pre-defined id used by ui board /
      /
      Break-up of msg_id into therapy internal ids /
      struct {
      uint8_t src_id; /
      LSB of msg_id */
      uint8_t dest_id;
      uint8_t t_cmd;
      uint8_t param;
      };
      };
      union {
      uint8_t ack;
      int8_t notify;
      };
      uint8_t ext_msg_type;
      union
      {
      bool val_bool;
      int8_t val_s8;
      int16_t val_s16;
      int32_t val_s32;
      uint8_t val_u8;
      uint16_t val_u16;
      uint32_t val_u32;
      float val_f32;
      DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(config_data, addr);
      DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(config, val);
      DEFINE_MSG_PAYLOAD_WITH_2_U16_ARGS(valve_move, id, unused);
      DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(soft_assert, id);
      DEFINE_MSG_PAYLOAD_WITH_1_U32_ARG(rx_msg, addr);
      DEFINE_MSG_PAYLOAD_WITH_2_U16_ARGS(rx_file, size, chunk_size);
      uint8_t payload[4];
      };
      uint8_t crc;
      uint8_t stop_byte;
      } MSG_EXT_T, *MSG_EXT_PTR;

      my code for transmitting command is below.

      char b[sizeof(omsg)];
      memcpy(b,(char*)&omsg,sizeof(omsg));
      QByteArray ba = QByteArray::fromRawData(b,sizeof(b));
      serial->write((const char *)ba,sizeof(ba)-1);
      qDebug() << ba;

      qDebug() output: "U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA"

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #6

      @Ramarao said in Not able to pass 0x00 over Qserial port:

      my code for transmitting command is below.
      char b[sizeof(omsg)];
      memcpy(b,(char*)&omsg,sizeof(omsg));
      QByteArray ba = QByteArray::fromRawData(b,sizeof(b));
      serial->write((const char *)ba,sizeof(ba)-1);
      qDebug() << ba;
      qDebug() output: "U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA"

      Just a side note, try to use button </> to insert the code, so it will be more readable.

      To convert your raw structure to QByteArray, I would do:
      QByteArray ba = QByteArray::fromRawData((char*)&omsg, sizeof(omsg));

      Then to send: serial->write(ba.data(), ba.size());

      EDIT
      A shorter way could be: serial->write((char*)&omsg, sizeof(omsg));

      And why using sizeof(ba)? You don't want to have the size of the QByteArray instance but the size of the data with is ba.size().

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      R 1 Reply Last reply
      2
      • KroMignonK KroMignon

        @Ramarao said in Not able to pass 0x00 over Qserial port:

        my code for transmitting command is below.
        char b[sizeof(omsg)];
        memcpy(b,(char*)&omsg,sizeof(omsg));
        QByteArray ba = QByteArray::fromRawData(b,sizeof(b));
        serial->write((const char *)ba,sizeof(ba)-1);
        qDebug() << ba;
        qDebug() output: "U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA"

        Just a side note, try to use button </> to insert the code, so it will be more readable.

        To convert your raw structure to QByteArray, I would do:
        QByteArray ba = QByteArray::fromRawData((char*)&omsg, sizeof(omsg));

        Then to send: serial->write(ba.data(), ba.size());

        EDIT
        A shorter way could be: serial->write((char*)&omsg, sizeof(omsg));

        And why using sizeof(ba)? You don't want to have the size of the QByteArray instance but the size of the data with is ba.size().

        R Offline
        R Offline
        Ramarao
        wrote on last edited by
        #7

        Thanks for the suggestion. i am getting below result after trying this.
        code_text

        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        ""                                                                              
        "?"
        KroMignonK 1 Reply Last reply
        0
        • R Ramarao

          Thanks for the suggestion. i am getting below result after trying this.
          code_text

          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          ""                                                                              
          "?"
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #8

          @Ramarao said in Not able to pass 0x00 over Qserial port:

          i am getting below result after trying this.

          After trying what exactly?

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Ramarao
            wrote on last edited by Ramarao
            #9

            @KroMignon after trying below code, i got "U" as first character and rest are like above.
            code_text

            QByteArray ba = QByteArray::fromRawData((char *)&omsg,sizeof(omsg));
            serial->write(ba.data(),ba.size());

            KroMignonK 1 Reply Last reply
            0
            • R Ramarao

              @KroMignon after trying below code, i got "U" as first character and rest are like above.
              code_text

              QByteArray ba = QByteArray::fromRawData((char *)&omsg,sizeof(omsg));
              serial->write(ba.data(),ba.size());

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #10

              @Ramarao said in Not able to pass 0x00 over Qserial port:

              after trying below code, i got "U" as first character and rest are like above.

              Which seems to be normal to me 0x55 is U is ASCII table.
              But where to you see this?
              You are only sending data with this code extract.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              R 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @Ramarao said in Not able to pass 0x00 over Qserial port:

                after trying below code, i got "U" as first character and rest are like above.

                Which seems to be normal to me 0x55 is U is ASCII table.
                But where to you see this?
                You are only sending data with this code extract.

                R Offline
                R Offline
                Ramarao
                wrote on last edited by
                #11

                @KroMignon i am sending this command to AM335x and observing the data in microcom or other QT receiving application running on board..Am335x_terminal.png

                KroMignonK 1 Reply Last reply
                0
                • R Ramarao

                  @KroMignon i am sending this command to AM335x and observing the data in microcom or other QT receiving application running on board..Am335x_terminal.png

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #12

                  @Ramarao said in Not able to pass 0x00 over Qserial port:

                  i am sending this command to AM335x and observing the data in microcom or other QT receiving application running on board..

                  You should be aware that '\0' (NULL character) are not printable, so you cannot see it with microcom.
                  You have to use an hexadecimal terminal, something like ssterm (https://github.com/vsergeev/ssterm)

                  ssterm -o hex /dev/ttyXXX
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  1
                  • R Offline
                    R Offline
                    Ramarao
                    wrote on last edited by Ramarao
                    #13

                    @KroMignon iam running QT receiving app also at board side to observe data.

                    code_text
                    QByteArray data = serial->readAll();
                       QString str = QString(data);
                       qDebug() << str;
                    

                    My doubt is serial->write() will not send 0's by treating it as null character if we send in any format.

                    JonBJ KroMignonK 2 Replies Last reply
                    0
                    • R Ramarao

                      @KroMignon iam running QT receiving app also at board side to observe data.

                      code_text
                      QByteArray data = serial->readAll();
                         QString str = QString(data);
                         qDebug() << str;
                      

                      My doubt is serial->write() will not send 0's by treating it as null character if we send in any format.

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

                      @Ramarao
                      I know you have ignored every post I have made, but I did tell you:

                         QString str = QString(data);
                         qDebug() << str;
                      

                      Using qDebug() like this is not the way to be able to see the data. Particularly 0x00s....

                      And additionally did you read QString::QString(const QByteArray &ba):

                      Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromUtf8(). Stops copying at the first 0 character, otherwise copies the entire byte array.

                      [My bold.]

                      R 1 Reply Last reply
                      2
                      • R Ramarao

                        @KroMignon iam running QT receiving app also at board side to observe data.

                        code_text
                        QByteArray data = serial->readAll();
                           QString str = QString(data);
                           qDebug() << str;
                        

                        My doubt is serial->write() will not send 0's by treating it as null character if we send in any format.

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by KroMignon
                        #15

                        @Ramarao said in Not able to pass 0x00 over Qserial port:

                        iam running QT receiving app also at board side to observe data.
                        code_text
                        QByteArray data = serial->readAll();
                        QString str = QString(data);
                        qDebug() << str;

                        And this wrong, you want to see hexadecimal data:

                        QByteArray data = serial->readAll();
                        
                        qDebug() << "Raw data:" << data;
                        qDebug() << "Hex data:"<< data.toHex(':');
                        
                        

                        My doubt is serial->write() will not send 0's by treating it as null character if we send in any format.

                        QSerialPort::write(const char * buffer, int size) do not check null byte, what matter is size. You can send as many null bytes you want, there is no interpretation about buffer content.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        1 Reply Last reply
                        3
                        • JonBJ JonB

                          @Ramarao
                          I know you have ignored every post I have made, but I did tell you:

                             QString str = QString(data);
                             qDebug() << str;
                          

                          Using qDebug() like this is not the way to be able to see the data. Particularly 0x00s....

                          And additionally did you read QString::QString(const QByteArray &ba):

                          Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromUtf8(). Stops copying at the first 0 character, otherwise copies the entire byte array.

                          [My bold.]

                          R Offline
                          R Offline
                          Ramarao
                          wrote on last edited by
                          #16

                          @JonB sorry that as i am new guy, forum is not allowing me immediately(making me to wait 10 mins). I am new to QT and i am trying to send 16 bytes command to AM335x board from QT application(running in PC) with button press for time being.
                          My goal is to send and receive data over serial communication between AM335x and STM32 where AM335x acting as GUI interface with QT application.

                          JonBJ 1 Reply Last reply
                          0
                          • R Ramarao

                            @JonB sorry that as i am new guy, forum is not allowing me immediately(making me to wait 10 mins). I am new to QT and i am trying to send 16 bytes command to AM335x board from QT application(running in PC) with button press for time being.
                            My goal is to send and receive data over serial communication between AM335x and STM32 where AM335x acting as GUI interface with QT application.

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

                            @Ramarao
                            That's OK, welcome :)

                            If you read what @KroMignon & I have said between us you ought be good to go!

                            Truly there is no problem sending 0 bytes across serial. Your problems will be (a) if you ever convert a QByteArray to a QString, because it's liable to terminate at the 0, as I wrote earlier, and (b) if you're not careful about interpreting what you see from qDebug(), because it shows "odd" bytes in ways you might misinterpret. Using @KroMignon's QByteArray::toHex() at least is one way to visualise the bytes without having them "terminated early" or being shown "oddly".

                            1 Reply Last reply
                            1
                            • R Offline
                              R Offline
                              Ramarao
                              wrote on last edited by
                              #18

                              This issue is resolved.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                manel.sam
                                wrote on last edited by
                                #19

                                This my open for opening IMU
                                Imu::Imu() :
                                moving(false)
                                {

                                serialPort = new QSerialPort("COM7",this);
                                if (!serialPort->open( QIODevice::ReadOnly))
                                {
                                    log_warning("imu","failed to open device file \"%s\", IMU measures will be unavailable",qPrintable(serialPort->portName()));
                                    return;
                                }
                                
                                if (!serialPort->setBaudRate(115200))
                                    log_error("imu","failed to set baudrate, error no %d",serialPort->error());
                                serialPort->setDataBits(QSerialPort::Data8);
                                serialPort->setParity(QSerialPort::NoParity);
                                serialPort->setStopBits(QSerialPort::OneStop); // One Stop bit
                                serialPort->setFlowControl(QSerialPort::NoFlowControl);
                                //serialPort->open(QIODevice::ReadOnly);
                                
                                pollingTimer = new QTimer(this);
                                QObject::connect(pollingTimer, SIGNAL(timeout()), this, SLOT(pollSerialPort()));
                                pollingTimer->start(10);
                                
                                }
                                

                                Imu::~Imu()
                                {
                                serialPort->close();
                                }

                                void Imu::pollSerialPort()
                                {
                                static const unsigned char START_BYTES[2] = {0x55,0xAA};
                                static const QByteArray START_WORD((char*)START_BYTES,2);

                                static QTime startTime = QTime::currentTime();
                                
                                static QByteArray data;
                                data.append(serialPort->readAll());
                                qDebug() <<"Raw data"<<data.append(serialPort->readAll());
                                QByteArray hex = data.append(serialPort->readAll()).toHex('0');   // returns "123456abcdef"
                                qDebug() <<"Hex data"<<hex;
                                

                                I would like to get the data in hexadecimal, however I could not get anything

                                Neither in binary data, nor in hexadecimal

                                JonBJ 1 Reply Last reply
                                0
                                • M manel.sam

                                  This my open for opening IMU
                                  Imu::Imu() :
                                  moving(false)
                                  {

                                  serialPort = new QSerialPort("COM7",this);
                                  if (!serialPort->open( QIODevice::ReadOnly))
                                  {
                                      log_warning("imu","failed to open device file \"%s\", IMU measures will be unavailable",qPrintable(serialPort->portName()));
                                      return;
                                  }
                                  
                                  if (!serialPort->setBaudRate(115200))
                                      log_error("imu","failed to set baudrate, error no %d",serialPort->error());
                                  serialPort->setDataBits(QSerialPort::Data8);
                                  serialPort->setParity(QSerialPort::NoParity);
                                  serialPort->setStopBits(QSerialPort::OneStop); // One Stop bit
                                  serialPort->setFlowControl(QSerialPort::NoFlowControl);
                                  //serialPort->open(QIODevice::ReadOnly);
                                  
                                  pollingTimer = new QTimer(this);
                                  QObject::connect(pollingTimer, SIGNAL(timeout()), this, SLOT(pollSerialPort()));
                                  pollingTimer->start(10);
                                  
                                  }
                                  

                                  Imu::~Imu()
                                  {
                                  serialPort->close();
                                  }

                                  void Imu::pollSerialPort()
                                  {
                                  static const unsigned char START_BYTES[2] = {0x55,0xAA};
                                  static const QByteArray START_WORD((char*)START_BYTES,2);

                                  static QTime startTime = QTime::currentTime();
                                  
                                  static QByteArray data;
                                  data.append(serialPort->readAll());
                                  qDebug() <<"Raw data"<<data.append(serialPort->readAll());
                                  QByteArray hex = data.append(serialPort->readAll()).toHex('0');   // returns "123456abcdef"
                                  qDebug() <<"Hex data"<<hex;
                                  

                                  I would like to get the data in hexadecimal, however I could not get anything

                                  Neither in binary data, nor in hexadecimal

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

                                  @manel-sam
                                  Hi. It might have been better to open your own topic for this than putting it in this existing thread. You could still do that.

                                  You are calling serialPort->readAll() 3 times, including in a qDebug() statement. Each time you call it all the data is read, it will not be there for next read.

                                  Wouldn't using readyRead() signal be better than your timed polling?

                                  M 1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @manel-sam
                                    Hi. It might have been better to open your own topic for this than putting it in this existing thread. You could still do that.

                                    You are calling serialPort->readAll() 3 times, including in a qDebug() statement. Each time you call it all the data is read, it will not be there for next read.

                                    Wouldn't using readyRead() signal be better than your timed polling?

                                    M Offline
                                    M Offline
                                    manel.sam
                                    wrote on last edited by
                                    #21

                                    @JonB Thank you for your feedback, I am new to this forum and QT too, I will do so indeed

                                    Thanks

                                    Can you tell me then how I can read the data without using the qDebug(), to be able to visualize if I receive the data well

                                    and how to get the data in Hexadecimal

                                    JonBJ 1 Reply Last reply
                                    0
                                    • M manel.sam

                                      @JonB Thank you for your feedback, I am new to this forum and QT too, I will do so indeed

                                      Thanks

                                      Can you tell me then how I can read the data without using the qDebug(), to be able to visualize if I receive the data well

                                      and how to get the data in Hexadecimal

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

                                      @manel-sam

                                      QByteArray ba = serialPort->readAll();
                                      qDebug() << ba.count() << ba.toHex()
                                      
                                      M 1 Reply Last reply
                                      1
                                      • JonBJ JonB

                                        @manel-sam

                                        QByteArray ba = serialPort->readAll();
                                        qDebug() << ba.count() << ba.toHex()
                                        
                                        M Offline
                                        M Offline
                                        manel.sam
                                        wrote on last edited by
                                        #23

                                        @JonB
                                        Thanks a lot,

                                        I receive only the value 0, whereas the Imu I use receives the values in Hexadecimal.
                                        Is it necessary to write other commands?

                                        JonBJ 1 Reply Last reply
                                        0
                                        • M manel.sam

                                          @JonB
                                          Thanks a lot,

                                          I receive only the value 0, whereas the Imu I use receives the values in Hexadecimal.
                                          Is it necessary to write other commands?

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

                                          @manel-sam said in Not able to pass 0x00 over Qserial port:

                                          whereas the Imu I use receives the values in Hexadecimal.

                                          No, it does not. Forget about hexadecimal. You are receiving 0 bytes.

                                          M 1 Reply Last reply
                                          0

                                          • Login

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