Exemplo n.º 1
0
        /// <summary>
        /// CAN卡数据接收线程
        /// </summary>
        private unsafe void TRecv()
        {
            while (!T_CANRecv_NeedDel)
            {
                if (IsCANopen)
                {
                    //check is have data
                    uint res = CanCmd.CAN_GetReceiveCount(CANDeviceHandle, canDeviceChannel);
                    if (res == 0)
                    {
                        continue;//not receive data
                    }

                    /////////////////////////////////////
                    UInt32 con_maxlen = 500;
                    IntPtr pt         = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CAN_DataFrame)) * (Int32)con_maxlen);

                    res = CanCmd.CAN_ChannelReceive(CANDeviceHandle, canDeviceChannel, pt, con_maxlen, 100);
                    ////////////////////////////////////////////////////////
                    if (res == 0)
                    {
                        // 读取错误信息
                        CAN_ErrorInformation err = new CAN_ErrorInformation();
                        // 必须调用此函数
                        if (CanCmd.CAN_GetErrorInfo(CANDeviceHandle, canDeviceChannel, ref err) == CanCmd.CAN_RESULT_OK)
                        {  // CAN通讯有错误
                           // 处理错误信息
                            Info?.Invoke("[E]CAN通讯有错误: " + DecodeErrCode(err.uErrorCode));
                        }
                        else
                        {  // 没有收到CAN数据
                            Info?.Invoke("[E]没有收到CAN数据,获取错误信息失败");
                        }
                        CanCmd.CAN_ClearReceiveBuffer(CANDeviceHandle, canDeviceChannel);
                    }
                    else
                    {
                        WriteDataToCANRecvData = true;
                        for (UInt32 i = 0; i < res; i++)
                        {
                            CAN_DataFrame obj = (CAN_DataFrame)Marshal.PtrToStructure((IntPtr)((UInt64)pt + (UInt64)(i * Marshal.SizeOf(typeof(CAN_DataFrame)))), typeof(CAN_DataFrame));
                            CANRecvedData.AddLast(obj);
                        }
                    }
                    WriteDataToCANRecvData = false;
                    Marshal.FreeHGlobal(pt);
                }
            }
        }
Exemplo n.º 2
0
 public static extern uint CAN_GetErrorInfo(uint dwDeviceHandle, uint dwChannel, ref CAN_ErrorInformation pErr);