xPC Target |
 |
Adding RS-232 Driver Blocks (Synchronous)
You add RS-232 driver blocks to your Simulink model when you want to use the serial ports on the target PC for I/O.
After you create a Simulink model, you can add xPC Target driver blocks and define the initialization, send/receive, and termination message structures.
- In the MATLAB command window, type
- The xPC Target driver block library opens.
- Double-click the RS-232 group block.
- A window with blocks for RS-232 drivers opens.
Note This library contains two setup blocks. The second block is included for compatibility with xPC Target Version 1.0.

- Alternatively, you could 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 RS-232.
- Drag-and-drop an RS-232 Setup block to your Simulink model.
- In the Library window, double-click the RS-232 Synchronous mode group block. The library window with blocks for RS-232 synchronous communication opens.
- Note This library contains two Setup and Receive blocks. The second block is included for compatibility with xPC Target version 1.0.

- Drag-and-drop an RS-232 Send/Receive block to your Simulink model.
- Add a Signal Generator, and a Constant block.
- Your model should look similar to the figure shown below. Notice, the inputs on the RS-232 Send/Receive block are not defined or visible. The inputs are defined in a MATLAB message structure, and visible only after you load that structure into the MATLAB workspace and update your Simulink model.

- Double-click the RS-232 Setup block. Enter values to configure the COM1 port on the target PC.
- For example, if the target PC is connected to COM1, and serial communication is set to 5760 baud, 8 databits, and 1 stopbit, your Block Parameter dialog box should look similar to the figure shown below.
Note If you are not using an initialization or termination structure, in the Initialization Struct and Termination Struct boxes, enter the empty matrix []
.

For more information on entering the block parameters, see RS-232 Setup Block. For the procedure to create the initialization and termination structures, see RS-232 MATLAB Structure Reference.
- Click OK. The Block Parameters dialog box closes.
- Double-click the RS-232 Send/Receive block. The Block Parameters dialog box opens.
- From the Port list, select either
COM1
or COM2
. For this example, select COM1
. In the Message Struct Name box, enter the name for the MATLAB structure this block uses to send messages to the COM1 port. The name of the message structure is not the name of the M-file script, but the name of the structure created with the script.
- In the Sample Time box, enter the sample time or a multiple of the sample time you entered in the Receive block.
Your Block Parameter dialog box should look similar to the figure shown below.

For information on entering the block parameters, see RS-232 Send/Receive Block (Synchronous). For the procedure to create the send/receive structure, see RS-232 MATLAB Structure Reference.
- Click OK. The Block Parameters dialog box closes.
Your next task is to create the MATLAB message structures that the RS-232 driver blocks use to sequence commands to the RS-232 device. See Creating RS-232 Message Structures (Synchronous).
| Notes For RS-232 Synchronous Mode | | Creating RS-232 Message Structures (Synchronous) |  |