Beispiel #1
0
        /// <summary>
        ///  接收数据处理函数
        /// </summary>
        void ReceiveDataProc()
        {
            try
            {
                //接收到数据长度
                int _receivedLen = 0;
                //CAN ERR INFO
                CANApi.VCI_ERR_INFO _CanErrInfo = new CANApi.VCI_ERR_INFO();
                _CanErrInfo.Passive_ErrData = new byte[3];
                //CAN Frames
                CANApi.VCI_CAN_OBJ[] _CanRawFrames = new CANApi.VCI_CAN_OBJ[50];
                while (true)
                {
                    Thread.Sleep(10);
                    if (!_mConnected)
                    {
                        break;
                    }
                    _receivedLen = 0;
                    _receivedLen = (int)CANApi.VCI_Receive(
                        _mDeviceType, _DeviceIndex,
                        _CanIndex,
                        ref _CanRawFrames[0], 50, 200);
                    if (_receivedLen <= 0)
                    {
                        //注意:如果没有读到数据则必须调用此函数来读取出当前的错误码,
                        //千万不能省略这一步(即使你可能不想知道错误码是什么)
                        CANApi.VCI_ReadErrInfo(
                            _mDeviceType, _DeviceIndex,
                            _CanIndex, ref _CanErrInfo);
                    }
                    else
                    {
                        for (int i = 0; i < _receivedLen; i++)
                        {
                            if (i > _CanRawFrames.Length)
                            {
                                break;
                            }
                            _CanRawFrame = _CanRawFrames[i];
                            //ID
                            _frameID = string.Format("{0:X8}", _CanRawFrame.ID);
                            //timestamp
                            if (_CanRawFrame.TimeFlag == 0)
                            {
                                _timestamp = "无";
                            }
                            else
                            {
                                _timestamp = string.Format("{0:X}", _CanRawFrame.TimeStamp);
                            }
                            //FrameFormat
                            if (_CanRawFrame.RemoteFlag == 0)
                            {
                                _frameFormat = "数据帧";
                            }
                            else
                            {
                                _frameFormat = "远程帧";
                            }
                            //FrameType
                            if (_CanRawFrame.ExternFlag == 0)
                            {
                                _frameType = "标准帧";
                            }
                            else
                            {
                                _frameType = "扩展帧";
                            }
                            //Data
                            _stringBuilder.Clear();
                            for (int j = 0; j < _CanRawFrame.DataLen; j++)
                            {
                                if (_CanRawFrame.Data.Length <= j)
                                {
                                    break;
                                }
                                _stringBuilder.AppendFormat("{0:X2}", _CanRawFrame.Data[j]);
                            }
                            _frameData = _stringBuilder.ToString();

                            _ReceviedList.Add(new FrameInfo(_frameID, _timestamp, _frameFormat, _frameType, _frameData));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog("CANhelper.ReceiveDataProc." + e.Message);
            }
        }
Beispiel #2
0
        /// <summary>
        ///  接收数据处理函数
        /// </summary>
        void ReceiveDataProc()
        {
            try
            {
                //接收到数据长度
                int _receivedLen = 0;
                //CAN ERR INFO
                CANApi.VCI_ERR_INFO _CanErrInfo = new CANApi.VCI_ERR_INFO();
                _CanErrInfo.Passive_ErrData = new byte[3];
                //CAN Frames
                CANApi.VCI_CAN_OBJ[] _CanRawFrames = new CANApi.VCI_CAN_OBJ[50];
                while (true)
                {
                    Thread.Sleep(10);
                    if (!_mConnected)
                        break;
                    _receivedLen = 0;
                    _receivedLen = (int)CANApi.VCI_Receive(
                        _mDeviceType, _DeviceIndex,
                        _CanIndex,
                        ref _CanRawFrames[0], 50, 200);
                    if (_receivedLen <= 0)
                    {
                        //注意:如果没有读到数据则必须调用此函数来读取出当前的错误码,
                        //千万不能省略这一步(即使你可能不想知道错误码是什么)
                        CANApi.VCI_ReadErrInfo(
                            _mDeviceType, _DeviceIndex,
                            _CanIndex, ref _CanErrInfo);
                    }
                    else
                    {
                        for (int i = 0; i < _receivedLen; i++)
                        {
                            if (i > _CanRawFrames.Length)
                                break;
                            _CanRawFrame = _CanRawFrames[i];
                            //ID
                            _frameID = string.Format("{0:X8}", _CanRawFrame.ID);
                            //timestamp
                            if (_CanRawFrame.TimeFlag == 0)
                            {
                                _timestamp = "无";
                            }
                            else
                            {
                                _timestamp = string.Format("{0:X}", _CanRawFrame.TimeStamp);
                            }
                            //FrameFormat
                            if (_CanRawFrame.RemoteFlag == 0)
                            {
                                _frameFormat = "数据帧";
                            }
                            else
                            {
                                _frameFormat = "远程帧";
                            }
                            //FrameType
                            if (_CanRawFrame.ExternFlag == 0)
                            {
                                _frameType = "标准帧";
                            }
                            else
                            {
                                _frameType = "扩展帧";
                            }
                            //Data
                            _stringBuilder.Clear();
                            for (int j = 0; j < _CanRawFrame.DataLen; j++)
                            {
                                if (_CanRawFrame.Data.Length <= j) break;
                                _stringBuilder.AppendFormat("{0:X2}", _CanRawFrame.Data[j]);
                            }
                            _frameData = _stringBuilder.ToString();

                            _ReceviedList.Add(new FrameInfo(_frameID, _timestamp, _frameFormat, _frameType, _frameData));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog("CANhelper.ReceiveDataProc." + e.Message);
            }
        }