This model shows CAN I/O communication using the First-In, First-Out (FIFO) mode of the Softing® CAN-AC2-PCI board. CAN_MESSAGE data types are used for message storage. The FIFO read block outputs an array of CAN_MESSAGE objects.
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 a Simulink Real-Time™ machine.
At the MATLAB® command prompt, type
xpccanpcififo1sdemo to download and run the model on the target computer.
Select and Open the Model
Figure 1: CAN IO Model Using FIFO Write and FIFO Read in function call output mode.
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. The switch block selects between two different messages. One with ID 112 and the other with ID 114.
Figure 2: The Encode Messages Subsystem
Message extraction (decoding) is contained in the For Iterator Decoder subsystem illustrated in Figure 3.
The FIFO read block is configured to output up to 3 different received messages if that many were received in the time step. For each message received, it will call the function call subsystem with one message available as the output data each time. If there are two messages in the FIFO, then the block steps through those two messages.
The decode subsystem connects the input data to the three decode blocks. Only the one that has an ID that matches the incoming message will decode. The other two will not change their output.
open_system([mdl,'/For Iterator Decoder']);
Figure 3: The For Iterator Decoder Subsystem
Build, Download, and Run the Model
set_param(mdl,'RTWVerbose','off'); rtwbuild(mdl); tg = slrt; +tg; pause(3); -tg;
### Starting Simulink Real-Time build procedure for model: xpccanpcififo1s 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: xpccanpcififo1s ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
Display Scope Data
The model uses several Simulink Real-Time™ scope blocks (see Figure 4) to output different types of data on the target computer display. In this example, messages with ID = 5100 are sent every time step so scope 4 gets new data on every time step. Messages with ID = 112 are sent every other time step so scope 5 changes every other time step. Messages with ID = 114 alternate with 112 so scope 6 updates on the opposite time step as scope 5. The data in scope 1 show this clearly with the blue (5100) trace updating every time step and the red and yellow traces updating on alternate time steps.
Scopes for this example are as follows:
Scope 1 (graphical) plots the data of the CAN messages received.
Scopes 2-4 (numerical) displays the Message IDs, Message Timestamp, and Message Frame size.
Scope 5 (numerical) displays the FIFO Write block status vector for each message.
Scope 6 (numerical) displays the Status of the FIFO Read block (Number of Lost Messages and Bus State)
Figure 4: Target Scopes
Close the Model