communication & strings blockset

51
Communication & Strings Blockset For use with Simulink version 2.0.4 of 17.Sep.2004 Matlab 5.3+: still not available Matlab 6.0: still not available Matlab 6.5: v. 2.0.4 (17.Sep.2004)

Upload: others

Post on 18-Dec-2021

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Communication & Strings Blockset

Communication & Strings

Blockset For use with Simulink

version 2.0.4 of 17.Sep.2004

Matlab 5.3+: still not available

Matlab 6.0: still not available

Matlab 6.5: v. 2.0.4 (17.Sep.2004)

Page 2: Communication & Strings Blockset

Index

Index................................................................................................................................... 2

Introduction ....................................................................................................................... 4

The COMM&STR Blockset for Simulink ...................................................................... 4

Blockset Overview.......................................................................................................... 5

Change record ................................................................................................................ 5

The COMM&STR blocks description................................................................................ 6

The Block List ................................................................................................................ 6

RS232_Setup .................................................................................................................. 8

RS232_Read ................................................................................................................... 9

RS232_Write ................................................................................................................ 10

LAN_Setup ................................................................................................................... 11

LAN_Read .................................................................................................................... 12

LAN_Write ................................................................................................................... 13

STRtoToken.................................................................................................................. 14

STRtoString.................................................................................................................. 15

STRtoBin ...................................................................................................................... 16

STRfromBin.................................................................................................................. 17

STRconst....................................................................................................................... 18

STRtoCStr .................................................................................................................... 19

STRfromCStr................................................................................................................ 20

STRlen .......................................................................................................................... 21

STRmem ....................................................................................................................... 22

STRcksum .................................................................................................................... 23

STRwriteBin................................................................................................................. 24

STRreadBin.................................................................................................................. 25

STRreadFixFmt ........................................................................................................... 27

STRwriteFmt................................................................................................................ 29

STRreadFmt................................................................................................................. 31

STRleft.......................................................................................................................... 33

STRright....................................................................................................................... 34

STRmid......................................................................................................................... 35

STRadd ......................................................................................................................... 36

STRprintf...................................................................................................................... 37

STRtoFile...................................................................................................................... 38

Page 3: Communication & Strings Blockset

STRfind......................................................................................................................... 39

STRextFind .................................................................................................................. 40

STRcustomIN ............................................................................................................... 41

STRcustomXBOW ........................................................................................................ 42

Format Specification Fields......................................................................................... 44

Format....................................................................................................................... 45

Format Width Specification ..................................................................................... 45

Binary Format Specification Fields ............................................................................ 46

Example: ................................................................................................................... 46

Blockset Application Example ........................................................................................ 47

Information and Warranty for the End User.............................................................. 49

Installation of the blockset .......................................................................................... 49

Acknowledgements (also for RS232 blockset)............................................................. 50

Page 4: Communication & Strings Blockset

Introduction

The COMM&STR blockset, also named RS232+LAN+STR blockset, is an evolution of the RS232 blockset, born from the necessity of having a versatile instrument to manipulate strings and communication in Simulink. This blockset is an evolution of the RS232 blockset, that will be no more supported and maintained. All functionalities of the RS232 blockset have been ported to this one, with more complex functionalities added.

The main innovation of the blockset is the introduction of the STRING object, that is used to port text information from a block to another and it can be used indifferently both with LAN and TCP blocks. This is not an open object (in the sense that its class is not public), but it can be used with more than 20 blocks to be combined, manipulated and converted or formatted.

Two more great thing of this blockset are the LAN (TCP/IP) blocks, whose use is very similar to the RS232 blocks, and the fact that the generation of strings may be easily customized (for people that is able to do it), so that a C language S-function may perform even a quick and complex C-language elaboration of strings and binary buffers read from serial or TCP port. An example of this customizable function is added to the blockset.

More than 30 blocks are available in this version, and other blocks will be developed in the future to cover some needs like the translation or reproduction of some dedicated protocols, like NMEA GPS protocol.

The COMM&STR Blockset for Simulink Each component of the blockset has been realized using a S-function written in C++ language (the relative DLLs are contained in the installation directory). The blockset is composed by some base blocks that can be composed to translate a complex serial protocol (ex.: a NMEA GPS protocol). The development of more blocks is foreseen in the case that one or more user of the blockset will signal a macro block needs.

Page 5: Communication & Strings Blockset

Blockset Overview

Change record Version 2.0.0 (5/9/04): First release. No documentation still available.

Version 2.0.2 (10/9/04): Documentation of RS232 blocks, LAN blocks and some STR blocks has been added. STRtoFile function has been updated: two arguments (separator and terminator chars) have been added.

Version 2.0.4 (17/9/04): Block RS232_Setup updated: removed Simulink segmentation violation bug in message string formatting (Thanks to Andrea Beltrame Pomè for bug signaling). Some documentation added on the present page.

Page 6: Communication & Strings Blockset

The COMM&STR blocks description

The Block List

Block Type Block name Short Description

RS232_Setup Initialize and close the serial communication.

RS232_Read This block reads from the serial port all the bytes in input and send it to the block port as a string object.

Serial Port Communication

