[Solved] QTSerialPort QByteArray Receive values higher than 127



  • 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!



  • 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.



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



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



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



  • 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.



  • Thanks, I will give it a try qxoz.



  • 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();
    @



  • 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];
    }
    @



  • 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;
    }@

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.