0

I am using readAll() method of QSerialPort class to read data from the virtual COM port.

I am getting two response from my hardware board.
1. Acknowledgement ("10") string
2. Query Data (";1395994881;1.0.0") string data
as a QByteArray : 10\x00;1395994881;1.0.0

I want to remove \x00 character from my QByteArray.

Whenever I am trying to perform any operation on my QByteArray, I will perform on first part of the QByteArray and that is ``10 . Remaining part is skipped that is ;1395994881;1.0.0.

Please guide me if there is any way to remove this \x00 character from QByteArray.

Code
QByteArray data = serial.readAll(); //("10\x00;1395994881;1.0.0");

  1. Try out: QList<QByteArray> list = data.split('\x00'); qDebug()<<list; // 10

  2. Try out: qDebug()<<data.remove(2, 1); //10

  3. Try out: qDebug()<<QString(data); //10

  4. Try out: qDebug()<<data.replace('\x00', ""); //10

  5. Try out: Used Loop to go byte by byte, but same result

I want resulted string as a single string or split, no matter. But need full string something like 10;1395994881;1.0.0 or 10 and ;1395994881;1.0.0.

I have also referred below Stack Overflow questions.
1. QByteArray to QString
2. Remove null from string:Java
3. Code Project

EDIT

//Function to read data from serial port  
void ComPortThread::readData()
{
    if(m_isComPortOpen) {
        //Receive data from serial port
        m_serial->waitForReadyRead(500); //Wait 0.5 sec
        QByteArray data = m_serial->readAll();
        if(!data.isEmpty()) {
            qDebug()<<"Data: "<<data;  //10\x00;1395994881;1.0.0
            //TODO: Separate out two responce like "10" and ";1395994881;1.0.0"
            //Problem : Unable to split data into two parts.
            emit readyToRead(data);
        }
    }
}

Please help me on this.

vahancho
  • 20,808
  • 3
  • 47
  • 55
AB Bolim
  • 1,997
  • 2
  • 23
  • 47
  • Please post [mcve]. Init the `data` with the bytes you got from the serial (instead of `serial.readAll()`), so we could compile the code, run a program and reproduce the issues you described above. – 273K Mar 08 '18 at 07:58
  • My hardware board writes the data on virtual com port(like COM3) and From my Qt App, I am reading that data using `readAll()` method of `QSerialPort` class. When I print `data` using `qDebug()` its display as expected with `\x00` character. – AB Bolim Mar 08 '18 at 08:05
  • If it is displayed like `\00` then it is not the `\x00` character, it is three chars `\ 0 0`. – 273K Mar 08 '18 at 08:08
  • I believe these are two strings combined: `10\x00;1395994881;1.0.0`. As byte array stores '\0'-terminted strings, writing `QByteArray ba("10\x00;1395994881;1.0.0");` will give you just '10'. I don't know how did you create a byte array you mentioned. – vahancho Mar 08 '18 at 08:09
  • @vahancho I dint create `QByteArray`, I just read data from serial port. Please refer `EDIT` section for more idea. – AB Bolim Mar 08 '18 at 08:12
  • Again, it is your work to provide [mcve] if you wish us to help you. The memory dump of the array would be helpful too. – 273K Mar 08 '18 at 08:12
  • @S.M. U cant create that `QByteArray` grammatically, and How can I provide my hardware board o/p to you directly. I have also provide my code snippet, and my findings too. – AB Bolim Mar 08 '18 at 08:21
  • If the various methods you've tried (`remove(2, 1)` etc.) don't work as expected, then can I suggest that the contents of the `QByteArray` are *not* what you think they are. Also, using [`QIODevice::readyRead`](http://doc.qt.io/qt-5/qiodevice.html#readyRead) the way you do is probably not reliable -- the signal will be emitted as soon as `Qt` notices data is available so there's no guarantee that *all* of the response is available from the device. – G.M. Mar 08 '18 at 11:15

1 Answers1

1

The QByteArray returned by the serial port is created from raw data and does not behave like a character string. In order to process the full array you can use the STL style iterator which is robust to the included NULL characters. For more info about fromRawData arrays, please carefuly read the detailed description in the documentation.

Here's a dirty example showing how you can fix your string:

const char mydata[] = {
    '1', '0', 0x00, ';', '1', '3', '9', '5', '9', '9', '4', '8', '8', '1', ';', '1', '.', '0', '.', '0'
};
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
qDebug() << data; // "10\x00;1395994881;1.0.0"

char fixed[255];
int index = 0;
QByteArray::iterator iter = data.begin();
while(iter != data.end())
{
    QChar c = *iter;
    if (c != '\0') fixed[index++] = c.toLatin1();
    iter++;
}
fixed[index] = '\0';

qDebug() << fixed; // 10;1395994881;1.0.0
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36