xPC Target    

Adding RS-232 Driver Blocks (Asynchronous)

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.

  1. In the MATLAB command window, type
  1. The xPC Target driver block library opens.

  1. Double-click the RS-232 group block.
  1. 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.

  1. Drag-and-drop a RS-232 Setup block to your Simulink model.
  2. In the Library window, double-click the RS-232 Synchronous mode group block. The library window containing blocks for RS-232 Synchronous communication opens.
  1. Note This library contains two send and two receive 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.

  1. Drag-and-drop the RS-232 Send and RS-232 Receive blocks into your Simulink model.
  2. Add a Signal Generator, Gain, and xPC Target Scope block.
  1. Your model should look similar to the figure below. Notice, you cannot connect to the inputs on the RS-232 Send block and the outputs on the RS-232 Receive block, because they are not defined or visible. The inputs and outputs are defined in a MATLAB massage structure, and visible only after you load that structure into the MATLAB workspace and update your Simulink model.

  1. Double-click the first RS-232 Setup block. Enter values to configure the COM1 port on the target PC.
  1. For example, if the COM1 and COM2 ports of the target are connected with a RS-232 null modem cable and setting serial communication to 57600 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.

  1. Click OK. The Block Parameters dialog box closes.
  2. Repeat the previous setup for the second RS-232 Setup block and the COM2 port. Use the same Baudrate, Databits, Stopbits, Parity, and Protocol that you entered in the first RS-232 Setup block.
  3. Double-click the Send block. The Block Parameters dialog box opens.
  4. 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. In the Sample Time box, enter the sample time or a multiple of the sample time you entered in the RS-232 Receive block.
  1. Your Block Parameters dialog box should look similar to the figure shown below.

    For information on entering the block parameters, see RS-232 Send Block (Asynchronous). For the procedure to create the send structure, see RS-232 MATLAB Structure Reference.

  1. Click OK. The Block Parameters dialog box closes.
  2. Double-click the RS-232 Send
  3. . The Block Parameters dialog box opens.
  4. From the Port list, select either COM1 or COM2. For this example, select COM2. In the Message Struct Name box, enter the name for the MATLAB structure this block uses to receive messages from the COM2 port. In the Sample Time box, enter the sample time or a multiple of the sample time you entered in the RS-232 Send block.
  1. Your Block Parameters dialog box should look similar to the figure shown below.

    For information on entering the block parameters, see RS-232 Receive Block (Asynchronous). For the procedure to create the send structure, see RS-232 MATLAB Structure Reference.

  1. Click OK. The Block Parameters dialog box closes.
  2. Double-click the Signal Generator block, and enter parameters. For example, from the Wave Form list, select, sine. In the Amplitude and Frequency boxes enter 1. From the Units list, select Hertz. Click OK.
  3. Double-click the Gain block, and enter parameters. For example, in the Gain box, enter -1. Click OK.

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 Asynchronous Mode Creating RS-232 Message Structures (Asynchronous)