RS-232 Composite Drivers

This topic describes the components that make up the RS-232 composite drivers, and how you can create a model using these drivers. These drivers perform RS-232 asynchronous communications.

The Simulink® Real-Time™ software provides composite drivers that support the target computer (main board) serial ports.

These drivers distribute the functionality of the device across several subsystems and blocks. For most RS-232 requirements, you can use these RS-232 drivers as they are implemented. However, if you must customize the Simulink Real-Time RS-232 drivers, the composite nature of the drivers enables you to do so.

Adding RS-232 Blocks

You add RS-232 subsystem blocks to your Simulink model when you want to use the serial ports on the target computer for serial I/O.

After you create a Simulink model, you can add Simulink Real-Time driver blocks and configure those blocks. The following procedure describes how to use the serial ports on the target computer for I/O with the composite drivers.

Before you start, decide what COM port combinations you want to use. The example has you configure the Baseboard Send/Receive block. To configure this block, first select serial port pairs. This parameter specifies the ports for which you are defining transmit and receive. You have a choice of the following:

  • Com1/none

  • Com2/none

  • Com1/Com3

  • Com2/Com4

  • none/Com3

  • none/Com4

  • Custom

If you select either the Com1/Com3 or Com2/Com4 pair, check that the port pair shares an interrupt. If the port pair does not share an interrupt, you cannot use the two ports as a pair.

Alternatively, you can define a Custom port pair. A Custom port pair is one that does not match the existing combinations of port pairs. When you select Custom, the dialog box allows you to configure your own port pair. For example, you can set the IRQ and two addresses for the port pair. If one of the ports is not used, set that address to 0.

Normally, the ports are set to the following:

  • COM1 — 0x3F8, IRQ 4

  • COM2 — 0x2F8, IRQ 3

  • COM3 — 0x3E8 (if present), IRQ 4

  • COM4 — 0x2E8 (if present), IRQ 3

In a Custom port pair, either set one or both ports of the pair to addresses other than these conventions, or assign a different IRQ value. Some boards allow you to set the IRQ numbers independently.

If you select the port pairs Com1/Com3 or Com2/Com4, you must include one Send/Receive subsystem block in the model. If you use COM1 and COM2, or COM1 and a custom port pair, you must include two Send/Receive blocks in the model.

The following example shows two models, one that uses a standard Com1/Com3 port pair, and one that uses custom port pairs:

  1. In the Command Window, type

    slrtlib

    The Simulink Real-Time driver block library opens.

  2. Double-click the RS-232 group block.

    A window with blocks for RS-232 composite drivers opens.

    Alternatively, you can access the Simulink Real-Time block library from the Simulink Library Browser. In the Simulink Editor, on the Real-Time tab, from the Prepare section, click Library Browser. In the left pane, double-click Simulink Real-Time, and then click RS232.

  3. Drag an ASCII Encode block to your Simulink model. This block encodes input for the RS-232 Send Receive block.

  4. Configure this block.

  5. Drag an ASCII Decode block to your Simulink model. This block decodes output from the RS-232 Send Receive block.

  6. Configure this block.

  7. Double-click the Mainboard group block.

  8. Depending on your port pair configuration, drag one or two Baseboard RS-232 Send/Receive blocks to your Simulink model.

  9. Double-click the Baseboard RS-232 Send/Receive block.

  10. Configure this block. Note the following Parameter group values:

    • When you select Board Setup, make sure that the Configuration value is consistent with your RS-232 serial port configuration.

    • When you select Receive Setup, for each channel, set the value of the Receive Sample Time parameter to a sample time value faster than the data being sent. Do not leave this value at -1. Set this parameter for all channels, including channels that you are not using; otherwise, you receive an error when generating code for the real-time application.

  11. Add a Pulse Generator block and a target Scope block.

  12. Configure the Pulse Generator block so that its Pulse type is Sample based.

    The dialog box changes to display a Sample time parameter. Enter a Sample time that is slower than the one you set for Receive Setup.

  13. From the Simulink Library Browser, select Sinks. Depending on your configuration, drag one or more Terminator blocks to your model. To suppress unused port messages, connect this block to the unused RCV1 port.

    From the Simulink Library Browser, select Sources. Depending on your configuration, drag the Ground block to your model. To suppress unused port messages, connect this block to the unused XMT3 port.

    Your model can use one block or two.

    The single-block model uses the Com1/Com3 port pair:

    The two-block model uses two sets of Custom port pairs:

  14. Double-click a Baseboard RS232 Send Receive block. To configure the ports on the target computer for this board, enter values.

    Note

    This dialog box changes depending on the Parameter group selection.

    For example, if the Parameter group is Board Setup and the target computer port is connected to COM1/COM3, your Send Receive block dialog box looks like this figure.

    For more information on entering the block parameters, see RS-232 Send/Receive.

  15. Click OK. The Send Receive block dialog box closes.

Your next task is to build and run the real-time application.

Building and Running the Real-Time Application

The Simulink Real-Time software and Simulink Coder™ create C code from your Simulink model. You can then use a C compiler to create executable code that runs on the target computer. This topic assumes that you know how to configure your model to create a real-time application. See Build and Download Real-Time Application by Using Run on Target.

After you have added the RS-232 blocks for the main board to your Simulink model and configured your model, you can build your real-time application.

In the Simulink Editor, on the Real-Time tab, click Run on Target.

Simulink Real-Time RS-232 Reference

The Simulink Real-Time software supports RS-232 communication with driver blocks in your Simulink model.

Using the FIFO Read Blocks

There are three kinds of FIFO Read blocks: FIFO Read, FIFO Read HDRS, and FIFO Read Binary. To develop your model, use the following guidelines:

  • 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 ends with 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 Read 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 and FIFO Read 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 character vector like this:

    <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). 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 several different messages, each beginning with a different fixed character vector, use the FIFO Read HDRS block. For example, a digital multimeter connected through an RS-232 port sends 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].

    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.

Signal Data Types

Signals between blocks in composite drivers can be one of several basic data types, 8-bit, 16-bit, and 32-bit. These types are structures.

The 8-bit data types are NULL-terminated character vectors that are represented as Simulink vectors. The width is the maximum number of characters that can be stored. In the following figure, M is the actual set of stored characters and N is the maximum number of characters that can be stored. This figure illustrates 8-bit int NULL-terminated and 8–bit uint NULL-terminated data types.

This character vector has 11 characters terminated with a NULL byte (0). This data type cannot contain a NULL byte as part of the real data.

The 16-bit and 32-bit data types use the first element of the vector as a count of the valid data. In the following figure of a 16-bit data type, C is the count of the valid data, N is the width of the vector. This figure illustrates count + 16-bit int and count + 16-bit uint data types. It also applies to count + 32-bit int and count + 32-bit uint data types.

These serial blocks interpret each entry in the vector as a single character. The low-level Send block writes the low-order byte of each entry to the UART. The 16-bit and 32-bit data types allow the embedding of 8-bit data values, including 0. The 8-bit data type is most useful with the ASCII Encode and Decode blocks. The 16-bit and 32-bit data types are most useful for binary data streams.

Handling Zero Length Messages

Usually, you configure a FIFO read block of your model serial I/O to execute faster than the model receives data. Doing so prevents the receive FIFO buffer from overflowing. However, you must also configure your model to deal with the possibility that a FIFO read block does not have a message on its output.

Receive FIFOs can have too few characters for a FIFO read operation. A model that receives serial I/O can have a FIFO read block that executes in this situation. This condition causes a FIFO read block to perform one of the following, depending on how you configure the behavior:

  • Return the last message it received

  • Return a zero length message

The Simulink Real-Time library of composite serial drivers has three FIFO read blocks: FIFO Read HDRS, FIFO Read Binary, and FIFO Read. For the FIFO Read HDRS or FIFO Read Binary blocks, you configure this behavior with the Output behavior parameter. The FIFO Read block returns either a new message or a zero length message.

To execute model code only if a new message arrives, check the first element of the returned vector, depending on the character vector data type:

  • In the 8-bit data type, the returned character vector is NULL-terminated. Therefore, if the first element is 0, the character vector has zero length and the FIFO read did not detect a new message.

  • In the 16-bit and 32-bit data types, the first element is the number of characters in the character vector. This value is 0 if the FIFO read did not detect a new message.

If the message has nonzero length, enable a subsystem to process the new character vector; otherwise, do not process it.

Controlling When You Send a Message

You can use the structure of both serial data types (Signal Data Types) to control when a message is sent. In both cases, a 0 in the first position indicates an empty character vector.

  • 8-bit data types — A value of 0 in the first position is the NULL terminator for the character vector.

  • 16-bit and 32-bit data types — The first position is the number of characters that follow.

If you connect an empty character vector to the XMT port on one of the send/receive subsystems, no characters are pushed onto the transmit FIFO. You can get this empty character vector by using one of the following:

  • To send a specific character vector occasionally, use the Product block to multiply the entire character vector by either 0 or 1. In this case, the 0 or 1 value becomes a transmit enable. To optimize this operation, use a Demux block to extract the first element. Multiply just that element by 0 or 1, then use the Mux block to combine it again.

  • Use a Manual Switch, Multiport Switch, or Switch block. Configure the blocks for two ports to choose between different messages, with one of the choices a vector of 0 values. The Switch block only chooses between vectors of the same width. However, because the character vector length does not use the whole vector, you can pad your data to the same width with 0 values.

See Also

| | | | | | | | | | |