RS232_Write This block writes to the serial port the string of the input port.

LAN_Setup Initialize and close the TCP/IP communication.

LAN_Read This block reads from the TCP/IP port all the bytes in input and send it to the block port as a string object.

LAN Port Communication

LAN_Write This block writes to the serial port the string of the input port.

STRtoToken This block divides a string in tokens, using the delimitation characters declared as block panel argument.

STRtoString Converts a vector of number into a vector of formatted strings

STRtoBin Converts a vector of numbers into a vector of binary strings

STRfromBin Converts a vector of binary strings into a vector of numbers

STRtoCStr Convert a string object in a C string

String transformation blocks

STRfromCStr Convert a C string in a string object

STRconst Generate a constant string (object)

STRlen Compute the length of a string

STRmem It’s the string equivalent of the discrete memory block. It stores the input for the next simulation step.

String utility blocks

STRcksum Compute the 8-bit or 16-bit checksum of the input string

STRwriteBin Create a string binary formatting the input number into string

STRreadBin Scan a binary string interpreting the content as described in the argument Format String

STRreadFixFmt Translate a fix formatted string into a vector of numbers.

STRwriteFmt This function creates a formatted string

String formatting blocks

STRreadFmt This function translates a formatted string

Page 7: Communication & Strings Blockset

STRprintf Print the input string in the Matlab Command Window

String output blocks

STRtoFile Send the input string to a file

STRfind Search a substring in a string String search blocks

STRextFind Search an input substring in a string

STRcustomIN A customization sample that use the output of the STRtoCStr block.

String customizable blocks STRcustomXBOW A customization sample that generates a C-string

pointer (and string length) that (approximately) simulates the output of the Crossbow IMU sensor.

STRleft Extracts a left portion of the string, for the indicated length.

STRright Extracts a right portion of the string, for the indicated length.

STRmid Extracts a mid portion of the string, starting from a given point, for the indicated length.

String manipulation blocks

STRadd Sum the content of two or more strings, adding, if requested, a string separator or terminator.

Page 8: Communication & Strings Blockset

RS232_Setup

Purpose Initialize and close the serial communication. The settings of the serial communication are selectable using the block panel

