xPC Target    

Creating RS-232 Message Structures (Synchronous)

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.

After you add an RS-232 Setup and RS-232 Send/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 using 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. For information and examples of this structure, see RS-232 MATLAB Structure Reference.
  1. For example, you could have an external RS-232 device with an D/A module that wants a string in the format 'identifier, channel, value;\n'. Identifier is any string. Channel is an integer value between 1 and 2, defining which D/A channel to update. Value is a floating-point value indicating the new voltage for the D/A output.

    Additionally, when the external device receives a legal string, it accepts the string as an input message, and returns the message 'noerror;\n'. This message is provided as a confirmation. As an example, you could type the following

    Note Field names in the structures are case sensitive.

  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 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. You model should look similar to the figure shown below.

  1. Set the PreLoadFcn for your Simulink model to load the message structures when you open your model. For example, if you saved the message structures in the M-file RS232Sync_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 driver break.

Your next task is to build and run the target application. However, the example above only illustrates how to set up the dialog entries when using the Send & Receive block. Without an external RS-232 device to receive the messages, and return a reply "no error\n", this model cannot run successfully on your target PC. It will block and wait for a reply each time the application sends a message.


  Adding RS-232 Driver Blocks (Synchronous) RS-232 Asynchronous Mode