Main Content

Protocol Decoder

Decode a uint8 byte stream by specifying the packet structure

Since R2021b

Add-On Required: This feature requires the Simulink Support Package for Arduino Hardware add-on.

  • Arduino Protocol Decoder Block Icon

Libraries:
Simulink Support Package for Raspberry Pi Hardware / Utilities
Simulink Support Package for Arduino Hardware / Common
C2000 Microcontroller Blockset / Target Communication

Description

The Protocol Decoder block decodes a uint8 byte stream as per the specified packet structure based on the communication protocol. You can use this block to decode packet into separate fields, by specifying the header, terminator, name of packet fields in sequence and their size. You can also validate the packet using the checksum logic that you specify.

Ports

Input

expand all

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

The input packet can be partially received over multiple sample times. The Protocol Decoder block has the capability to buffer such partial data until the complete packet is received.

Data Types: uint8

Optional input port to include the exact length of valid data stream. Use this option when you know the exact length of the valid data in the input data stream

This option is useful when you have a communication channel receive peripheral that outputs partially received data that contains trailing zeros. Such peripherals also output the length of the actual number of valid data bytes received. You can connect the length output of the peripheral directly with the Length input port of Protocol Decoder block, so that trailing zeros in the input byte stream do not affect the decoding logic.

Dependencies

This input port appears only if you select the Is input data stream length available parameter.

Data Types: uint16

Output

expand all

Name of the first field decoded by the block. The name of the output port is defined using the block parameter, Field name. Each subsequent field that you create while defining the packet structure results in a new output port, with the Field name value appearing as the name of output port.

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

Data Types: string

New data indicator returned as a logical. A value of 1 indicates that a new data is available since the last sample was received by the block. This output can be used to trigger subsystems to process new data received from the Protocol Decoder block.

If IsNew = 0, the block outputs decoded fields from the last complete packet decoded by the block. If no last sample is available, the block outputs zeros.

Data Types: Boolean

Checksum validation indicator returned as a logical. A value of 1 indicates that the input packet received by the block is valid when validated using the checksum logic that you specified. If IsValid = 0, the block outputs zeros.

Dependencies

This output port appears only if you select the Specify logic for checksum validation parameter.

Data Types: Boolean

Actual data length of the last field that is variable-size. The maximum size of the variable size field can be specified using Specify maximum length if last field has variable size parameter. The block outputs the variable-size data as a fixed size data with size that is equal to the maximum length specified. The output contains actual data followed by trailing zeros. The size of actual data can be determined using this Variable Field Length output.

Dependencies

This output port appears only if you select the Specify maximum length if last field has variable size parameter. For more details, see Decode Variable-size Packet.

Data Types: double

Actual data lengths of comma separated variables that are decoded from the input stream.

This output value (1-by-N array) corresponds to the actual data length of the fields in the packet, where N is the number of comma separated variables in the packet.

When Parse comma separated variables is selected, the Output field length parameter value corresponds to the maximum size of the field. The actual size of the field is available as the Variable field lengths output.

If the actual field length of nth field is less than the specified Output field length parameter value on the block, the output corresponding to the field is actual data followed by trailing zeros. The nth element of the Variable field lengths output provides the actual length corresponding to that variable in that array.

If the actual field length of nth field is greater than the specified Output field length parameter value on the block, the output corresponding to the field is truncated and outputted, given the actual packet size does not exceed maximum packet size. The nth element of the Variable field lengths output provides the actual length corresponding to that variable in that array.

Dependencies

This output port appears only if you select the Parse comma separated variables parameter. For more details, see Decode Comma-separated Variable.

Data Types: double

Parameters

expand all

Specify that the input data to be parsed contains comma separated variables.

Data Types: uint8

Specify the header that indicates the beginning of the data. The simulation disregards data that occurs before the header. The header data is not sent to the output port.

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 included in the packet structure. This name appears as the name of the output 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 field in the packet structure as per the data type that you enter in the next column (Output field data type). For example, if the Output field data type of a field is uint16 and the length is 1, the decoder reads two bytes from the packet as per the packet structure and combine these bytes to output a uint16 value.

If you select Parse comma separated variables parameter, the Output field length value corresponds to the maximum length that the field can have.

Data Types: double

Select the output 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.

Note

If you select the Parse comma separated variables parameter, you cannot edit this parameter (the Output field data type is set to uint8 always).

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

Note

If you select the Parse comma separated variables parameter, you cannot edit this parameter (the Byte order is set to Little endian always).

Enable last field of the packet to output variable-size data and specify the maximum length of the variable length field, which is present as the last field in the packet. For more details, see Decode Variable-size Packet.

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

If you select Custom algorithm, the additional parameters – Checksum size and File path – are enabled.

Checksum bytes are expected to be in the last in the packet (before terminator, if terminator is available).

If you select the logic as XOR of bytes, checksum byte is expected to be the last byte before terminator. The logic calculates the XOR of all bytes excluding header, terminator and checksum byte, and compare it with the checksum byte.

If you select the logic as 2's complement of sum of bytes, checksum byte is expected to be the last byte before terminator. The logic calculates the 2's complement of sum off all bytes excluding header, terminator and checksum byte, and compare it with the checksum byte.

If you select Custom algorithm, you can provide your own custom logic to validate packet. In this case, checksum bytes are extracted as per the size specified in the Checksum size field. For more information, see Function Template for Custom Checksum Logic in Protocol Decoder Block.

Specify the number of bytes of checksum data. The blocks extracts the checksum bytes as per the specified size and passes it to the function used for custom algorithm, for validating the packet. For more details, see Function Template for Custom Checksum Logic in Protocol Decoder Block. Checksum bytes are expected to be in the last in the packet (before terminator, if terminator is available).

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 checksum logic. The file must be saved in a directory in the MATLAB® path. For more details, see Function Template for Custom Checksum Logic in Protocol Decoder Block.

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 data. The terminator data is not sent to the output port.

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

Data Types: uint8

Custom terminator that indicates the end of the data. The terminator data is not sent to the output port.

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

Enable optional input port to include the exact length of valid data stream.

More About

expand all

Version History

Introduced in R2021b