Parameters • Port: COM1/2/3/4 (If needed, a port number greater then 4 can be used simply modifying the block's mask).

• Baudrate: 115200,...,9600,...,110 baud

• Number of Databits: 8 / 7 / 6 / 5 bits

• Number of Stopbits: 1 / 2

• Parity: None / Odd / Even

Input None

Output • hnd: the first is used to transmit an handle for the opened communication. Therefore, it should be connected to all the blocks that use the port opened by this block. More then one RS232 Setup block can be present at the same time in the same simulation model, allowing the management of many serial port at the same time.

• flag: The second port is used to communicate the state of the port (0: not open, 1: open). This output is used to conditioning the others RS232 blocks to the actual status of the port.

Description

Even if this block is executed at each step, this block is active only in correspondence to the first and the last step of the simulation, step in which the block, respectively, open and close the communication. During the simulations, when the communication is open, the handle (hnd) and the flag (flag) are constant, unless a major communications error occurs: in that case, hnd and flag becomes 0.

Page 9: Communication & Strings Blockset

RS232_Read

Purpose This block reads from the serial port all the bytes in input and send it to the block port as a string object.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • hnd: the handle to an open communication link generated from a RS232 Setup block.

• flag: flag that enable the execution of the read operations (0: not enabled, 1: enabled). It is sufficient that the input flag becomes 1 once, to let the block executes its read operation.

Output • hnd: A replication of the Input 1 handle

• flag: A flag that indicated the completion (successful or not) of the read operation (0: no byte in input or port not open, 1: read complete).

• str: a string object containing the bytes read from the serial port.

Description

Remarks:

The block starts its read operations when received a '1' flag from the input 2. Once completed the read it modify the output 3 with new data and put the output 2 flag to '1' for only one simulation step. The string is presented to output port for only the simulation step in which the string has been read.

While the block is not active (the comm. handle is not valid (0) or the input flag (input 2) is not active, the block doesn't performs any operation.

Page 10: Communication & Strings Blockset

RS232_Write

Purpose This block writes to the serial port the string of the input port.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • hnd: the handle to an open communication link generated from a RS232 Setup block.

• flag: flag that enable the execution of the write operations (0: not enabled, 1: enabled). It is sufficient that the input flag becomes 1 once, to let the block executes its read operation. The block writes to the serial output each time the flag becomes 1.

• str: the string to be written to the serial port.

Output • hnd: A replication of the Input 1 handle

• flag: A flag that indicated the completion (successful or not) of the write operation (0: not completed or not enabled, 1: write complete).

Description

Remarks:

While the block is not active (the comm. handle is not valid (0) or the input flag (input 2) is not active) the block doesn't performs any operation.

Page 11: Communication & Strings Blockset

LAN_Setup

Purpose Initialize and close the TCP/IP communication. The settings of the serial communication are selectable using the block panel

Parameters • Port: Number of client/server port to open. Use big numbers (greater then 200) to avoid the use of an already busy port.

• Address: The address of the client/server. For a client block, this is the address of the host that runs the server. For a server block, this is the address (filter) of the client allowed to connect to the server.

• Type: Type of the port. May be client or server.

Input None

Output • hnd: the first is used to transmit an handle for the opened communication. Therefore, it should be connected to all the blocks that use the port opened by this block. More then one LAN Setup block can be present at the same time in the same simulation model, allowing the management of many serial port at the same time.

• flag: The second port is used to communicate the state of the port (0: not open, 1: open). This output is used to conditioning the others blocks to the actual status of the port.

Description

Even if this block is executed at each step, this block is active only in correspondence to the first and the last step of the simulation, step in which the block, respectively, open and close the communication. During the simulations, when the communication is open, the handle (hnd) and the flag (flag) are constant, unless a major communications error occurs: in that case, hnd and flag becomes 0.

Page 12: Communication & Strings Blockset

LAN_Read

Purpose This block reads from the TCP/IP port all the bytes in input and send it to the block port as a string object.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • hnd: the handle to an open communication link generated from a LAN Setup block.

• flag: flag that enable the execution of the read operations (0: not enabled, 1: enabled). It is sufficient that the input flag becomes 1 once, to let the block executes its read operation.

Output • hnd: A replication of the Input 1 handle

• flag: A flag that indicated the completion (successful or not) of the read operation (0: no byte in input or port not open, 1: read complete).

• str: a string object containing the bytes read from the serial port.

Description

Remarks:

The block starts its read operations when received a '1' flag from the input 2. Once completed the read it modify the output 3 with new data and put the output 2 flag to '1' for only one simulation step. The string is presented to output port for only the simulation step in which the string has been read.

While the block is not active (the comm. handle is not valid (0) or the input flag (input 2) is not active, the block doesn't performs any operation.

Page 13: Communication & Strings Blockset

LAN_Write

Purpose This block writes to the serial port the string of the input port.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • hnd: the handle to an open communication link generated from a LAN Setup block.

• flag: flag that enable the execution of the write operations (0: not enabled, 1: enabled). It is sufficient that the input flag becomes 1 once, to let the block executes its read operation. The block writes to the serial output each time the flag becomes 1.

• str: the string to be written to the serial port.

Output • hnd: A replication of the Input 1 handle

• flag: A flag that indicated the completion (successful or not) of the write operation (0: not completed or not enabled, 1: write complete).

Description

Remarks:

While the block is not active (the comm. handle is not valid (0) or the input flag (input 2) is not active) the block doesn't performs any operation.

Page 14: Communication & Strings Blockset

STRtoToken

Purpose This block divides a string in tokens, using the delimitation characters declared as block panel argument.

Parameters • Tokens separators string: the list of all characters that will be considered as string separators

• Number of output tokens: Indicates the maximum number of tokens in output. If the number of token in the string is smaller than the size of the output port, the remaining ports channels output null strings. If the number of token in the string is greater, the last port channel contains all the remaining non-tokenized strings.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string to be subdivided in tokens;

Output • str: A vector of token strings;

Description

Remarks:

Take care in what you connect to the input port. If the input is not a string, a major Simulink error may occurs.

Example:

Tokens separators string: @.

Number of output tokens: 3

Input string: [email protected]

Output vector: [leonardo, daga, libero.it]

Page 15: Communication & Strings Blockset

STRtoString

Purpose Converts a vector of number into a vector of formatted strings

Parameters • format string: a formatting string, that specifies the final numeric format of the numeric string. The format specification follows the same rules as the C-standard printf function, as described in the string format page.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • n: The input number vector;

Output • str: A vector of formatted strings. The width of the output is the same of the input width.

Description

Example:

format string: %5.3f

Input number vector: [1, 5, 223.33, 4.4]

Output vector: [1.000, 5.000, 223.3, 4.400]

Page 16: Communication & Strings Blockset

STRtoBin

Purpose Converts a vector of numbers into a vector of binary strings

Parameters • Data Type: The binary structure of the string to create. Possible types are: DOUBLE, SINGLE, INT8, UINT8, INT16, UINT16, INT32, UINT32;

• Invert byte order: Inverts the order of the bytes in the created string.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • n: The input number vector;

Output • str: A vector of binary formatted strings. The width of the output is the same of the input width.

Description

Example:

Data Type: SINGLE

Invert byte order: No

Input number vector: [1.0, 223.33, 4.4]

Output vector: [00 00 80 3f, 7b 54 5f 43, cd cc 8c 40]

Page 17: Communication & Strings Blockset

STRfromBin

Purpose Converts a vector of binary strings into a vector of numbers

Parameters • Data Type: The binary structure of the string to translate. Possible types are: DOUBLE, SINGLE, INT8, UINT8, INT16, UINT16, INT32, UINT32;

• Invert byte order: Inverts the order of the bytes in the created string.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: A vector of binary formatted strings.

Output • n: The output number vector; The width of the output is the same of the input width.

Description

Example:

Data Type: SINGLE

Invert byte order: Yes

Input string vector: [3f 80 00 00, 43 5f 54 7b, 40 8c cc cd]

Output numeric vector: [1.0, 223.33, 4.4]

Page 18: Communication & Strings Blockset

STRconst

Purpose Generate a constant string (object)

Parameters • Const String: A single-quoted text that represent the constant output string. More single-quoted text separated by a comma, generate an output string vector;

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • none.

Output • str: The output string vector; The width of the output is given by the number of comma-separated single-quoted text in the string parameter.

Description

Example:

Const String: ‘const’, ‘abba’, ‘0010’

Output string vector: [const, abba, 0010]

Page 19: Communication & Strings Blockset

STRtoCStr

Purpose Convert a string object in a C string

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: Whatever string object.

Output • chr*: A pointer to a permanent memory zone where the string is stored. Use this area with care. The string could be not null-terminated, so do not apply C-string function without taking into account the real length of the string.

• sz: The size of memory reserved to the string (the length of the string).

Description

Example:

The two outputs are the address of the memory containing the two strings. These address may be used by another block to process a string output and, for example, translate the content of the string. The second output is the length of the two strings and the length of the allocated memory. The address and the length of the string is permanent only for the actual simulation step, and it may change from one simulation step to the next one.

Page 20: Communication & Strings Blockset

STRfromCStr

Purpose Convert a C string in a string object

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • chr*: A pointer to a permanent memory zone where the string is stored. The string could be not null-terminated, because the real end of the string is given by the size port of the block.

• sz: The size of memory reserved to the string (the length of the string).

Output • str: A string object containing the input string.

Description

Example:

The two input channels of the first port are the address of the memory containing the two strings. These address may be generated by a custom block (see customizable block section). The second input port is the length of the two strings and the length of the allocated memory. The address and the length of the string must be permanent only for the actual simulation step, and it could change from one simulation step to the next one.

Page 21: Communication & Strings Blockset

STRlen

Purpose Compute the length of a string

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The input string vector;

Output • len (int32): The output length vector; The number of elements of the output vector is the same number of elements of the input vector.

Description

Example:

Input numeric vector: ‘const’, ‘abba’, ‘0010’

Output numeric vector: [5, 4, 4]

Page 22: Communication & Strings Blockset

STRmem

Purpose It’s the string equivalent of the discrete memory block. It stores the input for the next simulation step.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The input string vector;

Output • str: The output string vector, relative to the last simulation step. In the first simulation step the output of the block is a null string.

Description

Note: Actually there’s no way to set the output of the block in the first simulation step.

Page 23: Communication & Strings Blockset

STRcksum

Purpose It’s the string equivalent of the discrete memory block. It stores the input for the next simulation step.

Parameters • Check Type: (8 bit Sum/16 bit Sum): Selection of the checksum type to be computed

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The input string vector;

Output • chk: A vector of uint8 (8-bit checksum) or uint16 (16-bit checksum) corresponding to each input string.

Description

The checksum is the sum (8-bit) of the binary values corresponding to each byte of the string, or the sum (16-bit) of the binary values corresponding to each word (2 bytes) of the string. In the second case, the string must contain a pair number of bytes.

Example:

Page 24: Communication & Strings Blockset

STRwriteBin

Purpose Create a string binary formatting the input number into string

Parameters • Format String: The binary format of the data into the string

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • n (1..n): the nth input port contains the data to write to the n-th format element of the output data.

Output • str: The binary formatted string.

Description

The width and the type of the input port is dependant of the format string argument, as reported in the Binary Format Page.

Example:

Format String: 2i1,2u4,3d8,1i1 (i.e.: 2 1-byte integer, 2 4-byte unsigned integer, 3 8-byte double, 1 1-byte integer)

Cautions:

Sometimes, Simulink forgets to saving the modifications of the Format String or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 25: Communication & Strings Blockset

STRreadBin

Purpose Scan a binary string interpreting the content as described in the argument Format String

Parameters • Format String: The binary format of the data into the string

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The binary formatted string.

Output • flag: A flag that indicated the completion (successful or not) of the read operation (0: not completed or not enabled, 1: read complete).

• n (1..n): the output port that contains the data read from the input data. The nth output port contains the read data relative to the n-th format element.

Description

The width and the type of the input port is dependant of the format string argument, as reported in the Binary Format Page.

Example:

Format String: 2i1,2u4,3d8,1i1 (i.e.: 2 1-byte integer, 2 4-byte unsigned integer, 3 8-byte double, 1 1-byte integer)

Cautions:

Sometimes, Simulink forgets to saving the modifications of the

Page 26: Communication & Strings Blockset

Format String or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 27: Communication & Strings Blockset

STRreadFixFmt

Purpose Translate a fix formatted string into a vector of numbers. This block has been generated specifically to read Flight Simulator's formatted data string, but it can be adapted to many fix formatted string interpretation problems.

Parameters • Format String: The format string is structured as follows:

o each field is contained into a couple of '%' chars.

o each field is structured as follows: - the number of characters that is assigned to this field in the input string;

o the type of the input data;

o the size of the input data;

o each field not contained in the '%' delimitators is considered as a simple string, that should be found in each input string;

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The fix formatted string to translate.

Output • flag: A flag that indicated the completion (successful or not) of the read operation (0: not completed or not enabled, 1: read complete).

• n (1..n): the output port that contains the data read from the input data. The nth output port contains the read data relative to the n-th format element.

Description

Remarks:

About the format string, for example, to read a string like this:

"#DATA:123.19,1010.1,23.1*"

you have to use a format string as follows:

"#DATA:%6d4%,%6d8%,%4d8%*"

In this example format string:

1. the format %6d4% means a field of 6 chars that should be translated into a single precision float (double of 4 bytes);

2. the format %6d8% means a field of 6 chars that should be translated into a double precision float (double of 8 bytes);

3. the format %4d8% means a field of 4 chars that should be translated into a double precision float (double of 8 bytes);

Page 28: Communication & Strings Blockset

The decimal point inside the field should be taken into account in the evaluation of the field length. In your case, the string to read is something like this: "_00594-003+000357000000_" The format string is the following: "_%5i4%%4i4%%4i4%%3i4%%3i4%%3i4%_" Example:

Cautions:

Sometimes, Simulink forgets to saving the modifications of the Format String or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 29: Communication & Strings Blockset

STRwriteFmt

Purpose This function creates a formatted string (function analogue to the standard-C sprintf function).

Parameters • Format String: A format control string that defines how data is organized in the output string. The format specification follows the same rules as the C-standard sprintf function, as described in the string format page.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • in (1..n): The input element to be formatted into the string. In this block, even strings are accepted as input values. The number of ports is in the same number of the type of argument. See description for more details.

Output • str: The formatted output string.

Description

The type sequence of input port is predetermined, following the order fixed by the following list:

1. DATA_DOUBLE, 2. DATA_SINGLE, 3. DATA_INT, 4. DATA_HEX, 5. DATA_STRING, 6. DATA_CHAR

Then, if you have in you format string an integer, a string and two double (it doesn’t matter the effective sequence of format tokens), the sequence of input port will be: 1) double type port with width = 1, 2) integer type port with width = 1, 3) string type port with width = 1. Example:

