Products & Services Solutions Academia Support User Community Company

Learn more about xPC Target   

Obsolete Drivers

Introduction

The xPC Target software interfaces the target PC to serial devices using either the COM1 or COM2 port of the main board.

This chapter describes the obsolete xPC Target blocks that communicate with the COM1 or COM2 port of your main board. The obsolete drivers support RS-232 I/O only for the target PC serial ports. These drivers support synchronous, asynchronous, and binary (asynchronous) communication mode. The xPC Target software uses a model for this RS-232 I/O that includes both Simulink blocks for the I/O drivers and MATLAB structures for sequencing messages and commands. These are older blocks for serial communication. Consider using the composite serial communication blocks described in Serial Communications Support instead.

xPC Target RS-232 Drivers (Obsolete)

This section describes the components that make up the RS-232 obsolete drivers, and how you can create a model using these drivers. This section includes the following topics:

Simulink Blocks for RS-232 I/O (Obsolete)

To support the use of RS-232, the xPC Target I/O library includes a set of RS-232 driver blocks. These driver blocks can be added to your Simulink model to provide inputs and outputs using one or more of the RS-232 ports.

MATLAB Message Structures for RS-232 I/O (Obsolete)

Communication is through a series of messages passed back and forth between the target PC and the RS-232 device. To accomplish this, the messages sent to the RS-232 device must be in a format that the device understands. Likewise, the target PC must know how to interpret the data returned from the RS-232 device.

The xPC Target software uses MATLAB structures to create 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. The RS-232 Setup block sends the messages in the initialization structure after downloading the target application. The RS-232 Send/Receive, RS-232 Send, and RS-232 Receive blocks repeat the sending of the messages in the send/receive, send, and receive structures during each sample interval. When the target application stops running, the RS-232 Setup block sends the messages in the termination structure.

Below is an example of the send and receive message structure for asynchronous communication. In this example, an external RS-232 device requires a string with two floating-point numbers. The numbers are entered from the Simulink model to the first and second input ports of the RS-232 Send driver block. The RS-232 device sends back two floating-point numbers that are passed to the outputs of the RS-232 Receive driver block.

For more information on this example, see Creating RS-232 Message Structures (Asynchronous).

RS-232 Synchronous Mode (Obsolete)

Use synchronous mode when you need to receive a response before continuing with other computations. In synchronous mode, data is sent to an external device and the driver block waits for a response. In other words, the I/O driver blocks or stops execution of the target application until an answer is received from the external device or it reaches a time-out. This section includes the following topics:

Notes for RS-232 Synchronous Mode.   For the example in this section, assume an external device (RS-232 device) includes a D/A conversion module with four independent channels and an output voltage range of -10 to 10 volts. Also assume that the external device outputs a new voltage if it receives a serial string with a value to identify the D/A channel and the voltage value.

Use a Constant block as an input to the Send/Receive block to select the D/A channel, and a Signal Generator block as a source for voltage values. Also, set up the message structures to receive a confirmation message from the external module after the target PC sends a message string to the device.

In synchronous mode, the data is sent to the external device and the block waits until a response (for example, data) is received from the device before the execution of the block is considered to be complete. In other words, the I/O driver blocks until an answer is received from the external device or it reaches a time-out.

When it is necessary to receive a response before continuing with other computations, synchronous mode is used, which implies that the Send & Receive block is placed in your model. This block includes both input and output lines.

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:

  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 two main sections, Composite drivers and Obsolete drivers. Refer to the Obsolete drivers section, where there are two setup blocks. The second block is included for compatibility with xPC Target Version 1.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 RS-232.

  3. From the Obsolete drivers area, drag and drop an RS-232 Setup block to your Simulink model.

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

  5. Drag and drop an RS-232 Send/Receive block to your Simulink model.

  6. Add a Signal Generator and a Constant block.

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

  7. 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 data bits, and 1 stop bit, 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 Mainboard Setup (Obsolete). For the procedure to create the initialization and termination structures, see RS-232 MATLAB Structure Reference (Obsolete).

  8. Click OK. The Block Parameters dialog box closes.

  9. Double-click the RS-232 Send/Receive block. The Block Parameters dialog box opens.

  10. 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, but the name of the structure created with the M-file.

    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 Mainboard Send/Receive (Synchronous) (Obsolete). For the procedure to create the send/receive structure, see RS-232 MATLAB Structure Reference (Obsolete).

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

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

    A MATLAB text editor window opens.

  2. 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 (Obsolete).

    For example, assume that you have an external RS-232 device with a D/A module that requires 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 can type the following.

      Note   Field names in the structures are case sensitive.

    RS232_Send_Receive(1).SendData = 'da_1234,%d,%f,;\n';
    RS232_Send_Receive(1).InputPorts = [1 2];
    RS232_Send_Receive(1).RecData = 'noerror\n';
    RS232_Send_Receive(1).Timeout = 0.01;
    RS232_Send_Receive(1).EOM = 1;
  3. 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

    RS232Sync_Messages.m
  4. Close the text editing window.

  5. In the MATLAB Command Window, type the name of the M-file you created with the RS-232 structures. For example, type

    RS232Sync_Messages

    The MATLAB interface loads and runs the M-file to create the message structures in the MATLAB workspace needed by the RS-232 driver blocks.

  6. Open your Simulink model, or press Ctrl+D.

    The Simulink software updates the RS-232 driver blocks with the information from the structures. For example, it adds inputs and outputs defined in the structures to the blocks.

  7. Connect the input and output ports on the RS-232 driver blocks to other blocks in your Simulink model.

    Your model should look similar to the figure shown below.

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

    set_param(gcs, 'PreLoadFcn','RS232Sync_messages.m')

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.

