This model shows CAN I/O communication using the First-In, First-Out (FIFO) mode of the Softing CAN-AC2-PCI board. For the case where data is being received faster than it is being processed, here it is shown how to stop execution of the model after detecting a receive FIFO overflow.
Important: CAN_MESSAGE data types are used for message storage. See the Can Pack and Can Unpack blocks for details.
For this example, the CAN network consists of a single physical connection between CAN port 1 and CAN port 2 (loop-back configuration). To test this model:
Install the CAN-AC2-PCI board in the target computer.
Use a serial 9-pin/9-pin cable (directly wired) to connect CAN port 1 and CAN port 2.
Set the switches on the board's backplane to terminate both ports.
Boot the target computer as an xPC Target™ machine.
At the MATLAB command prompt, type xpccanpcififo4sdemo to download and run the model on the target computer.
Figure 1: CAN IO Model Using FIFO Write and FIFO Read
Message generation (encoding) is contained in the Encode Messages subsystem illustrated in Figure 2.
As described in FIFO mode example 3, only 2 of 3 messages are being sent to the CAN bus every 1 millisecond.
Figure 2: The Encode Messages Subsystem
Message extraction (decoding) is contained in the Decode Messages subsystem illustrated in Figure 3.
In this example, the FIFO Read block has a sample time of 1 millisecond and a read depth of 1. Since message generation transmits 2 messages every millisecond, the receive FIFO grows by one after every transmit/receive cycle. We can monitor the FIFO Read block's status port and perform some action (e.g., stop the simulation) when the buffer overflows. We do this by monitoring the lost message counter. When it grows too large, stop the model.
Figure 3: The Decode Messages Subsystem
set_param(mdl,'RTWVerbose','off'); rtwbuild(mdl); +tg; pause(3); -tg;
### Starting xPC Target build procedure for model: xpccanpcififo4s Warning: This model contains blocks that do not handle sample time changes at runtime. To avoid incorrect results, only change the sample time in the original model, then rebuild the model. ### Successful completion of build procedure for model: xpccanpcififo4s ### Looking for target: TargetPC2 ### Download model onto target: TargetPC2
The model uses several xPC Target™ scope blocks (see Figure 4) to output different types of data on the target computer display. In this example, notice the report of the lost message counter (Number of Lost Messages) in Scope 6. When the lost message condition is detected, model execution is stopped.
Scope 1 (graphical) plots the data of all received CAN messages.
Scope 2 (numerical) displays the FIFO Write block status vector for each message.
Scope 3-5 (numerical) displays the Message IDs, Message Timestamp, Message Frame size, and the count of the total messages received.
Scopes 6 (numerical) displays the Status of the FIFO Read block (Number of Lost Messages, and the Bus State)
Figure 4: Target Scopes