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 reset the receive FIFO before it overflows.
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 xpccanpcififo5demo 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, the matrix entering the FIFO Write block has size [2,5]. Namely, 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.
Consider a different handling of the receive FIFO overflow condition: If the receive FIFO level reaches a certain value (e.g., 100), it is reset to delete all currently stored events; thus, preventing a buffer overflow. To accomplish this, two new CAN driver blocks are added to the model (FIFO Read RCV Level block and FIFO Reset RCV block).
Note: Simulation execution continues uninterrupted but the messages that were flushed from the buffer are lost.
Figure 3: The Decode Messages Subsystem
set_param(mdl,'RTWVerbose','off'); rtwbuild(mdl); +tg; pause(3); -tg;
### Starting xPC Target build procedure for model: xpccanpcififo5 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: xpccanpcififo5 ### 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 number of messages in the receive FIFO (Read Level-MsgsInFIFO) in Scope 3. When this value reaches 100, the buffer is reset.
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, the FIFO Read block status vector [lost-message-counter, bus state], and the number of messages in the receive FIFO.
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 xpccanpcififo5_script.m