Main Content

Read XCP Measurements with Dynamic DAQ Lists

This example shows how to use the XCP protocol capability to connect and acquire data from a Simulink model deployed to a Windows executable. The example reads measurement parameters of the model using TCP and dynamic DAQ lists. XCP is a high-level protocol used for accessing and modifying internal parameters and variables of a model, algorithm, or ECU. For more information, refer to the ASAM standards.

Algorithm Overview

The algorithm used in this example is a Simulink model built and deployed as an XCP server. The model has already been compiled and is available to run in the file XCPServerSineWaveGenerator.exe. Additionally, the A2L-file XCPServerSineWaveGenerator.a2l is provided as an output of that build process. The model contains three measurements and two characteristics accessible via XCP. Because the model is already deployed, Simulink is not required to run this example. The following image illustrates the model.

For details about how to build a Simulink model, including an XCP server and generating an A2L-file, see Export ASAP2 File for Data Measurement and Calibration (Simulink Coder).

Run the XCP Server Model

To communicate with the XCP server, the deployed model must be run. By using the system function, you can execute the XCPServerSineWaveGenerator.exe from inside MATLAB. The function requires constructing an argument list pointing to the executable. A separate command window opens and shows running outputs from the server.

sysCommand = ['"', fullfile(pwd, 'XCPServerSineWaveGenerator.exe'),'"', ' &'];
system(sysCommand);

Open the A2L-File

An A2L-file is required to establish a connection to the XCP server. The A2L-file describes all of the functionality and capability that the XCP server provides, as well as the details of how to connect to the server. Use the xcpA2L function to open the A2L-file that describes the server model.

a2lInfo = xcpA2L("XCPServerSineWaveGenerator.a2l")
a2lInfo = 
  A2L with properties:

   File Details
                 FileName: 'XCPServerSineWaveGenerator.a2l'
                 FilePath: 'C:\Users\kuanliu\Documents\MATLAB\Examples\vnt-ex16421241\XCPServerSineWaveGenerator.a2l'
               ServerName: 'ModuleName'
                 Warnings: [0×0 string]

   Parameter Details
                   Events: {'100 ms'}
                EventInfo: [1×1 xcp.a2l.Event]
             Measurements: {'Sine'  'SineAfterGain'  'SineAfterTable'  'XCPServer_DW.lastCos'  'XCPServer_DW.lastSin'  'XCPServer_DW.systemEnable'}
          MeasurementInfo: [6×1 containers.Map]
          Characteristics: {'Gain'  'ydata'}
       CharacteristicInfo: [2×1 containers.Map]
                 AxisInfo: [1×1 containers.Map]
            RecordLayouts: [4×1 containers.Map]
             CompuMethods: [3×1 containers.Map]
                CompuTabs: [0×1 containers.Map]
               CompuVTabs: [0×1 containers.Map]

   XCP Protocol Details
        ProtocolLayerInfo: [1×1 xcp.a2l.ProtocolLayer]
                  DAQInfo: [1×1 xcp.a2l.DAQ]
    TransportLayerCANInfo: [0×0 xcp.a2l.XCPonCAN]
    TransportLayerUDPInfo: [0×0 xcp.a2l.XCPonIP]
    TransportLayerTCPInfo: [1×1 xcp.a2l.XCPonIP]

TCP is the transport protocol used to communicate with the XCP server. Details for the TCP connection, such as the IP address and port number, are contained in the TransportLayerTCPInfo property.

a2lInfo.TransportLayerTCPInfo
ans = 
  XCPonIP with properties:
          CommonParameters: [1×1 xcp.a2l.CommonParameters]
    TransportLayerInstance: ''
                      Port: 17725
                   Address: 2.1307e+09
             AddressString: '127.0.0.1'

Create an XCP Channel

To create an active XCP connection to the server, use the xcpChannel function. The function requires a reference to the server A2L-file and the type of transport protocol to use for messaging with the server.

xcpCh = xcpChannel(a2lInfo, "TCP")
xcpCh = 
  Channel with properties:

              ServerName: 'ModuleName'
             A2LFileName: 'XCPServerSineWaveGenerator.a2l'
          TransportLayer: 'TCP'
    TransportLayerDevice: [1×1 struct]
              SeedKeyDLL: []

Connect to the Server

To make communication with the server active, use the connect function.

connect(xcpCh)

Create and View a Measurement List

A measurement in XCP represents a variable in the memory of the model. Measurements available from the server are defined in the A2L-file. One way to read measurement data is using dynamic DAQ lists. Use the createMeasurementList function to create a dynamic DAQ list with a specified event used to trigger the data acquisition and measurements that comprise the list.

createMeasurementList(xcpCh, "DAQ", "100 ms", ["Sine", "SineAfterGain", "SineAfterTable"]) 

View configured dynamic DAQ lists using the viewMeasurementLists function.

viewMeasurementLists(xcpCh)
DAQ List #1 using the "100 ms" event @ 0.100000 seconds and the following measurements:
   Sine
   SineAfterGain
   SineAfterTable

Acquire Data form XCP server

Start the configured dynamic DAQ list using the startMeasurement function. It begins the transmission of DAQ data from the server and stores the DAQ data in the XCP channel. After running for a few seconds, stop measurements using the stopMeasurement function.

startMeasurement(xcpCh)
pause(3);
stopMeasurement(xcpCh)

Retrieve the Sine Measurement Data

To retrieve the acquired data from the XCP channel for the Sine measurement, use the readDAQ function. The function requires a reference to the XCP channel and the specified measurement to read. readDAQ returns all available samples held by the XCP channel. Measurement data returned by readDAQ is fully scaled using the compute methods defined for the measurement in the A2L-file.

dataSine = readDAQ(xcpCh, "Sine");
plot(dataSine, "o-")
title("Sine Measurement Data")
xlabel("Data Point")
ylabel("Data Value")

Retrieve the SineAfterGain Measurement Data

To retrieve the acquired data from the XCP channel for the SineAfterGain measurement, use the readDAQ function.

dataSineAfterGain = readDAQ(xcpCh, "SineAfterGain");
plot(dataSineAfterGain, "o-")
title("SineAfterGain Measurement Data")
xlabel("Data Point")
ylabel("Data Value")

Retrieve the SineAfterTable Measurement Data

To retrieve the acquired data from the XCP channel for the SineAfterTable measurement, use the readDAQ function.

dataSineAfterTable = readDAQ(xcpCh, "SineAfterTable");
plot(dataSineAfterTable, "o-")
title("SineAfterTable Measurement Data")
xlabel("Data Point")
ylabel("Data Value")

Disconnect from the Server

To make communication with the server inactive, use the disconnect function. The XCP server can be safely closed after disconnecting.

disconnect(xcpCh)

Clean Up

clear a2lInfo