Documentation

FIFO Read/Write (Composite)

FIFO Read/Write

Library

Simulink® Real-Time™ Library for RS-232

Description

This section describes the FIFO Read and Write blocks. Of particular note are the FIFO Read blocks, of which there are three. Use the following guidelines when using these blocks:

  • Simple data streams — Use the FIFO Read block to read simple data streams. An example of a simple data stream is one that has numbers, separated by spaces, and that is terminated by a new-line character. The FIFO Read block is a simple block that can easily extract these numbers.

  • More complicated data streams — Use the FIFO Read HDRS and FIFO Binary blocks for more complicated data streams. A more complicated data stream can be one that contains headers, messages of varying lengths, or messages without specific terminators. A message header consists of one or more character identifiers at the beginning of a message that specify what data follows. ASCII messages normally have a variable length and a terminator. Typically, the messages of a particular device use the same predefined terminator. Binary messages are normally of fixed length without a specific terminator.

    The FIFO Read HDRS or FIFO Binary blocks are also useful to work with devices that can send different messages at different times.

The three FIFO read block types need their input to be of type serialfifoptr, which is output from F type Send Receive subsystems.

The following are examples of when you can use the FIFO read block.

  • For an instrument that sends a string like the form

    <number> <number> ... <CR><LF>

    use the simple FIFO Read block to read the message. Configure the FIFO Read block Delimiter parameter for a line feed (value of 10). You should then connect the output to an ASCII Decode block with a format that separates the numbers and feeds them to the output ports.

  • For an instrument that can send one of a number of different messages, and each message begins with a different fixed string, use the FIFO Read HDRS block. For example, a digital multimeter connected through an RS-232 port, might send a voltage reading and an amp reading with messages of the following format:

    volts <number> <CR><LF>
    amps <number> <CR><LF>

    Configure the FIFO Read HDRs block Header parameter for the volts and amps headers, in a cell array: {'volts', 'amps'}. Also configure the Terminating string parameter for carriage return (13) and line feed (10): [13 10].

    You should then connect the output to multiple ASCII Decode blocks, one for each header and message. See the xpcserialasciitest and xpcserialasciisplit models in xpcdemos for examples of how to use this block in a model.

  • For an instrument that sends a binary message, you typically know the length of each full message, including the header. Configure the FIFO Read Binary block Header parameter for the headers of the message, in a cell array, and the Message Lengths parameter for the message lengths. See the xpcserialbinarytest and xpcserialbinarysplit models in xpcdemos for further examples of how to use this block in a model.

The FIFO Read block is the read side of a FIFO read/write pair. The block functions in two modes, set using the Read to delimiter check box.

  • If you select the Read to delimiter check box, the block only reads elements if the specified delimiter has been written to the write side of the FIFO (the FIFO Write block). If the delimiter is found, the block returns elements up to and including the delimiter in the output vector. If the delimiter is not found, the block returns a zero length vector, as determined by the data type. (If you have a zero length vector, you might want to have your model perform a particular operation, or ignore the case.)

  • If you clear the Read to delimiter check box, the block returns a number of elements between Minimum read size and the smaller of the number of elements currently in the FIFO and Maximum read size.

    Tip   You usually select the Read to delimiter check box when performing ASCII reads and clear it when performing binary reads.

FIFO Read Block Parameters

Maximum read size

Specify the maximum number of characters that you will ever expect to be returned by this block. The resulting vector size will be one more than this maximum number of characters. This block indicates the number of characters being returned using the extra element as:

  • A null terminator for the 8-bit data types

  • The character count for the 16- and 32-bit data types

Be sure to enter a large enough number. If this number is too small, the block might not be able to return anything. For example, if you enter a value 10, but on execution the FIFO contains 11 characters plus the null terminator, the block will not return any characters. On the other hand, if it contains 5, the block returns 5 characters plus the null terminator.

If you select the parameter Max and Min read size ports, the block interprets the value input on this port to be the maximum number of characters to return. The actual maximum number of characters to return is the smaller of the value on the max input port or the maximum read size in the block parameters. This is mainly useful in binary mode when the Read to delimiter check box is not selected.

Minimum read size

Enter the smallest desired read size in bytes. The FIFO must contain at least this number of elements before elements will be returned. If you select the Max and Min read size ports check box, this value is superseded by the external signals.

Read to delimiter

Select this check box to enable the return of element sets that terminate with the Delimiter value. Use this parameter when working with character-based elements.

Delimiter

Enter the decimal value for an 8-bit input terminator. This parameter specifies the value on which a FIFO read operation should terminate. It works with the Read to delimiter parameter. By default, this block looks for a carriage return. It only returns characters when one is found. For reference, the decimal value of a carriage return is 13, a line feed is 10.

Output vector type

From the list, select count+32 bit int, count+32 bit uint, count+16 bit int, count+16 bit uint, 8 bit int null terminated, or 8 bit uint null terminated. This parameter specifies the output vector type. The 8-bit data types produce a null terminated string in the output vector. For 16- and 32-bit data types, the first element contains the number of elements to expect in the rest of the output vector.

Max and Min read size ports

Select this check box to enable the maximum and minimum input ports. When this check box is selected:

  • The value from the maximum input port is the maximum number of characters to be removed from the FIFO. If this number exceeds the value of Maximum read size, the block disregards the value from the maximum input port and takes the value of Maximum read size as the maximum number of characters to be removed from the FIFO.

  • The value from the minimum input is the minimum number of characters the FIFO must contain before elements can be returned. This value supersedes the value set with the Minimum read size parameter.

Enable passthrough

Select this check box to pass the maximum read input through to the passthrough output.

SampleTime

Base sample time or a multiple of the base sample time.

FIFO Write Block Parameters

Size

Enter the number of elements that can be held in the FIFO at one time. If a write operation to the FIFO causes the number of elements to exceed Size, an error occurs.

Input vector type

This parameter specifies the input vector type. From the list, select one of:

  • count+32 bit int, count+32 bit uint, count+16 bit int, count+16 bit uint — For the 16- and 32-bit data types, include as first element the number of elements to expect in the rest of the input vector. The count controls how many bytes that the block copies into the FIFO. The block does not copy the count itself into the FIFO.

  • 8 bit int null terminated, 8 bit uint null terminated — For the 8-bit data types, provide a null terminated string in the output vector. The block copies data into the FIFO up to, but not including, the null terminator.

For more information, see RS-232/422/485 Drivers (Composite).

Data present output

Select this check box to create a Boolean output that is true if data is present in the FIFO. The transmit side of the send/receive subsystem uses this output. This output is given to the Enable TX block, which enables the transmitter buffer empty interrupt.

SampleTime

Base sample time or a multiple of the base sample time.

ID

Enter a user-defined identifier for overflow messages.

Example

The following are some examples of how you can set up the FIFO Read block:

  • In the transmit side of the interrupt service routine, the maximum input port receives a value of 0 if the interrupt reason is not an empty FIFO on the board, and the FIFO size if the FIFO is empty. The minimum input port receives the constant value of 1.

    On the receive side, the typical case with ASCII data has the minimum and maximum input ports disabled. The Read to delimiter parameter check box is selected and the Delimiter parameter has the value of carriage return or line feed. The value of the Maximum read size parameter is large (along the order of the FIFO size) and the value of Minimum read size parameter is 1. In this form, the driver acts like a nonblocking read line.

  • An alternate receive-side configuration for fixed-length binary blocks of data has the value of the Maximum read size and Minimum read size parameters set to the fixed length of the block. The Read to delimiter parameter is not selected.

Was this topic helpful?