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 could I send an array to serial port?
Forum Updated to NodeBB v4.3 + New Features

How could I send an array to serial port?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.0k 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.
  • M Offline
    M Offline
    manum
    wrote on 20 Dec 2018, 21:23 last edited by
    #1

    Hi,

    I need to send this array

    test[121]={33, 20, 23, 20, 27, 27, 25, 29, 14, 12, 10, 74, 24, 29, 44, 57, 63, 62, 61, 42, 30, 30, 185, 178, 93, 50, 68, 74, 76, 69, 59, 36, 38, 255, 255, 115, 56, 73, 89, 91, 78, 64, 41, 45, 255, 248, 88, 61, 76, 105, 109, 89, 69, 45, 72, 255, 245, 85, 63, 76, 103, 119, 91, 67, 47, 65, 255, 252, 100, 60, 72, 94, 107, 83, 62, 39, 54, 255, 255, 134, 52, 67, 78, 84, 72, 56, 28, 56, 255, 255, 205, 58, 59, 66, 66, 61, 44, 23, 60, 255, 255, 255, 123, 46, 60, 61, 56, 37, 39, 60, 238, 241, 249, 176, 52, 47, 52, 47, 85, 93, 91};

    To Serial Port, however it is sending:

    !\x14\x17\x14\x1B\x1B\x19\x1D\x0E\f\nJ\x18\x1D,9?>=*\x1E\x1E\xB9\xB2]2DJLE;$&\xFF\xFFs8IY[N@)-\xFF\xF8X=LimYE-H\xFF\xF5U?Lgw[C/A\xFF\xFC""d<H^kS>'6\xFF\xFF\x86""4CNTH8\x1C""8\xFF\xFF\xCD:;BB=,\x17<\xFF\xFF\xFF{.<=8%'<\xEE\xF1\xF9\xB0""4/4/U][

    There is a way to send integer number to serial port?

    Thanks

    G 1 Reply Last reply 20 Dec 2018, 21:28
    0
    • M manum
      20 Dec 2018, 21:23

      Hi,

      I need to send this array

      test[121]={33, 20, 23, 20, 27, 27, 25, 29, 14, 12, 10, 74, 24, 29, 44, 57, 63, 62, 61, 42, 30, 30, 185, 178, 93, 50, 68, 74, 76, 69, 59, 36, 38, 255, 255, 115, 56, 73, 89, 91, 78, 64, 41, 45, 255, 248, 88, 61, 76, 105, 109, 89, 69, 45, 72, 255, 245, 85, 63, 76, 103, 119, 91, 67, 47, 65, 255, 252, 100, 60, 72, 94, 107, 83, 62, 39, 54, 255, 255, 134, 52, 67, 78, 84, 72, 56, 28, 56, 255, 255, 205, 58, 59, 66, 66, 61, 44, 23, 60, 255, 255, 255, 123, 46, 60, 61, 56, 37, 39, 60, 238, 241, 249, 176, 52, 47, 52, 47, 85, 93, 91};

      To Serial Port, however it is sending:

      !\x14\x17\x14\x1B\x1B\x19\x1D\x0E\f\nJ\x18\x1D,9?>=*\x1E\x1E\xB9\xB2]2DJLE;$&\xFF\xFFs8IY[N@)-\xFF\xF8X=LimYE-H\xFF\xF5U?Lgw[C/A\xFF\xFC""d<H^kS>'6\xFF\xFF\x86""4CNTH8\x1C""8\xFF\xFF\xCD:;BB=,\x17<\xFF\xFF\xFF{.<=8%'<\xEE\xF1\xF9\xB0""4/4/U][

      There is a way to send integer number to serial port?

      Thanks

      G Offline
      G Offline
      Gojir4
      wrote on 20 Dec 2018, 21:28 last edited by Gojir4
      #2

      Hi @manum,

      Actualy that's what you are doing here. But it seems you are reading them as some hexadecimal string format. I guess you are displaying the content of a QByteArray with qDebug() here. What you can do is using data() to get a char * and read the data as char * or casted to unsigned char *.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 20 Dec 2018, 21:30 last edited by
        #3

        Hi and welcome to devnet,

        How are you currently sending the data ?
        What is on the receiving end ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          manum
          wrote on 20 Dec 2018, 21:50 last edited by
          #4

          Hi again,

          My code is

          QByteArray c(test, 121);
          serial2.write(c);

          I send to serial port, the receiving is an arduino, the code is:
          byte patron[121];
          for(int x=1; x<122; x++){
          patron[x-1]=pattern[x];
          }

          And then the qt code receive the comunication from arduino

          QByteArray buffer= serial2.readAll();

          qDebug() << "resultado= "<<buffer;

          The issue is that when I check the array test with the following code:

          qDebug() <<"c= "<< c;

          Qt print :

          !\x14\x17\x14\x1B\x1B\x19\x1D\x0E\f\nJ\x18\x1D,9?>=*\x1E\x1E\xB9\xB2]2DJLE;$&\xFF\xFFs8IY[N@)-\xFF\xF8X=LimYE-H\xFF\xF5U?Lgw[C/A\xFF\xFC""d<H^kS>'6\xFF\xFF\x86""4CNTH8\x1C""8\xFF\xFF\xCD:;BB=,\x17<\xFF\xFF\xFF{.<=8%'<\xEE\xF1\xF9\xB0""4/4/U][

          Instead of

          test[121]={33, 20, 23, 20, 27, 27, 25, 29, 14, 12, 10, 74, 24, 29, 44, 57, 63, 62, 61, 42, 30, 30, 185, 178, 93, 50, 68, 74, 76, 69, 59, 36, 38, 255, 255, 115, 56, 73, 89, 91, 78, 64, 41, 45, 255, 248, 88, 61, 76, 105, 109, 89, 69, 45, 72, 255, 245, 85, 63, 76, 103, 119, 91, 67, 47, 65, 255, 252, 100, 60, 72, 94, 107, 83, 62, 39, 54, 255, 255, 134, 52, 67, 78, 84, 72, 56, 28, 56, 255, 255, 205, 58, 59, 66, 66, 61, 44, 23, 60, 255, 255, 255, 123, 46, 60, 61, 56, 37, 39, 60, 238, 241, 249, 176, 52, 47, 52, 47, 85, 93, 91};

          If Arduino doesn't receive the array as integer, it doesn't work.

          Maybe it is clearer now.

          Thanks in advanced

          J 1 Reply Last reply 20 Dec 2018, 22:37
          0
          • M manum
            20 Dec 2018, 21:50

            Hi again,

            My code is

            QByteArray c(test, 121);
            serial2.write(c);

            I send to serial port, the receiving is an arduino, the code is:
            byte patron[121];
            for(int x=1; x<122; x++){
            patron[x-1]=pattern[x];
            }

            And then the qt code receive the comunication from arduino

            QByteArray buffer= serial2.readAll();

            qDebug() << "resultado= "<<buffer;

            The issue is that when I check the array test with the following code:

            qDebug() <<"c= "<< c;

            Qt print :

            !\x14\x17\x14\x1B\x1B\x19\x1D\x0E\f\nJ\x18\x1D,9?>=*\x1E\x1E\xB9\xB2]2DJLE;$&\xFF\xFFs8IY[N@)-\xFF\xF8X=LimYE-H\xFF\xF5U?Lgw[C/A\xFF\xFC""d<H^kS>'6\xFF\xFF\x86""4CNTH8\x1C""8\xFF\xFF\xCD:;BB=,\x17<\xFF\xFF\xFF{.<=8%'<\xEE\xF1\xF9\xB0""4/4/U][

            Instead of

            test[121]={33, 20, 23, 20, 27, 27, 25, 29, 14, 12, 10, 74, 24, 29, 44, 57, 63, 62, 61, 42, 30, 30, 185, 178, 93, 50, 68, 74, 76, 69, 59, 36, 38, 255, 255, 115, 56, 73, 89, 91, 78, 64, 41, 45, 255, 248, 88, 61, 76, 105, 109, 89, 69, 45, 72, 255, 245, 85, 63, 76, 103, 119, 91, 67, 47, 65, 255, 252, 100, 60, 72, 94, 107, 83, 62, 39, 54, 255, 255, 134, 52, 67, 78, 84, 72, 56, 28, 56, 255, 255, 205, 58, 59, 66, 66, 61, 44, 23, 60, 255, 255, 255, 123, 46, 60, 61, 56, 37, 39, 60, 238, 241, 249, 176, 52, 47, 52, 47, 85, 93, 91};

            If Arduino doesn't receive the array as integer, it doesn't work.

            Maybe it is clearer now.

            Thanks in advanced

            J Online
            J Online
            JonB
            wrote on 20 Dec 2018, 22:37 last edited by JonB
            #5

            @manum
            qDebug() displays the content of QByteArray as string/printable characters. Characters outside normal printable will be displayed in \xDD format. Bearing that in mind, the output you show does match byte-for-byte with what you have in your test[] array, e.g. from the start 33, 20, 23 shows as !\x14\x17. So it is sending what you give it, you are just not understanding how qDebug() is showing the bytes.

            Now, whether what you are doing at the receiving end with the stream is correct/appropriate I cannot say.

            M 1 Reply Last reply 20 Dec 2018, 22:41
            2
            • J JonB
              20 Dec 2018, 22:37

              @manum
              qDebug() displays the content of QByteArray as string/printable characters. Characters outside normal printable will be displayed in \xDD format. Bearing that in mind, the output you show does match byte-for-byte with what you have in your test[] array, e.g. from the start 33, 20, 23 shows as !\x14\x17. So it is sending what you give it, you are just not understanding how qDebug() is showing the bytes.

              Now, whether what you are doing at the receiving end with the stream is correct/appropriate I cannot say.

              M Offline
              M Offline
              manum
              wrote on 20 Dec 2018, 22:41 last edited by
              #6

              @JonB Thanks, so how could I print "test"?

              J 1 Reply Last reply 20 Dec 2018, 22:47
              0
              • M manum
                20 Dec 2018, 22:41

                @JonB Thanks, so how could I print "test"?

                J Online
                J Online
                JonB
                wrote on 20 Dec 2018, 22:47 last edited by JonB
                #7

                @manum
                Well, you are already printing it! Just I think you don't like the output format.

                I think you mean to see it more like your 33, 20, 23, 20, 27, ... input. So you want a decimal-number-string output for each byte received. I can't test, but I think something like http://doc.qt.io/qt-5/qstring.html#number-1 will do it, so call QString::number(b) for each b byte in the QByteArray?

                1 Reply Last reply
                2
                • G Offline
                  G Offline
                  Gojir4
                  wrote on 21 Dec 2018, 08:01 last edited by
                  #8

                  @manum

                      QString output;
                      for(int i = 0; i < c.size(); i++){
                          output += QString::number(c.at(i) & 0xFF);
                          if(i < (c.size() - 1))
                              output += ",";
                      }
                      qDebug() << output;
                  
                  1 Reply Last reply
                  3

                  1/8

                  20 Dec 2018, 21:23

                  • Login

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