
Originally Posted by
d_stranz
If struct x_hdr's size is less than 256 bytes, then only one byte is needed to hold the size. I think you are confusing the sizes of the variables in the struct with the size of the struct itself. Just because one of the data members is 16 bits long, that adds only 2 bytes to sizeof( x ).
If you need to write a 2-byte size to your buffer, then store sizeof( x ) in a quint16_t variable and write that variable out. Same thing for your checksum.
I am trying to explain briefly, with pseudo code
serial data frame Format:
--------------------------------------------------------------------------------------------------------------------
(1 byte) | (1 byte) | (2 bytes) | (n bytes) | (2 bytes) | (1 byte)
-------------------------------------------------------------------------------------------------------------------
SOF | seq | len [holds length of payload] | payload | checksum | EOF
---------------------------------------------------------------------------------------------------------------------
typedef struct x_hdr
{
uint16_t id;
uint16_t type;
} X_HDR;
void MainWindow
::build_msg (uint8_t seq,
QByteArray *tx_msg
) {
uint16_t checksum;
X_HDR hdr;
switch(seq)
{
case 1:
/* update the command ID, 1byte long
tx_msg->append(seq);
/* update data to frame */
hdr.id = 0x1234;
hdr.type = 0x0;
/* update data len field, which is of 2bytes. as my struct is of 4 bytes, this field will be filled with a value 4*/
tx_msg->append(sizeof(hdr));
/* append data of nbytes, here in this case it's 4 bytes */
tx_msg->append((char *) &hdr,sizeof(hdr));
break;
}
/* Again checksum field is of 2 bytes long in the serial frame */
checksum = add_chksum(&hdr, sizeof(hdr));
tx_msg->append(checksum);
}
void MainWindow::button_pressed()
{
/* Appending SOF 0xAF (1 byte) to the serial frame */
command.append(0xAF);
build_msg (1,&command);
/*Appending EOF 0xFF (1 byte) to the serial frame */
command.append(0xFF);
/* Function writes command to serial port */
writeData(command);
}
typedef struct x_hdr
{
uint16_t id;
uint16_t type;
} X_HDR;
void MainWindow::build_msg (uint8_t seq, QByteArray *tx_msg)
{
uint16_t checksum;
X_HDR hdr;
switch(seq)
{
case 1:
/* update the command ID, 1byte long
tx_msg->append(seq);
/* update data to frame */
hdr.id = 0x1234;
hdr.type = 0x0;
/* update data len field, which is of 2bytes. as my struct is of 4 bytes, this field will be filled with a value 4*/
tx_msg->append(sizeof(hdr));
/* append data of nbytes, here in this case it's 4 bytes */
tx_msg->append((char *) &hdr,sizeof(hdr));
break;
}
/* Again checksum field is of 2 bytes long in the serial frame */
checksum = add_chksum(&hdr, sizeof(hdr));
tx_msg->append(checksum);
}
void MainWindow::button_pressed()
{
QByteArray command;
/* Appending SOF 0xAF (1 byte) to the serial frame */
command.append(0xAF);
build_msg (1,&command);
/*Appending EOF 0xFF (1 byte) to the serial frame */
command.append(0xFF);
/* Function writes command to serial port */
writeData(command);
}
To copy to clipboard, switch view to plain text mode
My question is How can I make sure irrespective of value in the "len" and "checksum", I want to allocate 2bytes for each fields in the serial frame.
Is this possible with above approach? What needs to be corrected here.
Best Regards,
Anita
Bookmarks