Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved] QTSerialPort QByteArray Receive values higher than 127

    General and Desktop
    5
    10
    1745
    Loading More Posts
    • 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
      paulogp last edited by

      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 Reply Quote 0
      • mrdebug
        mrdebug last edited by

        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 Reply Quote 0
        • P
          paulogp last edited by

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

          1 Reply Last reply Reply Quote 0
          • mrdebug
            mrdebug last edited by

            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 Reply Quote 0
            • Q
              qxoz last edited by

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

              1 Reply Last reply Reply Quote 0
              • P
                paulogp last edited by

                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 Reply Quote 0
                • P
                  paulogp last edited by

                  Thanks, I will give it a try qxoz.

                  1 Reply Last reply Reply Quote 0
                  • Jeroentjehome
                    Jeroentjehome last edited by

                    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 Reply Quote 0
                    • P
                      paulogp last edited by

                      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 Reply Quote 0
                      • M
                        miguelangellv last edited by

                        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 Reply Quote 0
                        • First post
                          Last post