Main Content

Protocol Encoder

Encode input data into a uint8 byte stream by specifying the packet structure

Since R2024a

Add-On Required: This feature requires the Embedded Coder Support Package for STMicroelectronics STM32 Processors add-on.

  • Protocol Encoder

Libraries:
Motor Control Blockset / Signal Management
C2000 Microcontroller Blockset / Target Communication

Description

The Protocol Encoder encodes input data into a uint8 byte stream as per the specified packet structure based on the communication protocol. You can use this block to encode the separate fields into a packet, by specifying the header, terminator, name of packet fields in sequence and their size. You can also generate checksum bytes for the packet for validating the packet.

Ports

Input

expand all

First field to be encoded by the block. The name of input port is defined using the block parameter Field name. Each subsequent field that you create while defining the packet structure results in a new input port, with the Field name value appearing as the name of input port.

The block can have from 1 to N input ports. N is the number of fields in the packet structure that you specify in the Specify packet fields sequentially pane.

Output

expand all

The uint8 byte stream packet that contains the encoded data from multiple signals as specified in the packet structure.

Data Types: uint8

Parameters

expand all

Specify the header that indicates the beginning of the encoded data. The encoded output data begins with the header.

The numeric array specified in this parameter is the uint8 integer representation of the corresponding ASCII characters.

Few examples of header values are: ‘START’, '$’, 36, [36,37].

Specify packet fields sequentially

Specify the name of the field to be included in the packet structure. This name appears as the name of the input port.

To add a new packet field and specify its properties, click Add. After adding a field, click Move Up or Move Down to change its sequence in the structure.

To delete a field and its properties, select the row and click Delete.

Length of the input field as per the data type that you enter in the next column (Input field data type). For example, if the Input field data type of a field is ‘uint16’ and the length is 1, the encoder converts the input into two uint8 bytes in the output packet.

Select the data type of the field.

Note

double is considered as 8 bytes. If your hardware does not support 8 bytes double, selecting this data type might give incorrect results.

Select the endianess of the packet field to describe the order in which bytes are unpacked.

Specify the logic to generate checksum for validation using standard algorithms or a custom algorithm.

If the logic is selected as XOR of bytes, the block calculates checksum as a single byte which is XOR of all bytes excluding header, terminator (if applicable).

If the logic is selected as 2's complement of sum of bytes, the block calculates checksum as a single byte which is the 2's complement of sum off all bytes excluding header and terminator (if applicable).

If you select Custom algorithm, the additional parameters – Checksum size and File path – are enabled. In this case, you can provide your own custom logic to validate packet. The checksum bytes returned by the custom logic should be of size specified in the Checksum size field and these bytes are appended to the packet. For more information, see Function Template for Custom Checksum Logic in Protocol Decoder Block (C2000 Microcontroller Blockset).

For the encoded packet in all the above three cases, checksum bytes are appended either at the end (if terminator is not available) or before the terminator (if terminator is specified).

Specify size of the checksum bytes generated by the function used for custom algorithm. For more details, see Function Template for Custom Checksum Logic in Protocol Decoder Block (C2000 Microcontroller Blockset).

Dependencies

This parameter is enabled only if you select Custom algorithm option under Specify logic for checksum validation parameter.

Data Types: uint8

Specify the path of the function (.m) in which you defined the custom algorithm for generating checksum bytes. The file must be saved in a directory in the MATLAB path.

Dependencies

This parameter is enabled only if you select Custom algorithm option under Specify logic for checksum validation parameter.

Data Types: uint8

Specify the terminator that indicates the end of the encoded data. The last byte/bytes in the encoded output is the terminator.

If you select Custom Terminator, you can specify your own terminator value.

Specify the custom terminator that indicates the end of the encoded data. The last byte/bytes in the encoded output is the terminator.

The numeric array specified in this parameter is the uint8 integer representation of the corresponding ASCII characters.

Few examples of custom terminator values are: ‘END’, '#’.

Data Types: uint8

More About

expand all

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Version History

Introduced in R2024a