VERY IMPORTANT NOTE: In the example, even if STRwriteFmt 2nd and 3rd input are both int32 type, the 2nd input is an hexadecimal while the 3rd input is a string type. Inverting types may cause a major memory protection error.

Page 30: Communication & Strings Blockset

Cautions:

Sometimes, Simulink forgets to saving the modifications of the Format String or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 31: Communication & Strings Blockset

STRreadFmt

Purpose This function translates a formatted string (function analogue to the standard-C sprintf function).

Parameters • Format String: A format control string that defines how data is organized in the incoming string. The format specification follows the same rules as the C-standard sprintf function, as described in the string format page.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string to be translated.

Output • flag: A flag that indicated the completion (successful or not) of the read operation (0: not completed or not enabled, 1: read complete).

• out (1..n): The output element read from the string. In this block, even strings may be presented as output values. The number of output ports is in the same number of the type of argument.

Description

The type sequence of output port is predetermined, following the order fixed by the following list:

1. DATA_DOUBLE, 2. DATA_SINGLE, 3. DATA_INT, 4. DATA_HEX, 5. DATA_STRING, 6. DATA_CHAR

Then, if you have in you format string an integer, a string and two double (it doesn’t matter the effective sequence of format tokens), the sequence of output port will be: 1) double type port with width = 1, 2) integer type port with width = 1, 3) string type port with width = 1. Remarks: While the C-language fscan function is not able to discriminate a sequence of input string, ReadFmt look first for the token end (in the example that follows, a ‘;’, and then translates the token, being so able to distinguish two or more successive strings or a string from successive numbers.

Example:

Page 32: Communication & Strings Blockset

The output port sequence is: a single, the hex and two strings.

Cautions:

Sometimes, Simulink forgets to saving the modifications of the Format String or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 33: Communication & Strings Blockset

STRleft

Purpose This block extracts a left portion of the string, for the indicated length.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string from which the characters are extracted

• nLeft: The number of characters to extract from the left of the string.

Output • str: A string containing a copy of the specified range of characters. Note that the returned string may be empty;

Description

Remarks:

This block works also for string vector arguments. The nLeft input is applied cyclically on the input strings.

Example:

Input string: [sample, oracle, virtuality]

Input nLeft: [3, 5]

Output vector: [sam, oracl, vir]

Page 34: Communication & Strings Blockset

STRright

Purpose This block extracts a right portion of the string, for the indicated length.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string from which the characters are extracted

• nRight: The number of characters to extract from the right of the string.

Output • str: A string containing a copy of the specified range of characters. Note that the returned string may be empty;

Description

Remarks:

This block works also for string vector arguments. The nRight input is applied cyclically on the input strings.

Example:

Input string: [sample, oracle, virtuality]

Input nRight: [3, 5]

Output vector: [ple, racle, ity]

Page 35: Communication & Strings Blockset

STRmid

Purpose This block extracts a mid portion of the string, starting from a given point, for the indicated length.

Parameters • Mode: application mode. Select an option from the following list:

o apply one per string: apply the n-th element of the nLeft and nNum input vector to the n-th element of the string input vector;

o apply all on each string: apply all the element of the nLeft and nNum input vector to each element of the string input vector;

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string from which the characters are extracted

• nLeft: The position from which to extract the string.

• nNum: The number of characters to extract from the nLeft character of the string.

Output • str: A string containing a copy of the specified range of characters. Note that the returned string may be empty;

Description

Example:

Mode: Apply one per string

Input string: [sample, oracle, virtuality]

Input nLeft: [2, 3]

Input nNum: [2, 4]

Output vector: [mp,cle]

Example:

Mode: Apply all on each string

Input string: [sample, oracle, virtuality]

Input nLeft: [2, 3]

Input nNum: [2, 4]

Output vector: [mp,ple,ac,cle,rt,tual]

Page 36: Communication & Strings Blockset

STRadd

Purpose This block sum the content of two or more strings, adding, if requested, a string separator or terminator.

Parameters • Number of string vector to sum: Number of block input port

• Separator character: The character, if any, to be used as separator between strings. Special characters are (still) not allowed. Max one character allowed.

• Terminator string: The character, if any, to be used as terminator of the output string. Special characters are (still) not allowed. One or more character allowed.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str (1..n): The string to sum

Output • str: The resulting string

Description

Remarks:

This block works also for string vector input. The separators and terminators are applied even for each element of input vectors.

Example:

Number of input vectors: 2

Separator character: -

Terminator string: **

Input string 1: [sample, oracle, virtuality]

Input string 2: [testing, simulink]

Output vector: [sample-oracle-virtuality-testing-simulink**]

Cautions:

Sometimes, Simulink forgets to saving the modifications of the strngs or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 37: Communication & Strings Blockset

STRprintf

Purpose Print the input string in the Matlab Command Window

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string vector to print

Output • none

Description

Each sample time, the input string is printed in a row, separating each element of the vector by a comma. Non printable chars are not translated to printable chars before being sent to the Matlab Command Window.

Page 38: Communication & Strings Blockset

STRtoFile

Purpose Send the input string to a file

Parameters • Filename: Name of the file (in the Matlab current directory) where to save the strings

• Separator character: The character, if any, to be used as separator between strings. Special characters are (still) not allowed. Max one character allowed.

• Terminator string: The character, if any, to be used as terminator of the output string. Special characters are (still) not allowed. One or more character allowed.

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string vector to save on the file

Output • none

Description

Remarks:

This block works also for string vector input. The separators and terminators are applied even for each element of input vectors.

Example:

Number of input vectors: 2

Separator character: -

Terminator string: **

Input string 1: [sample, oracle, virtuality]

Input string 2: [testing, simulink]

String saved on the file: [sample-oracle-virtuality-testing-simulink**]

Cautions:

Sometimes, Simulink forgets to saving the modifications of the strings or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 39: Communication & Strings Blockset

STRfind

Purpose Search a substring in a string

Parameters • String to find: The substring to search

• Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string vector where substring should be searched

Output • pos: The (0-based) position vector of the substring in each element of the input string vector. If string is not found, the resulting output is –1;

Description

Remarks:

This block works also for string vector input. Resulting vector reports the position of the found string for each input vector element.

Example:

String to find: ‘IO’

Cautions:

Sometimes, Simulink forgets to saving the modifications of the Format String or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 40: Communication & Strings Blockset

STRextFind

Purpose Search an input substring in a string

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • str: The string vector where substring should be searched

• find: The substring to search. Only 1-element (non vector) string allowed. Even binary string accepted.

Output • pos: The (0-based) position vector of the substring in each element of the input string vector. If string is not found, the resulting output is –1;

Description

Remarks:

This block works also for string vector input. Resulting vector reports the position of the found string for each input vector element.

Example:

Cautions:

Sometimes, Simulink forgets to saving the modifications of the Format String or the number of input or output port. To override this problem, disable the link of the block to the library (right-click on the block and then select the popup menu item: Link Options→Disable).

Page 41: Communication & Strings Blockset

STRcustomIN

Purpose A customization sample that use the output of the STRtoCStr block.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

Input • chr*: A pointer to a permanent memory zone where the string is stored. The string could be not null-terminated, because the real end of the string is given by the size port of the block.

• sz: The size of memory reserved to the string (the length of the string).

Output • none

Description

This is a simple block that read the input string. The relative C-language S-function is contained in the installation directory and its name is “STR_CustomFun.c”. In this S-function, the mdlUpdate function use the input pointer and length of the string to allocate some memory and print it on the Matlab Command Window. See the source code of the S-function for more details.

Page 42: Communication & Strings Blockset

STRcustomXBOW

Purpose A customization sample that generates a C-string pointer (and string length) that (approximately) simulates the output of the Crossbow IMU sensor.

Parameters • Sample Time: Indicates if the sample time should be inherited from the preceding blocks or should be defined independently (but it should be a multiple of the base sample time).

• DMU Mode:

o VG Mode: In voltage (VG) mode, the analog sensors are sampled and converted to digital data with 1 mV resolution. The digital data represents the direct output of the sensors. The data is 12-bit, unsigned. The value for each sensor is sent as 2 bytes in the data packet over the serial interface. A single data packet can be requested using a serial poll command or the DMU can be set to continuously output data packets to the host.

o Scaled sensor mode: the analog sensors are sampled, converted to digital data, temperature compensated, and scaled to engineering units. The digital data represents the actual value of the quantities measured. A calibration table for each sensor is stored in the DMU non-volatile memory. A single data packet can be requested using a serial poll command or the DMU can be set to continuously output data packets to the host. The data is sent as signed 16-bit 2’s complement integers. In this mode, the DMU operates as a six-axis measurement system.

• G Range: defines the maximum absolute value of detectable g (acceleration measured in g); 1g (put “1” in the control) is a standard g maximum value.

• Angular rate range: defines the maximum absolute value of detectable angular rate range; 150°/sec (put “150” in the control) is a standard value.

Input • data:

o VG mode: A double value 10 element vector containing the actual value (in the order: roll and pitch angle, roll, pith and yaw angle rate, the x-y-z accelerations, the temperature and time);

o Scaled sensor mode: A double value 8 element vector containing the actual value (in the order: roll, pith and yaw angle rate, the x-y-z accelerations, the temperature and time);

Page 43: Communication & Strings Blockset

and time);

