Documentation search search close
CONTENTS

    Open this example in MATLAB

v10.0.1.x for R2026a
View other versions

IO603 - LIN and DIO Loopback

This example shows how to run a simple loopback using the IO603 I/O module. The IO603 features 4 isolated channels for CAN FD and High-Speed CAN, 2 isolated channels for LIN and 4 individually configurable TTL digital I/O lines.
The Simulink model implements a unidirectional LIN loopback transmitting data from channel 2 to channel 1, and also a unidirectional DIO loopback from channels 1 to 2 and channels 3 to 4.
This example demonstrates the following modes and features:
  • Communication between a pair of LIN and DIO channels on an IO603 I/O module
  • Using the Vector Concatenate and several Selector blocks with a LIN Write and LIN Read block
  • Using the Byte Packing and Byte Unpacking blocks with a LIN Write and LIN Read block
  • Using the LIN Pack and LIN Unpack blocks with a LIN Write and LIN Read block

Setup

Prerequisites

You will require the following to run this example:
  • Speedgoat real-time target machine with one IO603 I/O module installed
  • 1x 25-pin D-sub to 4x 9-pin D-sub splitter cable
  • 1x external power supply capable of outputting 8-24V

Connection Diagram

To run this example, it is best to use the splitter cable to connect LIN channel 1 to channel 2, DIO channel 1 to channel 2, as well as DIO channel 3 to channel 4.
The external power supply is required to power the LIN Bus with a voltage in between 8 V and 24 V.
io603_ch1_ch2_dio_loopback.png

Initialize and Open the Simulink Model

% Open Simulink model
modelName = 'sgMdl_IO603_LIN_DIO_Loopback';
open_system(modelName);

Model Description

The Simulink model is split into two subsystems that represent the LIN and DIO communication. One subsystem is used to send LIN messages from channel 1 to channel 2, and the second subsystem manages the DIO communication.
The LIN communication is controlled by the LIN Read blocks configured as Master. The Master on channel 1 sends out a header requesting a response from a Slave on channel 2 with a matching ID. All LIN Read blocks are placed inside a triggered subsystem which executes the blocks alternately. This to schedule the communication on the LIN Bus in order to avoid conflicting header requests.

Sending LIN Data from Channel 1 to Channel 2

% open Ch1RxMaster_Ch2TxSlave subsystem
speedgoat.model.highlight([modelName '/Ch1RxMaster_Ch2TxSlave'],'Open',true);
In this subsystem, on the left-hand side, each of the eight signals are prepared differently to be sent as a LIN message. The first option utilizes the Vector Concatenate block to combine multiple uint8 signals into an 8-byte uint8 vector. The second option utilizes the Byte Packing block to combine multiple uint8 signals into an 8-byte uint8 vector. The third option utilizes the LIN Pack block to load and parse a .ldf file. This file describes a set of uint8 signals to be packed into a LIN frame and output as a 8-byte uint8 vector. All of these vectors are wired to a LIN Write block with LIN message IDs 48, 49 and 50.
On the right-hand side, the expected LIN message IDs are specified separately in two LIN Read block masks. Consequently, the two IDs are available as outputs on the respective block mask. The division of the 8-byte LIN messages into signals is again done in differently for each message. The first option utilizes the Selector block to index which uint8 to grab from the vector. The second option utilizes the Byte Unpacking block to map the signals to an output. The third option utilizes the LIN Unpack block to parse the same .ldf file again to unpack the LIN frame to output single signals again.

Sending Data from DIO Channel 1 to Channel 2 and Channel 3 to Channel 4

% open Ch1Ch3Tx_Ch2Ch4Rx subsystem
speedgoat.model.highlight([modelName '/Ch1Ch3Tx_Ch2Ch4Rx'],'Open',true);
In this subsystem, digital pulse signals are sent via the Digital Output block and received again by the Digital Input block.

Build, Download, and Run the Example

To run the example, either run the following code section or click the Run on Target button in the REAL-TIME tab in the Simulink model.
% Select and load the .ldf file for the LIN Pack and Unpack blocks
set_param([modelName, '/Ch1RxMaster_Ch2TxSlave/LIN Pack'], 'LDFFileName', 'LINDescriptorFile.ldf', 'selectedFrameName', 'Frame_06');
set_param([modelName, '/Ch1RxMaster_Ch2TxSlave/Enabled Subsystem2/LIN Unpack'], 'LDFFileName', 'LINDescriptorFile.ldf', 'selectedFrameName', 'Frame_06');
 
% Build the Simulink model
slbuild(modelName); % this will create the real-time application file (.mldatx)
 
% Create and connect to the Speedgoat real-time target machine
tg = slrealtime;
tg.connect;
 
% Download the real-time application to the target machine and set stop time
tg.load(modelName);
tg.setStopTime(10);
 
% Connect the Simulink model with external mode to the real-time application on the target machine
set_param(modelName,'SimulationMode', 'external') % put model into External Mode
set_param(modelName,'SimulationCommand','connect') % connect with External Mode
set_param(modelName,'SimulationCommand','start') % start real-time application through Simulink toolstrip
 
% Wait until the real-time application reaches the stop time of 10 seconds

Results

During the simulation, open the Simulink scopes or use SDI to view the data. The output should look as follows:

Ch1RxMaster_Ch2TxSlave

Ch1Ch3Tx_Ch2Ch4Rx

Additional References