I've made a tansmition with a ascii code style and everything was ok until I made crc check. In my project crc is a one byte. In my reciver i put everything into std::string and if crc is smaller then 127 everything is ok. But if my crc is grater then 127 then to my std::string is add addtional data - example below.
if crc = 80 -> in std:string "P"
if crc = 168 -> in std::string "�" (three diffrent signs)
What am I doing wrong? I thought that there are special character above the 127, but now I'm lost...
EDIT: I've tried to make it simple, but of course I can give u code :).
QSerialPort *serial = new QSerialPort;
std::string *buffer = new std::string;
//when 'readyRead' event comes from serial then:
QString tempBuffer = serial->readAll();
*buffer += tempBuffer.toStdString();
Then in diffrent thread I work on the buffer data. The problem is when I sending a byte which is bigger then 127 - in that situation some additional character are appended to my buffer (like in the first example).
The problem is here:
QString tempBuffer = serial->readAll();
*buffer += tempBuffer.toStdString();
Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromUtf8(). Stops copying at the first 0 character, otherwise copies the entire byte array.
You will need to call fromLatin1 explicitly in that case on the return value of readAll, i.e.:
QString tempBuffer = QString::fromLatin1(serial->readAll());
qDebug() << tempBuffer;
Your requirement could also be local 8 bit related, in which case you would do something similar but with fromLocal8Bit.
It is bad idea to allocate memory for the std::string object on the heap. It is meant to be a stack object.
In this scenario, the code will be much simpler if you drop the std::string internal data handling altogether because then you can also drop QString for the general operation and you can keep that only for data display. That way, you could also get the "raw c data" with the data() method.
There are some things to keep in mind when dealing with character data over 127, namely:
You need to have 8 data bit set for the property which is the default these days, but you need to make sure it is not 7 bits.
The QIODevice interface deals with char* type for reading and writing, and that is compiler dependent whether it is signed or unsigned. So long as you handle them consistently on the receiver and sender sides, this will not be an issue though.