Protocol Encoder
Encode input data into a uint8 byte stream by specifying the packet structure
Since R2023b
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
Simple Serial Communication with Encoder and Decoder Blocks
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 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 |
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.
Programmatic Use
Block Parameter:
FieldNames |
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.
Programmatic Use
Block Parameter:
FixedSizeFieldLength |
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.
Programmatic Use
Block Parameter:
FixedFieldDataTypes |
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.
Programmatic Use
Block Parameter:
IsLittleEndian |
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 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 |
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.
Dependencies
This parameter is enabled only if you select Custom
algorithm
option under Specify logic for checksum
validation parameter.
Programmatic Use
Block Parameter:
ChecksumSize |
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.
Programmatic Use
Block Parameter:
ChecksumFilepath |
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 is the terminator.
If you select Custom Terminator
, you can specify your own
terminator value.
Programmatic Use
Block Parameter:
TerminatorOption |
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 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
See Also
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 (한국어)