Convert serial data with QSerialPort
-
Hi!
From Arduino I'm sending a struct with two numbers:typedef struct packet{ uint8_t v1; uint8_t v2; }; packet pack; pack.v1 = v1; pack.v2 = v2; int x = Serial.write((byte*) &pack, sizeof(pack));in my Qt app, I'm reading with:
void Backend::receive() { QByteArray data = serial->readAll(); qDebug() << data; }but I wanna read the struct I sent and convert it to an array like [12, 91].
How can I do it? -
Hi!
From Arduino I'm sending a struct with two numbers:typedef struct packet{ uint8_t v1; uint8_t v2; }; packet pack; pack.v1 = v1; pack.v2 = v2; int x = Serial.write((byte*) &pack, sizeof(pack));in my Qt app, I'm reading with:
void Backend::receive() { QByteArray data = serial->readAll(); qDebug() << data; }but I wanna read the struct I sent and convert it to an array like [12, 91].
How can I do it? -
as mentioned, avoid structures when sending serial data (or understand your environment well enough to properly use pack() and understand its limitations). manually assemble your fields from the data in the stream....but this opens up a whole field of programming that folks routinely get wrong.
- add framing data and checksums to async serial comm messages
- don't use time division to parse messages, but always scan and parse the incoming stream as it arrives.
- understand that opposite ends of channels may have different endianness so formally specify endianness for your protocol
- NEVER transmiit floating point numbers in binary format over serial links
-
Hi!
From Arduino I'm sending a struct with two numbers:typedef struct packet{ uint8_t v1; uint8_t v2; }; packet pack; pack.v1 = v1; pack.v2 = v2; int x = Serial.write((byte*) &pack, sizeof(pack));in my Qt app, I'm reading with:
void Backend::receive() { QByteArray data = serial->readAll(); qDebug() << data; }but I wanna read the struct I sent and convert it to an array like [12, 91].
How can I do it?@arcy If you use Qt on both sides you can use https://doc.qt.io/qt-6/qdatastream.html