How to implement the UDS protocol over xl-driver library for sending Diagnostics on c#

847 Views Asked by At

I am new to xl-driver library

can not find any example code on xl-driver library

is there anyone who know how to send uds on c# via xl-driver library?

have checked xl-driver library docu.

1

There are 1 best solutions below

1
On

The XL-Driver-Library allows you to access different Bussystems (CAN/LIN/MOST/... - corresponding to the Data-Link-Layer). However it will not handle UDS directly for you; you will need to implement the logic for the Transport Layer (i.e. ISOTP) and Application Layer (i.e. UDS) using the XL Driver Library by yourself. This includes:

  • Segmentation/Desegmentation
  • Sending/Receiving of Flow Controls
  • Assigning the UDS Payload (i.e. 0x22 F1 90)
  • Interpreting the UDS Payload (i.e. 0x62 F1 90 61 62 63 64 ...)

An simple example to initialize the CAN channels is as following:

static int Main(string[] args) {
      XLDefine.XL_Status status = xlDriver.XL_OpenDriver();
      status = xlDriver.XL_GetDriverConfig(ref driverConfig);
      // If the application name cannot be found in VCANCONF...
      if ((xlDriver.XL_GetApplConfig(appName, 0, ref hwType, ref hwIndex, ref hwChannel, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN) != XLDefine.XL_Status.XL_SUCCESS) ||
          (xlDriver.XL_GetApplConfig(appName, 1, ref hwType, ref hwIndex, ref hwChannel, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN) != XLDefine.XL_Status.XL_SUCCESS)) {
        //...create the item with two CAN channels
        xlDriver.XL_SetApplConfig(appName, 0, XLDefine.XL_HardwareType.XL_HWTYPE_NONE, 0, 0, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN);
        xlDriver.XL_SetApplConfig(appName, 1, XLDefine.XL_HardwareType.XL_HWTYPE_NONE, 0, 0, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN);
        xlDriver.XL_PopupHwConfig();
      }
      // Request the user to assign channels until both CAN1 (Tx) and CAN2 (Rx) are assigned to usable channels
      while (!GetAppChannelAndTestIsOk(0, ref txMask, ref txCi) || !GetAppChannelAndTestIsOk(1, ref rxMask, ref rxCi)) {
        xlDriver.XL_PopupHwConfig();
      }
      UInt64 accessMask = txMask | rxMask;
      UInt64 permissionMask = accessMask;
      status = xlDriver.XL_OpenPort(ref portHandle, appName, accessMask, ref permissionMask, 1024, XLDefine.XL_InterfaceVersion.XL_INTERFACE_VERSION, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN);
      status = xlDriver.XL_CanRequestChipState(portHandle, accessMask);
      status = xlDriver.XL_ActivateChannel(portHandle, accessMask, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN, XLDefine.XL_AC_Flags.XL_ACTIVATE_NONE);
      int tempInt = -1;
      status = xlDriver.XL_SetNotification(portHandle, ref tempInt, 1);
      xlEvWaitHandle.SafeWaitHandle = new SafeWaitHandle(new IntPtr(tempInt), true);
      status = xlDriver.XL_ResetClock(portHandle);
      Thread rxThread = new Thread(new ThreadStart(RXThread));
      rxThread.Start();
      while (true) {
        string str = Console.ReadLine();
        // extend here to transmit Messages
      }
      rxThread.Abort();
      return 0;
    }

The RxThread could be defined like this:

public static void RXThread() {
      // Create new object containing received data 
      XLClass.xl_event receivedEvent = new XLClass.xl_event();
      // Result of XL Driver function calls
      XLDefine.XL_Status xlStatus = XLDefine.XL_Status.XL_SUCCESS; 
      // Note: this thread will be destroyed by MAIN
      while (true) {
        // Wait for hardware events
        if (xlEvWaitHandle.WaitOne(1000)) {
          // ...init xlStatus first
          xlStatus = XLDefine.XL_Status.XL_SUCCESS;
          // afterwards: while hw queue is not empty...
          while (xlStatus != XLDefine.XL_Status.XL_ERR_QUEUE_IS_EMPTY) {
            // ...block RX thread to generate RX-Queue overflows
            while (blockRxThread) { Thread.Sleep(1000); }
            // ...receive data from hardware.
            xlStatus = xlDriver.XL_Receive(portHandle, ref receivedEvent);
            //  If receiving succeed....
            if (xlStatus == XLDefine.XL_Status.XL_SUCCESS) {
              if ((receivedEvent.flags & XLDefine.XL_MessageFlags.XL_EVENT_FLAG_OVERRUN) != 0) {
              }
              // ...and data is a Rx msg...
              if (receivedEvent.tag == XLDefine.XL_EventTags.XL_RECEIVE_MSG) {
                if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_OVERRUN) != 0) {
                }
                // ...check various flags
                if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME)
                    == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME) {
                } else if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME)
                    == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME) {
                }
                else {
                }
              }
            }
          }
        }
        // No event occurred
      }
    }

The XL Driver Library contains a number of samples in C# to get started in the C:\Users\Public\Documents\Vector\XL Driver Library xx.xx.xx\samples\NET directory. If you want to use UDS-on-CAN i.e. xlCANdemo_Csharp

If you rather want to use UDS directly you would need to use other APIs like SAE J2534 or D-PDU API which can abstract the Transport Protocol Handling if an channel of such protocol is being used.