Output • chr*: A pointer to a permanent memory zone where the string is stored. The string could be not null-terminated, because the real end of the string is given by the size port of the block.

• sz: The size of memory reserved to the string (the length of the string).

Description

This is a simple block that output a C-String. The relative C-language S-function is contained in the installation directory and its name is “STR_CustomFunIO.c”. In this S-function, the mdlUpdate function translates the physical values into scaled values and then compose the output string. See the C-function for more details.

Page 44: Communication & Strings Blockset

Format Specification Fields A format specification has the following form:

% [width] [l] type

The format argument specifies the interpretation of the input and can contain one or more of the following: • White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space

character causes the block to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.

• Non–white-space characters, except for the percent sign (%). A non–white-space character causes the block to read, but not store, a matching non–white-space character. If the next character in the input buffer does not match, the block terminates the read, sending an error.

• Format specifications, introduced by the percent sign (%). A format specification causes the block to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.

The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in input buffer; the matching characters in input buffer are scanned but not stored. If a character in input buffer conflicts with the format specification, the block terminates, and the character is left in input buffer as if it had not been read.

When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.

An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.

Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a double, or an integer.

The simplest format specification contains only the percent sign and a type character (for example, %d). The percent sing is always intended as a format-control character, except in the case that another percent sing follows. Then, to specify that a percent-sign character is to be input, use %%.

Page 45: Communication & Strings Blockset

Format

The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number.

Table 1 Type Characters for format scan functions

Character Type of Input Expected

d Decimal integer.

i Decimal, hexadecimal, or octal integer.

o Octal integer.

x Hexadecimal integer.

e, E, f, g, G Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent (“e” or “E”) followed by an optionally signed integer value.

Format Width Specification

width is a positive decimal integer controlling the maximum number of characters to be read from input buffer. No more than width characters are converted and stored at the corresponding argument. Fewer than width characters may be read if a white-space character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.

The optional prefixe l indicate the “size” of the argument (long or short, single-byte character or wide character, depending upon the type character that they modify). This format-specification character is used with type characters in format block functions to specify interpretation of arguments as shown in the Table 2. The type characters and their meanings are described in Table 1

Table 2 Size Prefixes for format block Format-Type Specifiers

To Specify Use Prefix With Type Specifier

double l e, E, f, g, or G

long int l d, i, o, x, or X

Page 46: Communication & Strings Blockset

Binary Format Specification Fields A format specification has the following form:

[spec1],[spec2],...,[specn]

Each specification is associated to an output port that is created soon after the specification string is read and the block panel is closed.

Each specification is structured as follows:

[n][Type][bytes]

where: • n is the width of the relative port, i.e. the number of consecutive data to read from

