Documentation

ASCII Encode/Decode (Composite)

ASCII Encode/Decode

Library

Simulink® Real-Time™ Library for RS-232

Description

The ASCII Encode block generates a UINT8 output vector that contains a NULL-terminated string based on a printf like format string and data on the input ports.

The ASCII Decode block parses an input vector according to a format specifier similar to scanf, and makes converted values available to a Simulink program. The input vector to the ASCII Decode block can be either 8-bit or 16-bit and signed or unsigned. If the data format is 16-bit, the ASCII Decode block ignores the upper eight bits of each entry.

Encode Block Parameters

Format string

Enter a printf like format string. Each format specifier such as %d is replaced by the converted value that is present on the corresponding input variable. Acceptable format specifiers are %c, %d, %i, %o, %u, %x, %e, %f, and %g. These follow the normal description for printf.

Number of variables

Enter the number of input ports to this block. The value on each port is inserted into the output string with the format specified in Format string.

Max output string length

Enter the maximum allowed length of the converted string, in bytes. The block allocates enough memory to support this length for the output port. When selecting this length, take into account the NULL termination on the string.

If the converted string exceeds this length, the block returns an error and does not write that string to the output port.

Variable types

Enter one of the following: {'double'}, {'int8'}, {'uint8'}, {'int16}, {'uint16'}, {'int32'}, and {'uint32'}. The default is {'double'}. This parameter specifies the Simulink data types allowed for the input ports. A cell vector with the same number of elements as specified in Number of variables can specify a different data type for each input port. A single element is replicated. For example,

nvars=3

{ } — The three inputs are doubles.

{'uint8'} — The three inputs are uint8.

{'uint16', 'double', 'uint8'} — The first input is a uint16, the second input is a double, and the third input is a uint8.

Decode Block Parameters

Format string

Enter a scanf like format string. Each format specifier such as %d needs to match a corresponding part of the input vector. Literal strings in the format need to match the first character plus the number of characters. Acceptable format specifiers are %c, %d, %i, %o, %u, %x, %e, %f, and %g. These follow the normal description for scanf.

Number of variables

Enter the number of output ports for this block. For example,

If Format string has the value of %xmore text%x and the input vector for the block has cdmabcdefgh90, you must specify the value of the Number of variables parameter as 2.

The first variable is assigned the value 0xcd. Next, the string mabcdefgh is considered a match to more text because

  • The first character for both strings is m.

  • Both strings have the same number of characters.

The second variable is then assigned the value 0x90. Note that the string mabcdefgh does not have to match exactly the value of Format string. This behavior is different from that for scanf, which requires an exact match.

Variable types

Enter one of the following: {'double'}, {'int8'}, {'uint8'}, {'int16}, {'uint16'}, {'int32'}, and {'uint32'}. The default is {'double'}. This parameter specifies the Simulink data types allowed for the output ports. A cell vector with the same number of elements as specified in Number of variables can specify a different data type for each output port. A single element is replicated. For example,

nvars=3

{ } — The three outputs are doubles.

{'uint8'} — The three outputs are uint8.

{'uint16', 'double', 'uint8'} — The first output is a uint16, the second output is a double, and the third output is a uint8.

Was this topic helpful?