Unsolved I will send data via serialport
-
I will send 3 bytes of data via serialport.
my variables;
uint8_t x;
uint16_t y;After giving values to these variables;
QByteArray sendData;
sendData[0] = x;
sendData[1] = y;
serial->write(sendData);
Do I have to write the code like this? Would you help me with this topic ? -
@KARMA said in I will send data via serialport:
Do I have to write the code like this?
No. Rather like this:
QByteArray sendData; sendData[0] = x; sendData.setNum(y); serial->write(sendData);
Also, why not simply try what you have and see whether it works?
-
@jsulm said in I will send data via serialport:
sendData.setNum(y);
You sure?? IIRC, QByteArray::setNum()
Sets the byte array to the printed value
Printed value means string. That's not what the OP means by "3 bytes", I presume he means he wants the binary 2 bytes for the
uint16_t
? -
@JonB Oh, you're right.
sendData.append() should do. -
@jsulm
I don't think so, as there is noQByteArray::append()
which accepts a number/uint16_t
as a parameter? ;-)
Are you meaning something like:QByteArray::append(&y, sizeof(y))
with whatever casting required for
&y
->const char *
? -
@KARMA said in I will send data via serialport:
After giving values to these variables;
QByteArray sendData;
sendData[0] = x;
sendData[1] = y;
serial->write(sendData);
Do I have to write the code like this? Would you help me with this topic ?I would suggest you to use QDataStream:
QByteArray sendData; QDataStream stream(&senData, QIODevice:WriteOnly); stream << x; stream << y; serial->write(sendData);
EDIT: use QIODevice:WriteOnly!
-
@KroMignon
This looks better to me, assumingQDataStream
does not put any extra "information" for auint16_t
, which I can never recall whether it does or not. However, doesQDataStream
write any kind of header into the stream, e.g. to convey version number or anything? Or will your code produce just the only 3 bytes wanted by the OP?Separately, since you are writing data to
stream
why is it openedQIODevice::ReadOnly
? -
This looks better to me, assuming QDataStream does not put any extra "information" for a uint16_t, which I can never recall whether it does or not
There are no extra informations added, I use
QDataStream
in many softwares.Separately, since you are writing data to stream why is it opened QIODevice::ReadOnly?
It was a mistake, I change it to
QIODevice::WriteOnly
-
@KroMignon said in I will send data via serialport:
There are no extra informations added
Thanks, I have now read through https://doc.qt.io/Qt-5/qdatastream.html#versioning and understand reader and writer need to use same version (to be sure) but
QDataStream
does not put the version into the data stream. -
@KroMignon said in I will send data via serialport:
I would suggest you to use QDataStream:
QByteArray sendData; QDataStream stream(&senData, QIODevice:WriteOnly); stream << x; stream << y; serial->write(sendData);
EDIT: use QIODevice:WriteOnly!
I am sorry for the delay. I wasn't here for a while.
thanks for this suggestion when i made my code like this i was able to send data properly. -
@KARMA said in I will send data via serialport:
i was able to send data properly
Great. So if your issue is solved please don't forget to mark your post as such!