RS-232 Asynchronous Mode (Obsolete)

Use asynchronous mode when you do not need a response before continuing with other computations. You can achieve faster sample rates with asynchronous mode because neither the Send nor Receive block waits for a reply. As a result, the asynchronous mode blocks do not block as do the synchronous mode blocks. The application updates the received outputs only when the entire package of data is received from the external device. This section includes the following topics:

Notes for RS-232 Asynchronous Mode.   For the example in this section, two asynchronous mode blocks illustrate how you can test RS-232 I/O on the target PC in a simple loop-back test. This simple but effective test lets you check that the RS-232 Send and RS-232 Receive blocks work correctly with your system using minimal hardware.

In this loop-back test, you use the COM1 port for sending signals and the COM2 port for receiving signals. A NULL modem serial cable connects COM1 to COM2 so that any messages sent from the target PC through COM1 are received by COM2 on the same target PC.

Use a Sine Wave block as an input to an RS-232 Send block that you connect to the COM1 port. Connect the COM2 port to an RS-232 Receive block. The signal received from this block is then passed through a Gain block of -1.

In the asynchronous mode, data is sent without waiting for response data to be received. The Send block completes execution immediately upon completing the Send transfer. The Receive block completes execution upon completing the Receive transfer or when no more data is ready to be retrieved.

For sending data in asynchronous mode, use the RS-232 Send block. This block only has input lines for the data to be sent. For receiving data, you must use the Receive block. This block only has output lines for the data to be received. Outputs are updated only when the entire package of data is received from the external device.

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

    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.

    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 RS-232.

  3. Drag and drop two RS-232 Setup blocks to your Simulink model.

  4. In the Library window, double-click the RS-232 Asynchronous mode group block. The library window containing blocks for RS-232 Synchronous communication opens.

      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 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 RS-232.

  5. Drag and drop the RS-232 Send and RS-232 Receive blocks into your Simulink model.

  6. Add a Signal Generator, Gain, and xPC Target Scope block.

    Your model should look similar to the figure below. Note that 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 message structure, and visible only after you load that structure into the MATLAB workspace and update your Simulink model.

  7. Double-click the first RS-232 Setup block. Enter values to configure the COM1 port on the target PC.

    For example, if the COM1 and COM2 ports of the target are connected with a RS-232 NULL modem cable, and you set serial communication to 57600 baud, 8 data bits, and 1 stop bit. Your Block Parameters 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 Mainboard Setup (Obsolete). For the procedure to create the initialization and termination structures, see RS-232 MATLAB Structure Reference (Obsolete).

  8. Click OK. The Block Parameters dialog box closes.

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

  10. Double-click the Send block. The Block Parameters dialog box opens.

  11. 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 box, enter the sample time or a multiple of the sample time you entered in the RS-232 Receive block.

    Your Block Parameters dialog box should look similar to the figure shown below.

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

  12. Click OK. The Block Parameters dialog box closes.

  13. Double-click the RS-232 Receive block.

  14. The Block Parameters dialog box opens.

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

    Your Block Parameters dialog box should look similar to the figure shown below.

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

  16. Click OK. The Block Parameters dialog box closes.

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

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

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 the RS-232 Setup, Asynchronous Send, and Asynchronous Receive blocks 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. See xpcrs232V2.mdl in the xpcdemos directory for an example model. That example sends and receives two floating-point numbers. In that example, both floating-point number fields for SendData are filled from InputPorts 1 because only one input port is specified. In the case of RecData, the first floating-point number field is sent to OutputPorts 1, but the second floating-point number field is ignored because only one output port is specified.

