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. I will send data via serialport
Forum Updated to NodeBB v4.3 + New Features

I will send data via serialport

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 802 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.
  • K KARMA

    I will send 3 bytes of data via serialport.
    my variables;
    uint8_t x;
    uint16_t y;

    After giving values to these variables;
    QByteArray sendData;
    sendData[0] = x;
    sendData[1] = y;
    serial->write(sendData);
    Do I have to write the code like this? Would you help me with this topic ?

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

    @KARMA said in I will send data via serialport:

    Do I have to write the code like this?

    No. Rather like this:

    QByteArray sendData;
    sendData[0] = x;
    sendData.setNum(y);
    serial->write(sendData);
    

    Also, why not simply try what you have and see whether it works?

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

    JonBJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @KARMA said in I will send data via serialport:

      Do I have to write the code like this?

      No. Rather like this:

      QByteArray sendData;
      sendData[0] = x;
      sendData.setNum(y);
      serial->write(sendData);
      

      Also, why not simply try what you have and see whether it works?

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

      @jsulm said in I will send data via serialport:

      sendData.setNum(y);

      You sure?? IIRC, QByteArray::setNum()

      Sets the byte array to the printed value

      Printed value means string. That's not what the OP means by "3 bytes", I presume he means he wants the binary 2 bytes for the uint16_t?

      jsulmJ 1 Reply Last reply
      0
      • JonBJ JonB

        @jsulm said in I will send data via serialport:

        sendData.setNum(y);

        You sure?? IIRC, QByteArray::setNum()

        Sets the byte array to the printed value

        Printed value means string. That's not what the OP means by "3 bytes", I presume he means he wants the binary 2 bytes for the uint16_t?

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

        @JonB Oh, you're right.
        sendData.append() should do.

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

        JonBJ 1 Reply Last reply
        1
        • jsulmJ jsulm

          @JonB Oh, you're right.
          sendData.append() should do.

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

          @jsulm
          I don't think so, as there is no QByteArray::append() which accepts a number/uint16_t as a parameter? ;-)
          Are you meaning something like:

          QByteArray::append(&y, sizeof(y))
          

          with whatever casting required for &y -> const char *?

          1 Reply Last reply
          0
          • K KARMA

            I will send 3 bytes of data via serialport.
            my variables;
            uint8_t x;
            uint16_t y;

            After giving values to these variables;
            QByteArray sendData;
            sendData[0] = x;
            sendData[1] = y;
            serial->write(sendData);
            Do I have to write the code like this? Would you help me with this topic ?

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

            @KARMA said in I will send data via serialport:

            After giving values to these variables;
            QByteArray sendData;
            sendData[0] = x;
            sendData[1] = y;
            serial->write(sendData);
            Do I have to write the code like this? Would you help me with this topic ?

            I would suggest you to use QDataStream:

            QByteArray sendData;
            QDataStream stream(&senData, QIODevice:WriteOnly);
            stream << x;
            stream << y;
            serial->write(sendData);
            

            EDIT: use QIODevice:WriteOnly!

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

            JonBJ 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @KARMA said in I will send data via serialport:

              After giving values to these variables;
              QByteArray sendData;
              sendData[0] = x;
              sendData[1] = y;
              serial->write(sendData);
              Do I have to write the code like this? Would you help me with this topic ?

              I would suggest you to use QDataStream:

              QByteArray sendData;
              QDataStream stream(&senData, QIODevice:WriteOnly);
              stream << x;
              stream << y;
              serial->write(sendData);
              

              EDIT: use QIODevice:WriteOnly!

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

              @KroMignon
              This looks better to me, assuming QDataStream does not put any extra "information" for a uint16_t, which I can never recall whether it does or not. However, does QDataStream write any kind of header into the stream, e.g. to convey version number or anything? Or will your code produce just the only 3 bytes wanted by the OP?

              Separately, since you are writing data to stream why is it opened QIODevice::ReadOnly?

              KroMignonK 1 Reply Last reply
              0
              • JonBJ JonB

                @KroMignon
                This looks better to me, assuming QDataStream does not put any extra "information" for a uint16_t, which I can never recall whether it does or not. However, does QDataStream write any kind of header into the stream, e.g. to convey version number or anything? Or will your code produce just the only 3 bytes wanted by the OP?

                Separately, since you are writing data to stream why is it opened QIODevice::ReadOnly?

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

                @JonB

                This looks better to me, assuming QDataStream does not put any extra "information" for a uint16_t, which I can never recall whether it does or not

                There are no extra informations added, I use QDataStream in many softwares.

                Separately, since you are writing data to stream why is it opened QIODevice::ReadOnly?

                It was a mistake, I change it to QIODevice::WriteOnly

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

                JonBJ 1 Reply Last reply
                1
                • KroMignonK KroMignon

                  @JonB

                  This looks better to me, assuming QDataStream does not put any extra "information" for a uint16_t, which I can never recall whether it does or not

                  There are no extra informations added, I use QDataStream in many softwares.

                  Separately, since you are writing data to stream why is it opened QIODevice::ReadOnly?

                  It was a mistake, I change it to QIODevice::WriteOnly

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

                  @KroMignon said in I will send data via serialport:

                  There are no extra informations added

                  Thanks, I have now read through https://doc.qt.io/Qt-5/qdatastream.html#versioning and understand reader and writer need to use same version (to be sure) but QDataStream does not put the version into the data stream.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KARMA
                    wrote on last edited by KARMA
                    #10

                    @KroMignon said in I will send data via serialport:

                    I would suggest you to use QDataStream:

                    QByteArray sendData;
                    QDataStream stream(&senData, QIODevice:WriteOnly);
                    stream << x;
                    stream << y;
                    serial->write(sendData);
                    

                    EDIT: use QIODevice:WriteOnly!

                    I am sorry for the delay. I wasn't here for a while.
                    thanks for this suggestion when i made my code like this i was able to send data properly.

                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • K KARMA

                      @KroMignon said in I will send data via serialport:

                      I would suggest you to use QDataStream:

                      QByteArray sendData;
                      QDataStream stream(&senData, QIODevice:WriteOnly);
                      stream << x;
                      stream << y;
                      serial->write(sendData);
                      

                      EDIT: use QIODevice:WriteOnly!

                      I am sorry for the delay. I wasn't here for a while.
                      thanks for this suggestion when i made my code like this i was able to send data properly.

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #11

                      @KARMA said in I will send data via serialport:

                      i was able to send data properly

                      Great. So if your issue is solved please don't forget to mark your post as such!

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      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