User Tools

Site Tools

blog:2023-11-02_c_zlg_zcan_receive_data_function



2023-11-02 C#: ZLG ZCAN_Receive_Data Function

  • This is the ZCAN receive code piece
  • *
    // CAN data receive function
    protected void RecvDataFunc()
    {
      ZCAN_Receive_Data[]   can_data   = new ZCAN_Receive_Data[100];
      ZCAN_ReceiveFD_Data[] canfd_data = new ZCAN_ReceiveFD_Data[100];
      uint len;
    
      while (m_bStart)
      ...

Full Code

  • // CAN data receive function
    protected void RecvDataFunc()
    {
      ZCAN_Receive_Data[]   can_data   = new ZCAN_Receive_Data[100];
      ZCAN_ReceiveFD_Data[] canfd_data = new ZCAN_ReceiveFD_Data[100];
      uint len;
    
      while (m_bStart)
      {
        lock (locker)
        {
          len = Method.ZCAN_GetReceiveNum(channel_handle_, TYPE_CAN);
          if (len > 0)
          {
            int size = Marshal.SizeOf(typeof(ZCAN_Receive_Data));
            IntPtr ptr  = Marshal.AllocHGlobal((int)len * size);
            len = Method.ZCAN_Receive(channel_handle_, ptr, len, 50);
            for (int i = 0; i < len; ++i)
            {
              can_data[i] = (ZCAN_Receive_Data)Marshal.PtrToStructure(
                (IntPtr)((UInt32)ptr + i * size), typeof(ZCAN_Receive_Data));
            }
            OnRecvCANDataEvent(can_data, len);
            Marshal.FreeHGlobal(ptr);
          }
    
          len = Method.ZCAN_GetReceiveNum(channel_handle_, TYPE_CANFD);
          if (len > 0)
          {
            int size = Marshal.SizeOf(typeof(ZCAN_ReceiveFD_Data));
            IntPtr ptr  = Marshal.AllocHGlobal((int)len * size);
            len = Method.ZCAN_ReceiveFD(channel_handle_, ptr, len, 50);
            for (int i = 0; i < len; ++i)
            {
              canfd_data[i] = (ZCAN_ReceiveFD_Data)Marshal.PtrToStructure(
                (IntPtr)((UInt32)ptr + i * size), typeof(ZCAN_ReceiveFD_Data));
            }
            OnRecvFDDataEvent(canfd_data, len);
            Marshal.FreeHGlobal(ptr);
          }
        }
    
        Thread.Sleep(10);
      }
    }

TAGS

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

Permalink blog/2023-11-02_c_zlg_zcan_receive_data_function.txt · Last modified: 2023/11/02 11:26 by jethro

oeffentlich