xPC Target    

Creating RS-232 Message Structures (Asynchronous)

RS-232 drivers use MATLAB structures to send and receive messages and map the input and output ports on the RS-232 driver blocks to the data written and read from the RS-232 devices in synchronous mode.

After you add RS-232 Setup, Asynchronous Send, and Asynchronous Receive block to your Simulink model, you can create the message structures to communicate with the RS-232 devices. You need to create and load these structures into the MATLAB workspace before you build your target application. The easiest way to create these structures is to use an M-file and load that M-file into the MATLAB workspace.

  1. In the MATLAB command window, and from the File menu, point to New, and then click M-file.
  1. A MATLAB text editor window opens.

  1. Enter the initialization, send, receive, and termination messages. Each message is an element in a MATLAB structure array with a series of fields For information and examples of these fields, see RS-232 MATLAB Structure Reference.
  1. For example, if you want to send and receive two floating-point numbers, type the following.

    Note Field names in the structures are case sensitive.

    Note If you do not manually load the message structures before opening your Simulink model, or have the message structures automatically loaded with the model, the port connections to the RS-232 blocks break.

  1. From the File menu, click Save As. In the Save As File dialog box, enter the name of the M-file script. For example, enter
  2. Close the text editing window.
  3. In the MATLAB command window, type the name of the M-file script you created with the RS-232 structures. For example, type
  1. MATLAB loads and runs the M-file script to create the message structures in the MATLAB workspace needed by the RS-232 driver blocks.

  1. Open your Simulink model, or press Ctrl+D.
  1. Simulink updates the RS-232 driver blocks with the information from the structures. For example, Simulink adds the inputs and outputs defined in the structures to the blocks.

  1. Connect the input and output ports on the RS-232 driver blocks to other blocks in your Simulink model.
  1. Your model should look similar to the figure shown below.

  1. Set the pre-load function for your Simulink model to load the message structures when you open the model. For example, if you saved the message structures in the M-file RS232async_messages, type
  1. Note If you do not manually load the message structures before opening your Simulink model, or have the message structures automatically loaded with the model, the port connections to the RS-232 blocks breaks.

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


  Adding RS-232 Driver Blocks (Asynchronous) Building and Running the Target Application (Asynchronous)