The following procedure describes how to create an RS-232 message structure to send and receive one floating-point number:

  1. In the MATLAB Command Window, and from the File menu, point to New, and then click M-file.

    A MATLAB text editor window opens.

  2. 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 (Obsolete).

    For example, if you want to send and receive one floating-point number, type the following. In this example, the floating-point number field for SendData is filled from InputPorts 1. In the case of RecData, the floating-point number field is sent to OutputPorts 1.

      Note   Field names in the structures are case sensitive.

    RS232_Send(1).SendData = 'start,%f,%f,stop;\r';
    RS232_Send(1).InputPorts = [1];
    RS232_Send(1).Timeout = 0.01;
    RS232_Send(1).EOM = 1;
    RS232_Receive(1).RecData = 'start,%f,%f,stop;\r';
    RS232_Receive(1).OutputPorts = [1];
    RS232_Receive(1).Timeout = 0.01;
    RS232_Receive(1).EOM = 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 break.

    If you want to send more than one variable, for example three, in a single frame, use the following RS232_Receive(1).OutputPorts line. This line sends the first %f data to output port 1, the second %f to output port 2, and the third %f to output port 3.

    RS232_Receive(1).OutputPorts = [1 2 3];
  3. From the File menu, click Save As. In the Save As File dialog box, enter the name of the M-file. For example, enter

    RS232Async_Messages.m
  4. Close the text editing window.

  5. In the MATLAB Command Window, type the name of the M-file you created with the RS-232 structures. For example, type

    RS232Async_Messages

    The MATLAB interface loads and runs the M-file to create the message structures in the MATLAB workspace needed by the RS-232 driver blocks.

  6. Open your Simulink model, or press Ctrl+D.

    The Simulink interface updates the RS-232 driver blocks with the information from the structures. For example, it adds the inputs and outputs defined in the structures to the blocks.

  7. Connect the input and output ports on the RS-232 driver blocks to other blocks in your Simulink model.

    Your model should look similar to the figure shown below.

  8. Set the preload 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

    set_param(gcs, 'PreLoadFcn','RS232async_messages')

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

Building and Running the Target Application (Asynchronous).   The xPC Target and Real-Time Workshop software create C code from your Simulink model. You can then use a C compiler to create executable code that runs on the target PC.

After you have added the RS-232 blocks for asynchronous mode to your Simulink model, and created and loaded the RS-232 structures into the MATLAB workspace, 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)

    The target application begins running in real time.

    For each sample period, the RS-232 messages you entered in the RS-232 send and receive message structures are executed.

    In this example, the target PC displays the inverted waveform. The RS-232 Send and RS-232 Receive blocks require a minimum delay or one sample to send the data and also receive it. When running at faster sample rates, several sample intervals might elapse while one set of data is transmitted, because RS-232 communication is not particularly fast. The sample delay just described is not visible in this example.

You can extend this example for multiple D/A channels by simply adding more input signals and modifying the format string to have additional '%f' format specifiers.

RS-232 Simulink Block Reference (Obsolete)

The xPC Target block library supports RS-232 communication with driver blocks in your Simulink model and message structures in the MATLAB workspace.

This section includes the following topics:

RS-232 MATLAB Structure Reference (Obsolete)

You do not use all message fields in all messages. For example, a message to send data would not use the message field.RecData, but would use the field .SendData. However, knowing the possible message fields is helpful when you are creating any of the message structures. This section contains the following topics:

RS-232 Send/Receive Message Structure (Synchronous).   Below are descriptions of the possible message fields for the send/receive structures with asynchronous mode. The order of the fields does not matter. However, the field names are case sensitive.

Message Field

Description

SendData

Data and format sent to the RS-232 device. Default value = ''.

Note that the SendData syntax format is the same as the C printf() library function. It is also very similar to the MATLAB fscanf method, with the exception that SendData is not vectorized.

InputPorts

Number of input ports for the driver block. Data from the input ports is sent to the RS-232 device with the message field.SendData. Default value = []. The highest number you enter determines the number of input ports on the driver block

For example, the following message creates two input ports on the driver block,

RS232_Send_Receive(1).InputPorts= [1 2];

RecData

Data and format received from the RS-232 device. Default value = ''. The format of this statement is very similar to a scanf statement. The read data is mapped to the output ports defined in the message field .OutputPorts. If a negative output port is given, the data is read in, but not sent to any output port.

OutputPorts

Number of output ports from the driver block. Data received from an RS-232 device is sent to the output ports with the message field .RecData. Default value = []. The highest number you enter determines the number of output ports on the driver block.

For example, to use output ports 1 and 2 on the driver block, type

RS232_Send_Receive.OutputPorts = [1 2];

Timeout

Time, in seconds, the driver block waits for data to be returned. Default value = 0.049.

EOM

Number of characters you use to indicate the end of a message.

RS-232 Send Message Structure (Asynchronous).   Below is a description of the possible message fields for the send structure with synchronous mode. The order of the message fields does not matter. However, the field names are case sensitive.

Message Field

Description

SendData

Data and format sent to the RS-232 device. Default value = ''.

Note that the SendData syntax format is the same as the C printf() library function. It is also very similar to the MATLAB fscanf method, with the exception that SendData is not vectorized.

