User Tools

Site Tools

blog:2023-11-02_c_zlg_usbcan_clear_buffer



2023-11-02 C#: ZLG USBCAN Clear Buffer

  • Here is the test code for clear ZLG USBCAN
  • protected void Task_ZlgClearReceiveBuffer()
    {
      ZCAN_Receive_Data[] can_data = new ZCAN_Receive_Data[10];
      ZCAN_ReceiveFD_Data[] canfd_data = new ZCAN_ReceiveFD_Data[10];
    
      uint len;
      Thread.Sleep(1000);
    
      len = Method.ZCAN_GetReceiveNum(VAR_ZlgUsbCanChannelHandle, 0); // TYPE_CAN
      Console.WriteLine("ZLG USBCAN archived CAN frames are: " + len.ToString());
      ...

Full Code

  • protected void Task_ZlgClearReceiveBuffer()
    {
      ZCAN_Receive_Data[] can_data = new ZCAN_Receive_Data[10];
      ZCAN_ReceiveFD_Data[] canfd_data = new ZCAN_ReceiveFD_Data[10];
    
      uint len;
      Thread.Sleep(1000);
    
      len = Method.ZCAN_GetReceiveNum(VAR_ZlgUsbCanChannelHandle, 0); // TYPE_CAN
      Console.WriteLine("ZLG USBCAN archived CAN frames are: " + len.ToString());
    
      if (len > 0)
      {
        int size = Marshal.SizeOf(typeof(ZCAN_Receive_Data));
        IntPtr ptr = Marshal.AllocHGlobal((int)len * size);
        len = Method.ZCAN_Receive(VAR_ZlgUsbCanChannelHandle, ptr, len, 50);
        //for (int i = 0; i < len; ++i)
        //{
        //    // Drop the message
        //    can_data[0] = (ZCAN_Receive_Data)Marshal.PtrToStructure(
        //        (IntPtr)((Int64)ptr + i * size), typeof(ZCAN_Receive_Data));
        //}
        // OnRecvCANDataEvent(can_data, len);
        Marshal.FreeHGlobal(ptr);
      }
    
      len = Method.ZCAN_GetReceiveNum(VAR_ZlgUsbCanChannelHandle, 1); // TYPE_CANFD
      Console.WriteLine("ZLG USBCAN archived CAN-FD frames are: " + len.ToString());
      if (len > 0)
      {
        int size = Marshal.SizeOf(typeof(ZCAN_ReceiveFD_Data));
        IntPtr ptr = Marshal.AllocHGlobal((int)len * size);
        len = Method.ZCAN_ReceiveFD(VAR_ZlgUsbCanChannelHandle, ptr, len, 50);
        //for (int i = 0; i < len; ++i)
        //{
        //    // Drop the message
        //    canfd_data[0] = (ZCAN_ReceiveFD_Data)Marshal.PtrToStructure(
        //        (IntPtr)((Int64)ptr + i * size), typeof(ZCAN_ReceiveFD_Data));
        //}
        // OnRecvCANDataEvent(can_data, len);
        Marshal.FreeHGlobal(ptr);
      }
      Thread.Sleep(10);
    }

TAGS

  • 60 person(s) visited this page until now.

Permalink blog/2023-11-02_c_zlg_usbcan_clear_buffer.txt · Last modified: 2023/11/02 11:25 by jethro

oeffentlich