[SOLVED] Nasty String Ending
I need to transmit an int16 over a serial port. To do this in a fast way, I split the variable into two byte. Now, when I want to read these two byte into a QString or a QByteArray because the read() - function of the qserialdevice library allows no other type to write in, I get a problem. When the value of my int16 is below 256, the first byte is 0. And unfortunatly 0 is the string ending constant. So my string stops at this point and I don't gat any more information in that String. Does anyone have an idea how to bypass this string ending rule?
You may want to consider using a QDataStream to send your data, if possible. I'm not sure that sticking raw data into a QString is the best idea.
QString::number() and QString::toInt() could be of use here.
Another option is a QDataStream, as suggested by mlong already. You can open it on a QBuffer that is built on top of a QByteArray.
Obviously I need a QDataStream. I tried to play a bit around with QDataStream and QBuffer, but I wasn't able to set this up. It is the first time I use these classes and I haven't found any useful examples. If somebody could give me a short example how I can write a QByteArray with a QDataStream into a QBuffer and read this out again, that would be great!
tobias.hunger last edited by
I would not use QDataStream as that does add marshalling information into its output stream which is most likely not expected on the other side... provided you do not use a QDataStream there, too.
You are aware that you can use QIODevice::readAll() does put all the bytes available into the QByteArray? Check the count() of the byte array: I bet it says more than 0 in your case. You just can not interpret the first bytes as a string as you did not put a string there but an integer.
@qint32 = reinterpret_cast<qint32>(data.constData());@
should get the 32bit integer out of the first 4 bytes of the QByteArray data though. Make sure to not go past the end of the QByteArray;-)
PS: When moving data between devices you should use qu?int(8|16|32|64) as data types instead of a simple int. Int is defined as >=32 bit, and thus might differ between your sender and your receiver.
You're right. The data is in the QByteArray. QDebug is just not able to show what is in there but the size is obviously 4. These 4 Bytes consist of an STX (0x02) Byte then 2 Byte for the uint16 and finally the ETX (0x03) Byte. So first I have to check the QByteArray if it was transmittet correctly with the STX-ETX Chain and extract the 2 Bytes in the mid. Finally I get a QByteArray with my 2 Bytes.
I put them together in an traditional way like this:
@uint16 myValue = valueArray | (valueArray << 8);@
This works so far altough I get 2 warnings I don't understand for this and a similar tag:
@while(valueArray != 0x02) valueArray.remove(0,1);@
bq. warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
If I try to convert the 2 Bytes with your method like this:
@quint16 = reinterpret_cast<quint16>(valueArray.constData());@
I get this error:
bq. error: cast from 'const char*' to 'quint16' loses precision
But anyway, thx for the help until here!