Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using jnsoft.ASAP2.Values;
- using jnsoft.ASAP2;
- using jnsoft.ASAP2.XCP;
- using System;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace jnsoft.Comm.XCP.Examples
- {
- /// <summary>
- /// ASAP2 XCP Sample console application.
- ///
- /// Demonstrating a simple create connection to an ECU.
- /// Usage: ASAP2XCPExample.exe ASAP2Example.a2l
- /// </summary>
- class Program
- {
- const string fTime = "HH:mm:ss,fff";
- static void Main(string[] args)
- {
- var prevColor = Console.ForegroundColor;
- try
- {
- using (var a2lParser = new A2LParser())
- {
- a2lParser.parse(args[0]);
- var module = a2lParser.Project.getNode<A2LMODULE>(false);
- var modPar = module.getNode<A2LMOD_PAR>(false);
- string a2lEPK = modPar.EPK.Trim(new char[] { '\0', ' ' });
- // Create a XCP communication master to connect to a slave
- // using XCP over TCP to slave address 127.0.0.1:1800
- var xcpMedia = module.getXCPInterfaces().First(x => x.Type == A2LType.XCP_ON_TCP_IP);
- xcpMedia.getSettings(out A2LXCP_PROTOCOL_LAYER protocol, out A2LXCP_CHECKSUM Checksum
- , out A2LXCP_PAG PAG, out A2LXCP_DAQ DAQ, out A2LXCP_PGM PGM);
- using (var xcpMaster = new XCPMaster(ConnectBehaviourType.Manual
- , XCPType.TCP, "127.0.0.1", 1800, protocol, DAQ
- , onConnectionStateChanged, onXCPEventReceived, onXCPServiceRequestReceived, onXCPErrorReceived
- ))
- { // try to connect to ECU
- Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Connecting...EPK should be {a2lEPK}... (press any key to quit)");
- RespConnect respConnect;
- CmdResult result;
- while (CmdResult.OK != (result = xcpMaster.Connect(ConnectMode.Normal, out respConnect)))
- { // wait until connected
- Thread.Sleep(1000);
- if (Console.KeyAvailable)
- // exit process after keypress
- return;
- }
- // connect sucessful -> query GetStatus
- RespGetStatus respState;
- if (CmdResult.OK != (result = xcpMaster.GetStatus(out respState)))
- // exit process after failed query
- return;
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Session state={respState.SessionState}");
- #region query EPK on Address 0, length 30
- byte[] respData;
- if (CmdResult.OK == xcpMaster.ShortUpload(32, 0, 0, out respData))
- { // upload successful
- string deviceEPK = Encoding.Default.GetString(respData).Trim(new char[] { '\0', ' ', '\xff' });
- Console.WriteLine($"{DateTime.Now.ToString(fTime)}: EPK from device is: '{deviceEPK}'");
- }
- #endregion
- #region Reading a characteristic from ECU's memory
- // Create an empty datafile based on the A2L memory segments (serves for any characteristic)
- var dataFile = new DataFileS19(null, module.createInitialMemorySegments(false));
- // Get a characteristic from the A2L Definition
- var characteristic = a2lParser.Project.CharDict.Values.First();
- // Read the data from the ECU
- xcpMaster.readCharacteristic(ECUPage.RAM, dataFile, characteristic);
- // Get the characteristic's value from the datafile
- var charValue = CharacteristicValue.createValue(dataFile, characteristic, Helpers.ValueObjectFormat.Physical);
- // e.g. print the value
- Console.WriteLine($"Characteristic {characteristic.Name} ({((A2LCHARACTERISTIC)characteristic).CharType}) = {charValue.toSingleValue()}");
- #endregion
- }
- }
- }
- finally
- { // reset starting color
- Console.ForegroundColor = prevColor;
- }
- }
- // Callback receiving XCP service requests
- static void onXCPServiceRequestReceived(object sender, XCPServiceRequestArgs e)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Received service request: {e.ReceivedServiceRequest.ServiceRequestCode}");
- }
- // Callback receiving XCP events
- static void onXCPEventReceived(object sender, XCPEventArgs e)
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Received event: {e.ReceivedEvent.EventCode}");
- }
- // Callback receiving XCP errors
- static void onXCPErrorReceived(object sender, XCPErrorArgs e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Received error: {e.ErrorCode}");
- }
- // Callback receiving XCP connection state changes
- static void onConnectionStateChanged(object sender, EventArgs e)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- XCPMaster master = (XCPMaster)sender;
- Console.WriteLine($"{DateTime.Now.ToString(fTime)}: State: {(master.Connected ? "connected" : "disconnected")}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement