Main Content

Read XCP Measurements with Direct Acquisition

This example shows how to use the XCP protocol capability to connect and acquire data from an XCP Sample server. The XCP Sample server is specially designed for XCP examples only.

Vehicle Network Toolbox™ provides MATLAB functions for interfacing with a server over multiple transport layers including Controller Area Networks (CAN), Controller Area Network Flexible Data-Rate (CAN FD), Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). This example reads measurement parameters of the Sample server using direct memory access.

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 standard.

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("SampleECU.a2l")
a2lInfo = 
  A2L with properties:

   File Details
                 FileName: 'SampleECU.a2l'
                 FilePath: '/tmp/Bdoc24a_2528353_1101874/tp8faccd1d/vnt-ex10406968/SampleECU.a2l'
               ServerName: 'SampleServer'
                 Warnings: [0x0 string]

   Parameter Details
                   Events: {'Event DAQ 100ms'}
                EventInfo: [1x1 xcp.a2l.Event]
             Measurements: {'Line'  'PWM'  'Sine'}
          MeasurementInfo: [3x1 containers.Map]
          Characteristics: {'Gain'  'yData'}
       CharacteristicInfo: [2x1 containers.Map]
                 AxisInfo: [1x1 containers.Map]
            RecordLayouts: [3x1 containers.Map]
             CompuMethods: [1x1 containers.Map]
                CompuTabs: [0x1 containers.Map]
               CompuVTabs: [0x1 containers.Map]

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

Start the XCP Sample Server

The XCP Sample server mimics the behavior of a real XCP server in a controlled way. In this case, it serves ONLY for examples with limited functionalities. Use local SampleECU class to create the Sample server object and a Sample server will be created in the MATLAB Workspace. The XCP Sample server only supports CAN, CAN FD, and TCP. This example chooses the TCP protocol for demonstration.

sampleServer = SampleECU(a2lInfo,"TCP");

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. The XCP Channel must use the same device and a2l file as the Sample server to make sure they can establish connection with each other.

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

              ServerName: 'SampleServer'
             A2LFileName: 'SampleECU.a2l'
          TransportLayer: 'TCP'
    TransportLayerDevice: [1x1 struct]
              SeedKeyDLL: []
             ConnectMode: 'normal'

Connect to the Server

To activate communication with the server, use the connect function.

connect(xcpCh);

Directly Acquire Measurement Data

A measurement in XCP represents a variable in the memory of the Sample server. Measurements available from the server are defined in the A2L file. One way to read measurement data is using direct memory access. The readMeasurement function acquires the current value for a given measurement from the server. It is a single read without buffering.

SineData = readMeasurement(xcpCh, "Sine") 
SineData = 1
LineData = readMeasurement(xcpCh, "Line")
LineData = 0
PWMData = readMeasurement(xcpCh, "PWM")
PWMData = 2

Continuously Acquire Measurement Data

Sometimes, it is necessary to read a measurement continuously at regular interval for pooling data for different usage, such as for visualizing a value in a custom UI or using the value as input for other process. In such cases, readMeasurement is callable at any type of interval driven by a timer or loop. Reading measurements this way is best suited for asynchronous or low frequency purposes.

Disconnect from the Server

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

disconnect(xcpCh)

Clean Up

clear sampleServer a2lInfo