Read XCP Measurements with Dynamic DAQ Lists
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 XCP Sample server using 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 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/Bdoc24b_2725827_3770987/tpf6b9d6b0/vnt-ex17137611/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 CAN FD protocol for demonstration.
sampleServer = SampleECU(a2lInfo,"CAN FD","MathWorks","Virtual 1",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. 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,"CAN FD","MathWorks","Virtual 1",1)
xcpCh = Channel with properties: ServerName: 'SampleServer' A2LFileName: 'SampleECU.a2l' TransportLayer: 'CAN FD' TransportLayerDevice: [1x1 struct] SeedKeyDLL: [] ConnectMode: 'normal'
Connect to the Server
To make communication with the server active, use the connect
function.
connect(xcpCh);
Create the Measurement List for DAQ
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", "Event DAQ 100ms", ["Sine", "PWM", "Line"]);
View configured dynamic DAQ lists using the viewMeasurementLists
function.
viewMeasurementLists(xcpCh)
DAQ List #1 using the "Event DAQ 100ms" event @ 0.100000 seconds with timestamp enabled and the following measurements: Line PWM Sine
Acquire Data from 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(6); stopMeasurement(xcpCh);
Read All Available Measurement Data
To retrieve all measurement data from the XCP channel in the timetable format, use the readDAQList
function. The function requires a reference to the XCP channel. readDAQList
returns a timetable with all available samples held by the XCP channel. Measurements returned by readDAQList
are fully scaled using the compute methods defined for those measurements in the A2L file.
data = readDAQList(xcpCh);
data
contains all measurement lists defined by the createMeasurementList
function in a cell array. Each DAQ list represents an index in the returned cell array. In this case only one measurement list was created, so we can visualize the data by calling the corresponding timetable.
data{1}
ans=60×3 timetable
Time Line PWM Sine
________ ________ ___ ______
0 sec 0.037586 2 2.4959
0.05 sec 0.070794 2 2.9093
0.1 sec 0.097341 2 3.2087
0.15 sec 0.12736 2 3.5013
0.2 sec 0.1576 2 3.7355
0.25 sec 0.18727 2 3.897
0.3 sec 0.21725 2 3.985
0.35 sec 0.24726 2 3.994
0.4 sec 0.27729 2 3.9233
0.45 sec 0.30807 2 3.7712
0.5 sec 0.33725 2 3.5582
0.55 sec 0.36728 2 3.2778
0.6 sec 0.39725 2 2.9469
0.65 sec 0.42727 2 2.5779
0.7 sec 0.45725 2 2.1862
0.75 sec 0.48724 0 1.7871
⋮
Retrieve the Sine
Measurement Data
To retrieve the acquired data from the XCP channel for the Sine
measurement, find the data in the output timetable from readDAQList
function.
plot(data{1}.Sine, "o-") title("Sine Measurement Data") xlabel("Data Point") ylabel("Data Value")
Retrieve the Line
Measurement Data
To retrieve the acquired data from the XCP channel for the Line
measurement, find the data in the output timetable from readDAQList
function.
plot(data{1}.Line, "o-") title("Line Measurement Data") xlabel("Data Point") ylabel("Data Value")
Retrieve the PWM
Measurement Data
To retrieve the acquired data from the XCP channel for the PWM
measurement, find the data in the output timetable from readDAQList
function.
plot(data{1}.PWM, "o-") title("PWM 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 sampleServer a2lInfo