InputPorts

Number of input ports for the driver block. Data from the input ports is sent to the RS-232 device with the message field .SendData. Default value = []. The highest number you enter determines the number of input ports on the driver block.

For example, the following message creates two input ports on the driver block.

RS232_Send_Receive(1).InputPorts= [1 2];

Timeout

Time, in seconds, the driver block waits for data to be returned. Default value = 0.049.

EOM

Number of characters you use to indicate the end of a message.

RS-232 Receive Message Structure (Asynchronous).   Below are descriptions of the possible message fields for the receive message structure with synchronous mode.

Message Fields

Description

RecData

Data and format received from the RS-232 device. Default value = ''. The format of this statement is very similar to a scanf statement. The read data is mapped to the output ports defined in the message field .OutputPorts. If a negative output port is given, the data is read in but not sent to any output port.

OutputPorts

Number of output ports from the driver block. Data received from an RS-232 device is sent to the output ports with the message field .RecData. Default value = []. The highest number you enter determines the number of output ports on the driver block.

For example, to use output ports 1 and 2 on the driver block,

RS232_Send_Receive.OutputPorts = [1 2];

Timeout

Time, in seconds, the driver block waits for data to be returned. Default value = 0.049.

EOM

Number of characters you use to indicate the end of a message.

Supported Data Types for Message Fields.   The following table lists the supported data types for the RS-232 message fields.

Format

Description

%c and %C

Single character

%d or %i

Signed decimal integer

%u

Unsigned decimal integer

%o

Unsigned octal integer

%x or %X

Unsigned hexadecimal integer using 'abcdef' or 'ABCDEF' for the hexadecimal digits

%e or %E

Exponential format using e or E

%f

Floating point

%g

Signed value printed in f or e format depending on which is smaller

%G

Signed value printed in f or E format depending on which is smaller

RS-232 Binary Mode (Obsolete)

Use RS232 Binary mode when you want to transfer raw data. The format of this data is either a custom format or is an image of the bytes as they are stored in memory. This section includes the following topics:

RS-232 Binary Mode I/O.   The binary mode drivers operate in asynchronous mode. In other words, they do not wait until an entire packet of data is received, but receive as many bytes as available and then go on to the next data block. When an entire packet has been received, the block outputs the new data. Sent data is also handled similarly. The Send block instructs the RS-232 hardware to send a certain number of bytes, but does not wait for these bytes to actually be sent.

The RS-232 binary mode infrastructure also includes blocks to pack and unpack any data received. This translates the raw bytes into signals that the Simulink software can understand.

The functioning of these blocks is identical to the corresponding blocks in the UDP section of the xPC Target block library. The RS232 Binary Pack and Unpack blocks are actually references to these blocks. For information about UDP and the functionality of these blocks, see UDP I/O Support.

Using RS-232 Binary Mode.   To use the RS-232 binary mode blocks, you must first insert exactly one RS232 Setup block for each COM port into your model. The setup for this block is exactly the same as it is for text-based I/O, except that initialization or termination structures are ignored. In the dialog box, set both these fields to the empty matrix ([]).

The RS-232 binary mode blocks can be found in the RS-232 section of the xPC Target Block Library. Use the following procedure to access these blocks:

  1. In the MATLAB Command Window, type

    xpclib

    The xPC Target Block Library opens.

  2. Double-click the group block RS232.

    The Library: xpclib/RS232 library opens.

  3. Double-click the group block Binary Mode.

    The Library: xpclib/RS232/Binary Mode library opens.

  4. Drag and drop any of these blocks into your Simulink model.

Example Using RS-232 Binary Mode I/O.   To show the flexibility provided by the RS232 Binary Receive block, the following illustrates how this block can be used. The model that implements this setup is provided with the xPC Target product. To access this model, type

xpcrs232bindemo

at the command prompt. This opens the model, which is essentially self-documenting. Open each subsystem in the model to see what that part is supposed to accomplish.

Here is an example of a messaging protocol that the model has to conform to.

The protocol consists of a one-byte header, followed by a variable-length body. The header can have only two legal values, 12 and 17. If the header is 12, the body is 6 bytes long, and consists of a uint16 followed by an int32 (in terms of MATLAB data types). If the header is 17, the body is 4 bytes long, and consists of a uint16 followed by an int16.

The model receives one header byte at a time, rejecting any invalid ones. As soon as a valid header byte is received, the execution switches to the body block, where the proper number of bytes is received. The data is then appropriately decoded and displayed on an xPC Target Scope of type target. The model should serve as an example of how this is done.

The basic algorithm is to receive a header byte and then compare it to the list of known headers (12 and 17). The body length is set appropriately depending on the header, and the "Done" function-call output of the header block is used to trigger functioning of the body block (via the "distributor" function call subsystem).

  


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