Well ... that almost worked ... except for QByteArray ...

Given the following struct:

Qt Code:
  1. //! \brief SERIAL PACKET FORMAT (Physical Layer - RS232)
  2.  
  3. typedef struct {
  4. quint8 DLE; // ASCII DLE character (16 decimal)
  5. quint8 packet_id; // packet ID
  6. // types:
  7. // 6 - ACK
  8. // 10 - Command
  9. // 14 - Date/Time Data
  10. // 21 - NAK
  11. // 38 - Unit ID/ESN
  12. // 51 - PVT (Position, Velocity, Time) Data
  13. // 135 - Legacy Stop message
  14. // 136 - Legacy text message
  15. // 161 - Fleet Management packet
  16. quint8 size_app_payload; // number of bytes of packet data (bytes 3 to n-4)
  17. QByteArray app_payload; // 0 to 255 bytes
  18. quint8 checksum; // 2's complement of the sum of all bytes from byte 1 to byte n-4 (end of the payload)
  19. quint8 DLE_end; // same as DLE
  20. quint8 ETX; // End of text - ASCII ETX character (3 decimal)
  21.  
  22. //! \note helper functions
  23. QByteArray serialize()
  24. {
  25. QByteArray byteArray;
  26.  
  27. QDataStream stream(&byteArray, QIODevice::WriteOnly);
  28.  
  29. stream << DLE
  30. << packet_id
  31. << size_app_payload
  32. << app_payload
  33. << checksum
  34. << DLE_end
  35. << ETX;
  36. return byteArray;
  37. }
  38.  
  39. void deserialize(const QByteArray& byteArray)
  40. {
  41. QDataStream stream(byteArray);
  42.  
  43. stream >> DLE
  44. >> packet_id
  45. >> size_app_payload
  46. >> app_payload
  47. >> checksum
  48. >> DLE_end
  49. >> ETX;
  50. }
  51. } serial_packet_format;
To copy to clipboard, switch view to plain text mode 

and the following code:

Qt Code:
  1. serial_packet_format *sPacket = new serial_packet_format; // send fleet management packet wrapped in a serial packet format...
  2. sPacket->DLE=16;
  3. sPacket->packet_id= FLEET_MANAGEMENT; // fleet management packet
  4. // fill rest
  5. sPacket->size_app_payload=driver_id_receipt.size();
  6. sPacket->app_payload = QByteArray::QByteArray ( driver_id_receipt );
  7. // calculate 2's complement checksum
  8. sPacket->checksum = CalculateChecksum(driver_id_receipt.data(), driver_id_receipt.size() );
  9. sPacket->DLE_end=16;
  10. sPacket->ETX=3;
  11.  
  12. QByteArray serial_packet( sPacket->serialize() );
To copy to clipboard, switch view to plain text mode 

what happens is that upon
Qt Code:
  1. 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