This model shows CAN I/O communication using the First-In, First-Out (FIFO) mode of the Softing CAN-AC2-PCI board and shows how CAN messages can be dynamically constructed at run-time.
Important: This legacy example uses standard data types for message storage. Use CAN_MESSAGE data types instead.
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 xpccanpcififo3demo 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.
In the first two FIFO mode examples, three messages are input to the FIFO Write block at each model update (every 1 millisecond). However, using a control element, only two messages are sent to the CAN port 1 transmit FIFO. With this approach, the message matrix entering the FIFO Write block has a size of [3,6].
Now look at a different implementation. Input only two CAN messages to the FIFO Write block and (dynamically) toggle the second message between two message generation sources. The three messages are transmitted at the same rate as in the previous examples but the matrix entering the FIFO Write block has a reduced size of [2,5] since no control element is needed.
Note: You would use the first implementation (message matrix [3,6] with the additional control element) if it were possible or necessary to transmit all three messages at any given update.
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 read depth of 2. Since only two messages are being sent, the FIFO Read Filter block looking for NE messages will always return 0.
Figure 3: The Decode Messages Subsystem
set_param(mdl,'RTWVerbose','off'); rtwbuild(mdl); +tg; pause(3); -tg;
### Starting xPC Target build procedure for model: xpccanpcififo3 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: xpccanpcififo3 ### 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 NE messages (Read Filter-NoNewEvent) in Scope 3. For each read, there should be 0 NE messages.
Scope 1 (graphical) plots the data of all received CAN messages.
Scope 2 (numerical) displays the FIFO Write block status vector [transmit message write status].
Scope 3 (numerical) displays the number of NE messages and the status vector leaving the FIFO Read block [lost-message-counter, bus state].
Scopes 4-6 (numerical) display the FIFO Read block info for each received CAN message [port, identifier, event type, data length, timestamp].
Figure 4: Target Scopes
close_system(mdl,0); % EOF xpccanpcififo3_script.m