serial port

• Type: is one of the following: d, i, u, b, where:

o d: indicates a double

o i: integer

o u: unsigned

o b: boolean

• bytes: indicate the number of bytes that compose the incoming data

Example:

• 2i1,3u4,1d8 means: 3 ports, with respectively 2 signed bytes, 3 unsigned integers, 1 double

• 6i2,u2,d4,d8 means: 4 ports, with respectively 6 signed words, 1 single float, 1 double float

Page 47: Communication & Strings Blockset

Blockset Application Example

In this example (that is contained in the blockset, STRnLAN_Test.mdl) , we want to simulate the output of a Crossbow IMU (that normally works on a serial port) on a LAN port. The LAN Setup block is configured to open a server connection on port 8000, to accept connections only from the local PC (connection mask: 127.0.0.1). The Noise block generates 10 random double values, that are traslated from the Crossbow IMU Simulation block (whose source is available free in the blockset) as it was the physical values sensed by the IMU. A classic C-String is generated as the output of the IMU block and translated into a String obkect from the STRfromCStr block. A string object is a non-official type of Simulink, created for the purposes of this blockset, and is propagated from a block to another using its handle, that is an int32 value. This string is then sent to the LAN Write block, that simply write it in the TCP server port, if a client is connected. The RT block is to maintain the real-time operations, and it can be found on the related page.

