Protocol Encoder
Encode input data into 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.
Libraries:
Simulink Support Package for Raspberry Pi Hardware /
Utilities
Simulink Support Package for Arduino Hardware /
Common
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
Field1 — First field to be encoded by the block
scalar
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
Packet — uint8 byte stream packet
vector
The uint8 byte stream packet that contains the encoded data from multiple signals as specified in the packet structure.
Data Types: uint8
Parameters
Header — Header of the packet
character array | array of numeric values less than 255
Specify the header that indicates the beginning of the encoded data. The encoded output data will begin 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].
Field name — Name of the field in the packet structure
Field1
(default) | string
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.
Input field length — Length of the input field as per the data type
1
(default) | numeric scalar
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.
Input field data type — Data type of the field
uint8
(default) | int8
| int16
| uint16
| int32
| uint32
| int64
| uint64
| single
| double
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.
Byte order — Endianess of the field in the packet
Little endian
(default) | Big endian
Select the endianess of the packet field to describe the order in which bytes are unpacked.
Specify logic to generate checksum for validation — Enable the logic for checksum validation and select the logic
XOR of bytes
| 2's complement of sum of bytes
| Custom algorithm
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 will be appended to the packet. For more
information, see Function Template for Custom Checksum Logic in Protocol Encoder Block.
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).
Checksum size — Size of checksum bytes specified in the function used for custom algorithm
numeric scalar
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 Encoder Block.
Dependencies
This parameter is enabled only if you select Custom
algorithm
option under Specify logic for checksum
validation parameter.
Data Types: uint8
File path — Path of the function (.m) used for custom checksum logic
MATLAB path
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
Terminator — Terminator of the packet
<none>
(default) | CR ('\r')
| LF ('\n')
| CR/LF ('\r\n')
| NULL ('\0')
| Custom Terminator
Specify the terminator that indicates the end of the encoded data. The last byte/bytes in the encoded output will be terminator.
If you select Custom Terminator
, you can specify your own
terminator value.
Custom terminator — Custom terminator of the packet
character array | array of numeric values less than 255
Specify the custom terminator that indicates the end of the encoded data. The last byte/bytes in the encoded output will be 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
Encode Data from Fixed-Sized Packet
The Protocol Encoder block can be used to encode input data into a fixed size packet
(uint8
byte stream).
The block converts the input data into uint8 bytes as per the byte order specified, and appends the data with header, and optional checksum and terminator values.
The general structure of a fixed size packet look like this, with optional checksum and terminator fields
For example, suppose that you need to encode input into packet as specified in this structure:
In this case, you specify the block parameters as shown in the below figure. The output displayed when you simulate the model conforms to the packet structure.
Function Template for Custom Checksum Logic in Protocol Encoder Block
function csBytes = generateChecksumBytes(payload) %#codegen end
Create the function as specified above. The function must return checksum bytes
(csBytes) as uint8
data type, whose size must be specified in the
Checksum size field available in the block mask. The input to the
function is the payload containing all the data bytes excluding header and terminator, as
uint8
datatype.
Save the function and specify the path of the function in the File path field in the block mask. Ensure that the file is in the MATLAB path.
Monitor Signals and Tune Parameters
The Protocol Encoder block is available with Simulink® Support Package for Arduino® Hardware and Embedded Coder® Support Package for Texas Instruments® C2000™ Processors. However, to monitor signals and tune parameters for protocol encoding in Simulink models to be run on TI’s C2000™-based targets, use the Universal Measurement and Calibration Protocol (XCP)-based External mode simulation.
Version History
Introduced in R2021b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)