Main Content

Protocol Encoder

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

Since R2023b

  • Protoco Encoder block

Libraries:
Simulink Real-Time / Utilities

Description

The Protocol Encoder block 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. These figures show how the Protocol Encoder and Protocol Decoder can simplify serial communications.

Simple Serial Communication without Encoder and Decoder Blocks

Model slrt_ex_serialasciitest_protocol_e_d showing simple serial communications.

Simple Serial Communication with Encoder and Decoder Blocks

Model slrt_ex_serialasciitest_protocol_e_d modified to show encoder and decoder blocks for simple serial communications.

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].

Programmatic Use

Block Parameter: Header
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.

Programmatic Use

Block Parameter: FieldNames

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.

Programmatic Use

Block Parameter: FixedSizeFieldLength

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.

Programmatic Use

Block Parameter: FixedFieldDataTypes

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

Programmatic Use

Block Parameter: IsLittleEndian

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 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).

Programmatic Use

Block Parameter: ChecksumAlgorithm

Specify size of the checksum bytes generated by the function used for custom algorithm.

Dependencies

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

Programmatic Use

Block Parameter: ChecksumSize

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.

Programmatic Use

Block Parameter: ChecksumFilepath

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.

Programmatic Use

Block Parameter: TerminatorOption

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’, '#’.

Programmatic Use

Block Parameter: CustomTerminatorVal

Extended Capabilities

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

Version History

Introduced in R2023b