In this other example, the message sent by the previous example is read from the LAN port and interpreted. Here the LAN Setup block opens a client connection to the port 8000 opened by the previous block. Every string read is sent as output of the LAN read block. The STRadd block add the read string to the string remained from the last simulation step, stored by the STRmem block. The resulting string is then scanned to search the position of the header of the message, that is the binary char 0xff. The block that perform the search operation is a macro block and is detailed in a next figure. When found the header, the STRright block extract (cut the right pos bytes from the string) all the chars that follows the header and then, if the length of the string (extracted from the STRlen block) is greater than 22 (22 is the fixed length of an IMU message) an IF action subsystem (whose content is detailed next) extracts data (only the 16bit values, not translated) from the string and outputs to a scope. The remaining string is sent to the STRmem block to be used in the next simulation step.

Page 48: Communication & Strings Blockset

The header position search block is quite easy. The string input is sent to the first port of the STRextFind block that look for the position of the string that receives in the second port. The string to search is obtained converting the byte 0xff = 255 into a 1-char string. The position of the found string is then subtracted to the total length of the string to obtain the number of bytes, from the right of the string, that should be extract to cutoff all bytes before the header. This number is the output of the block and then sent to the STRright block of the main model.

The IF action subsystem of the main block extracts data from the received string. The input string is sent first to the STRreadBin block, that decompose the message into a byte (the 0xff char), a vector (10) of short integers (the encoding of the double values made by the Crossbow block) and the final characted, the checksum. The input string is sent also to a STRright block to remove the left 22 chars from the string, so obtaining the remaining string to be stored for the next simulation step. The input string is sent finally also to a STRmid block, to extract the middle string from the 2nd (1 is because strings are 0-based referenced) to the 21st character (20 for 0-based), that is the string that in the Crossbow IMU is used to compute the checksum. Then, the checksum is calculated using the STRchkSum block, and the result is compared with the checksum byte extracted from the STRreadBin block. If the comparison is successful the output of the STRreadBin block is propagated to the subsystem output, using another IFactionSubsystem that simply propagates the input to the output if the activating condition is valid.

Page 49: Communication & Strings Blockset

Information and Warranty for the End User I must remember to all the users of this blockset that this is a product not covered by any warranty abut its working performances and characteristics, because it's completely free. All the material and the manual has been realized by myself independently from any contractual constraint with any University or company, then the intellectual property is exclusive of the author (me).

The present library is actually under development and more blocks will be added. The notice from the users of any problem will be appreciated and recorded and any collaboration will be mentioned in the final version of the blockset.

Installation of the blockset At the start of this page, a link to the file COMMSTR.zip allows the download of the COMMSTR blockset library. The zip file has been memorized without any reference to an installation directory, then the directory will be selected from the end user at the installation time.

After the start of the Winzip (or equivalent) program, press the button "Extract" to extract all files in the desired directory. Preferably, select a folder (or create a new folder) insied the <MatlabDir>\work directory.

Page 50: Communication & Strings Blockset

Installation Directory selection window

After the completion of the setup, add the path of the selected directory utilizing the menu item File-->Set Path...

Now the installation is complete. To open the COMMSTR Blockset library is sufficient to type in the Matlab command window the command "COMMSTR". A window containing all the available blocks will be displayed.

Acknowledgements (also for RS232 blockset) Many thanks for their suggestions and requests:

• Nelson Rojas Mendelewski, from Chile, for his suggestions and requests about many topics.

• Juan Francisco Blanes Noguera, from Valencia (Espana), for his requests about upgrade of the RS232_Read_Format block.

• Pramlia Rani, from Vanderbilt University (Nashville, Tennessee - USA) that suggested me the creation of the binary blocks.

Many thanks for their debugging activity:

• Hugo Montenegro, from Argentina, for his debugging activity on the RS232_Wait_Buffer_Synch block.

• Jens Haecker, from Institute for Statics and Dynamics of Aerospace Structures (ISD), University of Stuttgart (Germany), for his requests about the RS232_Format_Read for his debugging on the formatted write and read.

• Lutz Evert, from FH-Bielefeld, Germany, for his debugging activity.

Many thanks for their encouragement and contributes:

• Jeff Renaud and Charles Bienvenue from l’Université du Québec, Montreal, Canada

• Ciamak Abkai, from Germany

• Cord Elias, from Mathworks Gmbh, Germany

• Victor Polidoro, from MIT, Boston, USA

• Gordon Strickert, from Germany

Page 51: Communication & Strings Blockset

• Stefan Thomas, from Wien, Austria

• Juan Francisco Blanes Noguera, from Escuela Universitaria de Informática, Valencia, Spain

• Peter How, from MIT, Boston, USA

• Tarif Erf, from Turkey

• Phaisit Chewputtanagul, from I don’t know where

• Thorsten Knappenberger, from Germany

• Sithabiso Madlala, from Zaire

• Shu-An Lee, from Taiwan

• Wesley Yu, from Republic of China

• Jacy Montenegro Magalhaes Neto, from Instituto Militar de Engenharia, Rio de Janeiro, Brasil