Well ... that almost worked ... except for QByteArray ... 
Given the following struct:
//! \brief SERIAL PACKET FORMAT (Physical Layer - RS232)
typedef struct {
quint8 DLE; // ASCII DLE character (16 decimal)
quint8 packet_id; // packet ID
// types:
// 6 - ACK
// 10 - Command
// 14 - Date/Time Data
// 21 - NAK
// 38 - Unit ID/ESN
// 51 - PVT (Position, Velocity, Time) Data
// 135 - Legacy Stop message
// 136 - Legacy text message
// 161 - Fleet Management packet
quint8 size_app_payload; // number of bytes of packet data (bytes 3 to n-4)
quint8 checksum; // 2's complement of the sum of all bytes from byte 1 to byte n-4 (end of the payload)
quint8 DLE_end; // same as DLE
quint8 ETX; // End of text - ASCII ETX character (3 decimal)
//! \note helper functions
{
stream << DLE
<< packet_id
<< size_app_payload
<< app_payload
<< checksum
<< DLE_end
<< ETX;
return byteArray;
}
void deserialize(const QByteArray& byteArray)
{
stream >> DLE
>> packet_id
>> size_app_payload
>> app_payload
>> checksum
>> DLE_end
>> ETX;
}
} serial_packet_format;
//! \brief SERIAL PACKET FORMAT (Physical Layer - RS232)
typedef struct {
quint8 DLE; // ASCII DLE character (16 decimal)
quint8 packet_id; // packet ID
// types:
// 6 - ACK
// 10 - Command
// 14 - Date/Time Data
// 21 - NAK
// 38 - Unit ID/ESN
// 51 - PVT (Position, Velocity, Time) Data
// 135 - Legacy Stop message
// 136 - Legacy text message
// 161 - Fleet Management packet
quint8 size_app_payload; // number of bytes of packet data (bytes 3 to n-4)
QByteArray app_payload; // 0 to 255 bytes
quint8 checksum; // 2's complement of the sum of all bytes from byte 1 to byte n-4 (end of the payload)
quint8 DLE_end; // same as DLE
quint8 ETX; // End of text - ASCII ETX character (3 decimal)
//! \note helper functions
QByteArray serialize()
{
QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);
stream << DLE
<< packet_id
<< size_app_payload
<< app_payload
<< checksum
<< DLE_end
<< ETX;
return byteArray;
}
void deserialize(const QByteArray& byteArray)
{
QDataStream stream(byteArray);
stream >> DLE
>> packet_id
>> size_app_payload
>> app_payload
>> checksum
>> DLE_end
>> ETX;
}
} serial_packet_format;
To copy to clipboard, switch view to plain text mode
and the following code:
serial_packet_format *sPacket = new serial_packet_format; // send fleet management packet wrapped in a serial packet format...
sPacket->DLE=16;
sPacket->packet_id= FLEET_MANAGEMENT; // fleet management packet
// fill rest
sPacket->size_app_payload=driver_id_receipt.size();
// calculate 2's complement checksum
sPacket->checksum = CalculateChecksum(driver_id_receipt.data(), driver_id_receipt.size() );
sPacket->DLE_end=16;
sPacket->ETX=3;
serial_packet_format *sPacket = new serial_packet_format; // send fleet management packet wrapped in a serial packet format...
sPacket->DLE=16;
sPacket->packet_id= FLEET_MANAGEMENT; // fleet management packet
// fill rest
sPacket->size_app_payload=driver_id_receipt.size();
sPacket->app_payload = QByteArray::QByteArray ( driver_id_receipt );
// calculate 2's complement checksum
sPacket->checksum = CalculateChecksum(driver_id_receipt.data(), driver_id_receipt.size() );
sPacket->DLE_end=16;
sPacket->ETX=3;
QByteArray serial_packet( sPacket->serialize() );
To copy to clipboard, switch view to plain text mode
what happens is that upon
stream << size_app_payload
stream << size_app_payload
To copy to clipboard, switch view to plain text mode
a DWORD containing the QByteArray size is prepended to the original QByteArray... 
In other words:
Upon execution of the above code here's what's inside sPacket:
SERIAL PACKET CONTENTS (size=20):
10
FFFFFFA1
0A
00
00
00
0A --> the 4 bytes in bold shouldn't be here!
08
12
00
00
00
01
01
00
00
00
FFFFFFE4
10
03
Please advise as this is the last thing to overcome my long standing problem... 
BR,
Pedro Doria Meunier
Bookmarks