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. [Solved] QTSerialPort QByteArray Receive values higher than 127

[Solved] QTSerialPort QByteArray Receive values higher than 127

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 2.1k 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.
  • P Offline
    P Offline
    paulogp
    wrote on last edited by
    #1

    I have a device that outputs 3 signals (values: temperature, speed, frequency) via rs232. The code that I use to capture the signals is the following:
    @if (serial->bytesAvailable() == 3)
    {
    QByteArray buffer;
    buffer = serial->readAll();
    qDebug() << (int)buffer[0] << ":" << (int)buffer[1] << ":" << (int)buffer[2];
    }@

    Example of an output: 65 : 10 : 98.

    The problem is that the values can go much higher than 127 that produces an output of negative values (-1, -60, ..). In Java we can use readIntArray; does anyone knows a way to read the values using QTSerialPort? Thanks in advance!

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      You can obtain a QString from QByteArray and then split on ":".
      After that the first element will be temp, the second speed and other.
      It' easy.

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • P Offline
        P Offline
        paulogp
        wrote on last edited by
        #3

        Thanks for the reply. The ":" are inserted in the qDebug(), unfortunately that doesn't do part of the signal.

        1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote on last edited by
          #4

          Can you post the exactly byte sequence, with first, last and control byte?

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

            Try change it to:
            @qDebug() << quint8(buffer[0]) << ":" << quint8(buffer[1]) << ":" << quint8(buffer[2]);@

            1 Reply Last reply
            0
            • P Offline
              P Offline
              paulogp
              wrote on last edited by
              #6

              Reading the device manual, what I only know is that it sends 3 sequencial "signals" (that corresponds to temperature, speed, frequency) from time to time.

              For example; I receive the 3 sequencial numbers:
              65 (temperature)
              10 (speed)
              98 (frequency)

              10 minutes later:
              256
              3
              128

              23 minutes later:
              250
              100
              192

              and so on...

              Init:
              @serial->setBaudRate(QSerialPort::Baud1200);
              serial->setDataBits(QSerialPort::Data8);
              serial->setParity(QSerialPort::NoParity);
              serial->setStopBits(QSerialPort::OneStop);
              serial->setFlowControl(QSerialPort::NoFlowControl);@

              @if (serial->bytesAvailable() == 3)
              {
              QByteArray buffer;
              buffer = serial->readAll();
              qDebug() << (int)buffer[0] << ":" << (int)buffer[1] << ":" << (int)buffer[2];
              }@

              I have no problem "catching" the values, my problems is with value itself. When (int)buffer[ 0 ] or (int)buffer[ 1 ] or (int)buffer[ 2 ] is higher than 127 it shows negative numbers (68, 5, -1) because byte goes from -127 to 127.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                paulogp
                wrote on last edited by
                #7

                Thanks, I will give it a try qxoz.

                1 Reply Last reply
                0
                • JeroentjehomeJ Offline
                  JeroentjehomeJ Offline
                  Jeroentjehome
                  wrote on last edited by
                  #8

                  Hi, do you need to add up two bytes into a signed int??
                  Maybe something like this:
                  int MyInt ((qint16)buffer[0] << 8 + (qint16)buffer[1]);
                  or if that failed, you could do something like this:
                  @
                  QByteArray MyInt (buffer.chop(2));
                  qDebug() << MyInt.toInt();
                  @

                  Greetz, Jeroen

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    paulogp
                    wrote on last edited by
                    #9

                    Thanks all for the replies. The solution presented by qxoz worked like a charm.

                    @ if (serial->bytesAvailable() == 3)
                    {
                    QByteArray buffer;
                    buffer = serial->readAll();
                    qDebug() << (quint8)buffer[0] << ":" << (quint8)buffer[1] << ":" << (quint8)buffer[2];
                    }
                    @

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      miguelangellv
                      wrote on last edited by
                      #10

                      Other solution is use QDataStream

                      @ if (serial->bytesAvailable() == 3)
                      {
                      QDataStream buffer(serial->readAll(), QIODevice::ReadOnly);
                      quint8 data1;
                      quint8 data2;
                      quint8 data3;

                          buffer >> data1;
                          buffer >> data2;
                          buffer >> data3;
                      }@
                      
                      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