Advertisement
alesi2000

ASAP2 XCP Sample console application

Apr 13th, 2012 (edited)
1,733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using jnsoft.ASAP2.Values;
  2. using jnsoft.ASAP2;
  3. using jnsoft.ASAP2.XCP;
  4. using System;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. namespace jnsoft.Comm.XCP.Examples
  10. {
  11.   /// <summary>
  12.   /// ASAP2 XCP Sample console application.
  13.   ///
  14.   /// Demonstrating a simple create connection to an ECU.
  15.   /// Usage: ASAP2XCPExample.exe ASAP2Example.a2l
  16.   /// </summary>
  17.   class Program
  18.   {
  19.     const string fTime = "HH:mm:ss,fff";
  20.     static void Main(string[] args)
  21.     {
  22.       var prevColor = Console.ForegroundColor;
  23.       try
  24.       {
  25.         using (var a2lParser = new A2LParser())
  26.         {
  27.           a2lParser.parse(args[0]);
  28.  
  29.           var module = a2lParser.Project.getNode<A2LMODULE>(false);
  30.           var modPar = module.getNode<A2LMOD_PAR>(false);
  31.           string a2lEPK = modPar.EPK.Trim(new char[] { '\0', ' ' });
  32.  
  33.           // Create a XCP communication master to connect to a slave
  34.           // using XCP over TCP to slave address 127.0.0.1:1800
  35.           var xcpMedia = module.getXCPInterfaces().First(x => x.Type == A2LType.XCP_ON_TCP_IP);
  36.           xcpMedia.getSettings(out A2LXCP_PROTOCOL_LAYER protocol, out A2LXCP_CHECKSUM Checksum
  37.             , out A2LXCP_PAG PAG, out A2LXCP_DAQ DAQ, out A2LXCP_PGM PGM);
  38.  
  39.           using (var xcpMaster = new XCPMaster(ConnectBehaviourType.Manual
  40.            , XCPType.TCP, "127.0.0.1", 1800, protocol, DAQ
  41.            , onConnectionStateChanged, onXCPEventReceived, onXCPServiceRequestReceived, onXCPErrorReceived
  42.            ))
  43.           { // try to connect to ECU
  44.             Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Connecting...EPK should be {a2lEPK}... (press any key to quit)");
  45.             RespConnect respConnect;
  46.             CmdResult result;
  47.             while (CmdResult.OK != (result = xcpMaster.Connect(ConnectMode.Normal, out respConnect)))
  48.             { // wait until connected
  49.               Thread.Sleep(1000);
  50.               if (Console.KeyAvailable)
  51.                 // exit process after keypress
  52.                 return;
  53.             }
  54.  
  55.             // connect sucessful -> query GetStatus
  56.             RespGetStatus respState;
  57.             if (CmdResult.OK != (result = xcpMaster.GetStatus(out respState)))
  58.               // exit process after failed query
  59.               return;
  60.  
  61.             Console.ForegroundColor = ConsoleColor.Yellow;
  62.             Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Session state={respState.SessionState}");
  63.  
  64.             #region query EPK on Address 0, length 30
  65.  
  66.             byte[] respData;
  67.             if (CmdResult.OK == xcpMaster.ShortUpload(32, 0, 0, out respData))
  68.             { // upload successful
  69.               string deviceEPK = Encoding.Default.GetString(respData).Trim(new char[] { '\0', ' ', '\xff' });
  70.               Console.WriteLine($"{DateTime.Now.ToString(fTime)}: EPK from device is: '{deviceEPK}'");
  71.             }
  72.  
  73.             #endregion
  74.  
  75.             #region Reading a characteristic from ECU's memory
  76.  
  77.             // Create an empty datafile based on the A2L memory segments (serves for any characteristic)
  78.             var dataFile = new DataFileS19(null, module.createInitialMemorySegments(false));
  79.  
  80.             // Get a characteristic from the A2L Definition
  81.             var characteristic = a2lParser.Project.CharDict.Values.First();
  82.  
  83.             // Read the data from the ECU
  84.             xcpMaster.readCharacteristic(ECUPage.RAM, dataFile, characteristic);
  85.  
  86.             // Get the characteristic's value from the datafile
  87.             var charValue = CharacteristicValue.createValue(dataFile, characteristic, Helpers.ValueObjectFormat.Physical);
  88.  
  89.             // e.g. print the value
  90.             Console.WriteLine($"Characteristic {characteristic.Name} ({((A2LCHARACTERISTIC)characteristic).CharType}) = {charValue.toSingleValue()}");
  91.  
  92.             #endregion
  93.           }
  94.         }
  95.       }
  96.       finally
  97.       { // reset starting color
  98.         Console.ForegroundColor = prevColor;
  99.       }
  100.     }
  101.     // Callback receiving XCP service requests
  102.     static void onXCPServiceRequestReceived(object sender, XCPServiceRequestArgs e)
  103.     {
  104.       Console.ForegroundColor = ConsoleColor.White;
  105.       Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Received service request: {e.ReceivedServiceRequest.ServiceRequestCode}");
  106.     }
  107.     // Callback receiving XCP events
  108.     static void onXCPEventReceived(object sender, XCPEventArgs e)
  109.     {
  110.       Console.ForegroundColor = ConsoleColor.Yellow;
  111.       Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Received event: {e.ReceivedEvent.EventCode}");
  112.     }
  113.     // Callback receiving XCP errors
  114.     static void onXCPErrorReceived(object sender, XCPErrorArgs e)
  115.     {
  116.       Console.ForegroundColor = ConsoleColor.Red;
  117.       Console.WriteLine($"{DateTime.Now.ToString(fTime)}: Received error: {e.ErrorCode}");
  118.     }
  119.     // Callback receiving XCP connection state changes
  120.     static void onConnectionStateChanged(object sender, EventArgs e)
  121.     {
  122.       Console.ForegroundColor = ConsoleColor.Green;
  123.       XCPMaster master = (XCPMaster)sender;
  124.       Console.WriteLine($"{DateTime.Now.ToString(fTime)}: State: {(master.Connected ? "connected" : "disconnected")}");
  125.     }
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement