| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → xPC Target |
| Contents | Index |
| Learn more about xPC Target |
| On this page… |
|---|
This section describes the components that make up the RS-232 and RS-422/485 composite drivers, and how you can create a model using these drivers. These drivers perform RS-232 or RS-422/485 asynchronous communications.
The xPC Target software supports the target PC serial ports (main board), Quatech RS-232/422/485 devices, Diamond Systems RS-232 devices, and Commtech Fastcom: 422/2-PCI adapters with composite drivers. These drivers distribute the functionality of the device across several subsystems and blocks. For most RS-232/422/485 requirements, you can use these RS-232/422/485 drivers as they are implemented. However, if you need to customize the xPC Target RS-232/422/485 drivers, the composite nature of the target PC serial port, Quatech RS-232/422/485, Diamond Systems RS-232, and Commtech Fastcom drivers enables you to do so. See Serial Communications Support with Internal Drivers for details.
Note the following characteristics of the Commtech Fastcom: 422/2-PCI adapter boards (http://www.commtech-fastcom.com):
The Fastcom 422/2-PCI board has two independent RS-422 channels.
The Fastcom 422/2-PCI board can handle baud rates up to 1.5 megabits/second.
The Fastcom 422/2-PCI board hardware FIFO is fixed at 128 bytes for receive and transmit.
Note the following characteristics of the Commtech Fastcom: 422/2-PCI-335 and Fastcom: 422/4-PCI-335 adapter boards (http://www.commtech-fastcom.com):
The Fastcom 422/2-PCI-335 board has two independent RS-422/485 channels, the Fastcom 422/4-PCI-335 board has four independent RS-422/485 channels.
The Fastcom 422/2-PCI-335 and Fastcom 422/4-PCI-335 board can handle baud rates up to 6.25 megabits/second.
Note Many of the blocks that support the RS-232 and RS-422/485 composite drivers are common across the main board, Quatech, and Diamond Systems boards. The descriptions for these blocks are applicable for all drivers, with specific board notes as appropriate. |
You add RS-232 subsystem blocks to your Simulink model when you want to use the serial ports on the target PC, Quatech QSC-100 or ESC-100, or Diamond Systems Emerald-MM or Emerald-MM-8 serial device connected to the target PC, for I/O.
After you create a Simulink model, you can add xPC Target driver blocks and configure those blocks. The following procedure describes how to use the serial ports on the target PC 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 properly configure this block, you need to 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 choose 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 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
A Custom port pair is one where one or both ports of the pair are set to addresses other than these conventions, or one for which you want to assign a different IRQ value. Some hardware allows you to set the IRQ numbers independently.
If you choose the port pairs Com1/Com3 or Com2/Com4, you need to include one Send/Receive subsystem block in the model. If you choose to use COM1 and COM2, or COM1 and a custom port pair, you need to 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:
In the MATLAB Command Window, type
xpclib
The xPC Target driver block library opens.
Double-click the RS-232 group block.
A window with blocks for RS-232 drivers opens.

Alternatively, you can access the xPC Target block library from the Simulink Library Browser. In the Simulink window, and from the View menu, click Show Library Browser. In the left pane, double-click xPC Target, and then click RS232.
Drag and drop an ASCII Encode block to your Simulink model. This block encodes input for the RS-232 Send Receive block.
Drag and drop an ASCII Decode block to your Simulink model. This block decodes output from the RS-232 Send Receive block.
Depending on your port pair configuration, drag and drop one or two Baseboard RS-232 Send/Receive blocks to your Simulink model.
Configure this block. Note the following:
Pay particular attention to the Parameter group Board Setup entry.
When you select the Receive Setup entry, for each channel, set the value of the Receive Sample Time parameter to an appropriate sample time value that is faster than the data being sent. Do not leave this value at -1. You must set this for all channels, including channels that you are not using; otherwise, you will receive an error when generating code for the target application.
Configure the Pulse Generator block so that its Pulse type is Sample based.
The dialog changes to display a Sample time parameter. Enter a Sample time that is slower than the one you set for Receive Setup.
From the Simulink Library Browser, select Sinks. Depending on your configuration, drag and drop one or more Terminator blocks. Connect this block to the unused RCV1 port to suppress unused port messages.
From the Simulink Library Browser, select Sources. Depending on your configuration, drag and drop the Ground block. Connect this block to the unused XMT3 port to suppress unused port messages.
Your model should look similar to one of the following figures. The first figure shows a single-block model. This model uses the Com1/Com3 port pair. The second figure shows a two-block model. This model uses two sets of Custom port pairs.


Double-click a Baseboard RS232 Send Receive block. Enter values to configure the port(s) on the target PC for this board.
For example, if the target PC is connected to COM1, your Send Receive block dialog box should look similar to the following figure. Note, this is a dynamic dialog box that changes depending on the Parameter group selection.

For more information on entering the block parameters, see RS-232/RS-422/RS-485 Send/Receive (Composite).
Click OK. The Send Receive block dialog box dialog box closes.
Your next task is to build and run the target application.
The xPC Target software and Real-Time Workshop® create C code from your Simulink model. You can then use a C compiler to create executable code that runs on the target PC. This topic assumes that you know how to configure your model to create an xPC Target application. (See xPC Target Application in the xPC Target Getting Started Guide for details.)
After you have added the RS-232 blocks for the main board to your Simulink model and configured your model, you can build your target application.
Note You cannot use a serial port to communicate between the host PC and target PC with this example. You can only use COM1 if it is not already in use for host-target communications. Additionally, if COM1 and COM3 share an interrupt, you cannot use COM3 if COM1 is already in use for host-target communications. |
In the Simulink window, and from the Tools menu, point to Real-Time Workshop, and then click Build Model.
In the MATLAB Command Window, type
+tg or tg.start or start(tg)
The xPC Target software supports RS-232/422/485 communication with driver blocks in your Simulink model.
This section includes the following topics:
Signal Data Types — Describes signal data types that composite drivers support.
ASCII Encode/Decode (Composite) — (Generic) Describes encoder and decoder blocks. Encoders convert input signals for the send/receive subsystem to ASCII strings. ASCII decoders parse the string from the Send/Receive subsystem.
FIFO Read/Write (Composite) — (Generic) Describes FIFO read and write blocks.
RS232 State (Composite) — (Generic) Monitors the hardware error state information that is present in the output vector from all blocks.
RS-232/RS-422/RS-485 Send/Receive (Composite) — Provides blocks for sending and receiving.
Modem Control (Composite) — Controls the state of either or both of the RTS and DTR output lines.
Modem Status (Composite) — Reads the states of the four input modem control lines.
Signals between blocks in composite drivers can be one of several basic data types, 8-bit and 16- or 32-bit. All of these types are structures.
8-bit data types are NULL-terminated strings 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 string has 11 characters terminated with a NULL byte (0). This data type cannot contain a NULL byte as part of the real data.
16- 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 hardware Send block writes the low-order byte of each entry to the UART. The 16- and 32-bit data types allow the embedding of any 8-bit data value, including 0. The 8-bit data type is most useful with the ASCII Encode and Decode blocks. The 16- and 32-bit data types are most useful for binary data streams.
As a general rule, 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. This implies that you must configure your model to deal with the possibility that there is no message on a FIFO read block output.
Receive FIFOs might not always have enough characters to satisfy a FIFO read operation. Any model that receives serial I/O might have a FIFO read block that will execute in this situation. This will cause 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 xPC Target library of composite serial drivers has three FIFO read blocks, FIFO Read HDRs, FIFO Read Binary, and FIFO Read (described in FIFO Read/Write. For the FIFO Read HDRs or FIFO Read Binary blocks, you configure this behavior with the Output behavior parameter. The FIFO Read block always returns either a new message or a zero length message.
If you need to execute model code only if a new message arrives, check the first element of the returned vector, depending on the string data type, as described in the following. If the message has non-zero length, enable a subsystem to process the new string; otherwise, do not process it.
In the 8-bit data type, the returned string is NULL-terminated. Therefore, if the first element is 0, the string has zero length and the FIFO read did not detect a new message.
In the 16- and 32-bit data types, the first element is the number of characters in the string. This value is 0 if the FIFO read did not detect a new 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 string.
8-bit data types — A value of 0 value in the first position is the NULL terminator for the string.
16- and 32-bit data types — The first position is the number of characters that follow.
If you connect an empty string to the XMT port on one of the send/receive subsystems, no characters are pushed onto the transmit FIFO. You can get this empty string using one of the following:
If you have a single string that you want to send occasionally, use the Product block to multiply the entire string by either 0 or 1. In this case, the 0 or 1 value becomes a transmit enable. To optionally optimize this operation, you can use a Demux block to extract the first element and 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 (configured for two ports to choose between different messages with one of the choices being a vector of all 0 values). The Switch block only selects between vectors that all have the same width. However, because the string length does not need to use the whole vector, you just need to fill them to the same width with 0 values following your data.
The Commtech Fastcom 422/2-PCI board can handle baud rates up to 1.5 megabaud. To configure a baud rate for the board, you need to set the following parameters. Note, this section applies to only Commtech Fastcom 422/2-PCI boards, not Commtech 422/2-PCI-335 and 422/4-PCI-335 boards.
Clock Bits in the Fastcom 422/2-PCI Send Receive block with the Parameter group parameter set to Board Setup
Baud Divisor in the Fastcom 422/2-PCI Send Receive block with the Parameter group parameter set to Basic Setup
The Fastcom 422/2-PCI board has two serial channels, each of which has an independent counter (baud clock). A master clock generator, which has a phase locked loop, controls the master clock for both serial channels. The master clock generates a maximum baud rate for both channels. The block determines the actual baud rate of a channel by dividing the maximum baud rate from the master clock by the baud rate divisor (n).

To correctly set the block parameters for this board, choose a maximum baud rate, as follows. This procedure assumes that both channels require different baud rates. Determine a common base clock that can be divided to produce the required baud rates for both channels, as follows:
In the MATLAB Command Window, type a command like the following. With a desired frequency (for example, 1.5e6) as the desired input, the fc422mexcalcbits utility calculates parameter values that you can use to configure the board rate for your board.
[a b df ] = fc422mexcalcbits(1.5e6)
This command returns three values.
a = 12199144 b = 24 df = 1500000
Examine the df value. The df value is the actual frequency the board will be able to attain compared to your desired frequency. If the actual attainable frequency is not accurate enough, you might want to try another frequency. In this example, the board can match the desired frequency of 1500000 (1.5e6).
Enter the first two values in the Clock Bits parameter of the Fastcom 422/2-PCI Send Receive block with the Parameter group parameter set to Board Setup.

After you define a maximum baud rate, you can set a unique baud rate for each channel by choosing a different baud rate divisor for each channel. For example, you can have Channel 1 have a baud rate of 750000 (1500000/2) and Channel 2 have a baud rate of 1500000 (1500000/1). To set Channel 1 to have a baud rate of 750000, with the Fastcom 422/2-PCI Send Receive block with the Parameter group parameter set to Basic Setup, set
Port to modify to 1
Baud Divisor to 2
Note For very slow baud rates (less than 30000), you must use the Baud Divisor parameter to achieve the desired baud rate. |
![]() | Serial Drivers | Boards and Blocks — Alphabetical List | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |