Products & Services Industries Academia Support User Community Company

Learn more about xPC Target   

xPC Target RS-232 and 422/485 Drivers (Composite)

Introduction

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):

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):

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 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:

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:

  1. In the MATLAB Command Window, type

    xpclib

    The xPC Target driver block library opens.

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

    A window with blocks for RS-232 drivers opens.

      Note   This library contains three sections, Composite, Obsolete, and Obsolete V1.0.

    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.

  3. Drag and drop 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 and drop 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 and drop 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:

    • 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.

  11. Add a Pulse Generator and Target Scope block.

  12. 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.

  13. 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.

  14. 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.

  15. 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).

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

Your next task is to build and run the target application.

Building and Running the Target Application (Composite)

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.

  1. In the Simulink window, and from the Tools menu, point to Real-Time Workshop, and then click Build Model.

  2. In the MATLAB Command Window, type

    +tg or tg.start or start(tg)

RS-232/422/485 Simulink Block Reference

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

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.

Handling Zero Length Messages

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:

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.

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 string.

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:

Defining the Commtech Fastcom Baud Rate for Commtech Fastcom 422/2-PCI Boards

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.

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:

  1. 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
  2. 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).

  3. 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

  


Related Products & Applications

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