Skip to main content
Version: 3.0.x

UART message creation

This is a description of how to create a UART message for the driver and common block API.

UART message structure

ByteDescription
0session | 0x80
1(session | 0x80)^0x55
2Message lenght -1
3SID
4Receiver address
5Payload bit 0
6Payload bit 1
7Payload bit 2
5 + nPayload bit n
Last byteCRC

How to calculate CRC

It is XOR from bytes in the UART message. The First 2 bytes are not counted to CRC.

crc = Byte2 ^ Byte3 ^ Byte4 ^ ... ^ Last_byte

Example of code to create the UART frame in C

/*!
\brief Create the UART frame

\param[in] data_len - Number of bytes to send
\param[in] data - data to send
0 byte of data = SID
1 byte of data = Receiver addr
2-end bytes = Payload
\param[in] txbuf - Output buffer
\param[in] tx_len - Number of bytes in output buffer

\return int32_t
0 - succes
-1 - output (tx) buffer is small
*/
int32_t
umsg_send_msg(uint8_t data_len, uint8_t* data, uint8_t* txbuf, uint8_t tx_len)
{
static uint8_t session = 0;

if (data_len + 4 > tx_len)
return -1;

uint8_t* ptr = txbuf;
uint8_t i;
session |= 0x80;
*ptr++ = session;
*ptr++ = session ^ 0x55;
*ptr++ = data_len - 1;

uint8_t crc = 0;
crc ^= data_len - 1;

for (i = 0; i < data_len; i++) {
crc ^= *data;
*ptr++ = *data++;
}

*ptr++ = crc;
session++;

return 0;
}