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. Convert serial data with QSerialPort
Qt 6.11 is out! See what's new in the release blog

Convert serial data with QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 475 Views 2 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.
  • arcyA Offline
    arcyA Offline
    arcy
    wrote on last edited by arcy
    #1

    Hi!
    From Arduino I'm sending a struct with two numbers:

    typedef struct packet{
      uint8_t v1;
      uint8_t v2;
    };
      packet pack;
      pack.v1 = v1;
      pack.v2 = v2;
      int x = Serial.write((byte*) &pack, sizeof(pack));
    

    in my Qt app, I'm reading with:

    void Backend::receive()
    {
        QByteArray data = serial->readAll();
        qDebug() << data;
    }
    

    but I wanna read the struct I sent and convert it to an array like [12, 91].
    How can I do it?

    M jsulmJ 2 Replies Last reply
    0
    • arcyA arcy

      Hi!
      From Arduino I'm sending a struct with two numbers:

      typedef struct packet{
        uint8_t v1;
        uint8_t v2;
      };
        packet pack;
        pack.v1 = v1;
        pack.v2 = v2;
        int x = Serial.write((byte*) &pack, sizeof(pack));
      

      in my Qt app, I'm reading with:

      void Backend::receive()
      {
          QByteArray data = serial->readAll();
          qDebug() << data;
      }
      

      but I wanna read the struct I sent and convert it to an array like [12, 91].
      How can I do it?

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @arcy
      Hi,
      Why not send a byte array in the first place.
      Incidentally, you may have byte alignment issue.

      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by Kent-Dorfman
        #3

        as mentioned, avoid structures when sending serial data (or understand your environment well enough to properly use pack() and understand its limitations). manually assemble your fields from the data in the stream....but this opens up a whole field of programming that folks routinely get wrong.

        1. add framing data and checksums to async serial comm messages
        2. don't use time division to parse messages, but always scan and parse the incoming stream as it arrives.
        3. understand that opposite ends of channels may have different endianness so formally specify endianness for your protocol
        4. NEVER transmiit floating point numbers in binary format over serial links

        The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

        1 Reply Last reply
        0
        • arcyA arcy

          Hi!
          From Arduino I'm sending a struct with two numbers:

          typedef struct packet{
            uint8_t v1;
            uint8_t v2;
          };
            packet pack;
            pack.v1 = v1;
            pack.v2 = v2;
            int x = Serial.write((byte*) &pack, sizeof(pack));
          

          in my Qt app, I'm reading with:

          void Backend::receive()
          {
              QByteArray data = serial->readAll();
              qDebug() << data;
          }
          

          but I wanna read the struct I sent and convert it to an array like [12, 91].
          How can I do it?

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

          @arcy If you use Qt on both sides you can use https://doc.qt.io/qt-6/qdatastream.html

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

          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