PDA

View Full Version : quint16 to QByteArray Conversion



zidoune06
10th January 2013, 15:21
Hello All,

I begin to work with Qt so sorry if the answer is easy for you.
I am working on a software which communicates with a device.
I use the QextSerialPort librairie and it is working fine.

In fact, it is working fine when I send to my device a constant frame.


/* Erasing Command */
static const char eraseCmd[] = {0x11, 0x02, 0x00, 0x00, 0x00, 0x02 };
QByteArray Command = QByteArray::fromRawData(eraseCmd, sizeof(eraseCmd));


But it is not working when I want to add in the frame a counter as an idFrame;
For example, if I send the 499th frame, I would like to send :
{beginning if the frame....,0x01, 0xF3,...end of the frame }.

I try a lot of functions, but maybe not the good one, like setNum(idFrame,16) but it returns :
Frame[X] = 3 ;
Frame[X+1] = F ;
Frame[X+2] = 1 ;

The best way for me will be to get :
Frame[X] = "0x01" ;
Frame[X+1] = "0xF3" ;

Can someone help me ?
Thank you in advance for your support.

Zidoune

Lesiok
10th January 2013, 16:55
Something like this (frame_no is 2 byte unsigned integer) :


Frame[X+2] = frame_no % 0xFF ;//lower byte
Frame[X+1] = frame_no / 0xFF ;//higher byte

zidoune06
22nd January 2013, 17:06
Thank you for your help Lesiok !

ChrisW67
22nd January 2013, 21:09
You need to understand the difference between the one byte number 0xF3 (243 in decimal) and the 5 byte string "0xF3" or to understand why setNum() was not the correct approach.