//数据接收函数 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); } }
private void AddData(ZCAN_Receive_Data[] data, uint len) { list_box_data_ = ""; list_box_data_ = ""; for (uint i = 0; i < len; ++i) { ZCAN_Receive_Data can = data[i]; uint id = data[i].frame.can_id; string eff = IsEFF(id) ? "扩展帧" : "标准帧"; string rtr = IsRTR(id) ? "远程帧" : "数据帧"; list_box_data_ = String.Format("接收到CAN ID:0x{0:X8} {1:G} {2:G} 长度:{3:D1} 数据:", GetId(id), eff, rtr, can.frame.can_dlc); for (uint j = 0; j < can.frame.can_dlc; ++j) { list_box_data_ = String.Format("{0:G}{1:X2} ", list_box_data_, can.frame.data[j]); } } Object[] list = { this, System.EventArgs.Empty }; this.listBox.BeginInvoke(new EventHandler(SetListBox), list); }