/// <summary> /// 发送CAN帧数据 /// </summary> /// <param name="length">需要发送的帧数</param> /// <returns>成功发送的帧数</returns> private uint Transmit(uint length) { uint nSuccessNum = 0; for (int index = 0; index < length; index++) { CAN_FRAME pCANFrame; if (this.p_CANSendingBufQueue.TryDequeue(out pCANFrame)) { if (CANDLL.Transmit((uint)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex, ref pCANFrame.CANObj, 1) == 1) { pCANFrame.Status = CAN_FRAME_STATUS.SUCCESS; pCANFrame.Time = DateTime.Now; nSuccessNum++; Logger.Debug(string.Format("channel[{0}] transmit a message successful.", m_ChannelName)); } else { CAN_ERR_INFO pCANErrInfo = new CAN_ERR_INFO(); if (ReadErrInfo(ref pCANErrInfo) == (uint)CAN_RESULT.SUCCESSFUL) { pCANFrame.CANErrInfo = pCANErrInfo; } pCANFrame.Status = CAN_FRAME_STATUS.FAILED; pCANFrame.Time = DateTime.Now; Logger.Info(string.Format("channel[{0}] transmit a message failed: [{1}]", m_ChannelName, pCANErrInfo.ErrCode)); } this.p_CANSendedBufQueue.Enqueue(pCANFrame); } } Logger.Info(string.Format("channel[{0}] transmit [{1}/{2}] messages successful.", m_ChannelName, nSuccessNum, length)); return(nSuccessNum); }
/// <summary> /// 复位CAN /// </summary> /// <returns></returns> public uint ResetCAN() { if (!this.m_IsStarted) { Logger.Info(string.Format("channel[{0}] already reset.", m_ChannelName)); return((uint)CAN_RESULT.SUCCESSFUL); } if (CANDLL.ResetCAN((UInt32)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("channel[{0}] reset successful.", m_ChannelName)); this.m_IsStarted = false; return((uint)CAN_RESULT.SUCCESSFUL); } CAN_ERR_INFO pCANErrorInfo = new CAN_ERR_INFO(); uint result = (uint)CAN_RESULT.ERR_UNKNOWN; if (ReadErrInfo(ref pCANErrorInfo) == (uint)CAN_RESULT.SUCCESSFUL) { result = pCANErrorInfo.ErrCode; } Logger.Info(string.Format("channel[{0}] reset failed: [0x{1}].", m_ChannelName, result.ToString("x"))); return(result); }
/// <summary> /// 初始化CAN /// </summary> /// <param name="mBaudRate">波特率</param> /// <param name="pInitConfig">初始化CAN的配置参数</param> /// <returns></returns> public uint InitCAN(int mBaudRate, ref INIT_CONFIG pInitConfig) { INIT_CONFIG pConfig = CAN.CreateBasicInitConfig(); CAN.ConfigBaudRate(m_BaudRate, ref pConfig); if (pConfig.Timing0 != pInitConfig.Timing0 || pConfig.Timing1 != pInitConfig.Timing1) { Logger.Info(string.Format("baudrate[{0}] and initConfig[0x{1}:0x{2}] not match", m_BaudRate, pInitConfig.Timing0.ToString("x"), pInitConfig.Timing1.ToString("x"))); return((uint)CAN_RESULT.ERR_UNKNOWN); } this.m_BaudRate = mBaudRate; this.p_InitConfig = pInitConfig; if (CANDLL.InitCAN((UInt32)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex, ref pInitConfig) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("init channel[{0}] successful.", m_ChannelName)); this.m_IsInitialized = true; return((uint)CAN_RESULT.SUCCESSFUL); } this.m_IsInitialized = false; uint result = (uint)CAN_RESULT.ERR_UNKNOWN; CAN_ERR_INFO pCANErrorInfo = new CAN_ERR_INFO(); if (ReadErrInfo(ref pCANErrorInfo) == (uint)CAN_RESULT.SUCCESSFUL) { result = pCANErrorInfo.ErrCode; } Logger.Info(string.Format("init channel[{0}] failed: [0x{1}].", m_ChannelName, result.ToString("x"))); return(result); }
/// <summary> /// 发送CAN帧 /// </summary> /// <param name="pCanFrames">发送的CAN帧数组</param> /// <param name="length">需要发送的帧数</param> /// <returns>成功发送的帧数</returns> unsafe public uint Transmit(CAN_FRAME[] pCanFrames, uint length) { uint realSendNum = 0; try { //设置发送失败后重发的超时时间 uint nTimeOut = DEFAULT_RESEND_INTERVAL; SetReference((uint)CAN_REFERENCE_REFTYPE.CONFIG_RESEND_TIMEOUT, (byte *)&nTimeOut); CAN_OBJ[] pCANObjs = new CAN_OBJ[length]; for (int index = 0; index < length; index++) { pCANObjs[index] = pCanFrames[index].CANObj; } realSendNum = CANDLL.Transmit((uint)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex, ref pCANObjs[0], (uint)pCANObjs.Length); Logger.Info(string.Format("channel[{0}] transmit [{1}/{2}] messages successful.", m_ChannelName, realSendNum, pCANObjs.Length)); return(realSendNum); } catch (Exception e) { Logger.Error(string.Format("channel[{0}] transmit exception.", m_ChannelName), e); return(realSendNum); } }
/// <summary> /// 打开设备 /// </summary> /// <returns></returns> public uint OpenDevice() { if (this.m_IsDeviceOpen) { Logger.Info(string.Format("device[{0}] already open", this.GetDeviceName())); return((uint)CAN_RESULT.SUCCESSFUL); } if (CANDLL.OpenDevice((UInt32)m_DeviceType, m_DeviceIndex, 0) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("open device[{0}] successful", this.GetDeviceName())); m_IsDeviceOpen = true; return((uint)CAN_RESULT.SUCCESSFUL); } uint result = (uint)CAN_RESULT.ERR_UNKNOWN; CAN_ERR_INFO pCANErrorInfo = new CAN_ERR_INFO(); if (CANDLL.ReadErrInfo((UInt32)m_DeviceType, m_DeviceIndex, -1, ref pCANErrorInfo) == CAN.CAN_DLL_RESULT_SUCCESS) { result = pCANErrorInfo.ErrCode; } Logger.Info(string.Format("open device[{0}] failed: [0x{1}]", this.GetDeviceName(), result.ToString("x"))); return(result); }
/// <summary> /// 读取CAN的指定配置参数 /// </summary> /// <param name="refType">参数类型</param> /// <param name="data">读取到的值</param> /// <returns></returns> public uint GetReference(uint refType, ref byte data) { if (CANDLL.GetReference((UInt32)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex, refType, ref data) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("channel[{0}] read reference successful.", m_ChannelName)); return((uint)CAN_RESULT.SUCCESSFUL); } Logger.Info(string.Format("channel[{0}] read reference failed.", m_ChannelName)); return((uint)CAN_RESULT.ERR_UNKNOWN); }
/// <summary> /// 读取CAN状态 /// </summary> /// <param name="pCanStatus"></param> /// <returns></returns> public uint ReadCanStatus(ref CAN_STATUS pCanStatus) { if (CANDLL.ReadCanStatus((UInt32)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex, ref pCanStatus) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("channel[{0}] read can status successful.", m_ChannelName)); return((uint)CAN_RESULT.SUCCESSFUL); } Logger.Info(string.Format("channel[{0}] read can status failed.", m_ChannelName)); return((uint)CAN_RESULT.ERR_UNKNOWN); }
/// <summary> /// 读取CAN最近一次错误信息 /// </summary> /// <param name="pCanErrInfo"></param> /// <returns></returns> public uint ReadErrInfo(ref CAN_ERR_INFO pCanErrInfo) { if (CANDLL.ReadErrInfo((UInt32)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, (int)m_ChannelIndex, ref pCanErrInfo) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("channel[{0}] read error info successful.", m_ChannelName)); return((uint)CAN_RESULT.SUCCESSFUL); } Logger.Info(string.Format("channel[{0}] read error info failed.", m_ChannelName)); return((uint)CAN_RESULT.ERR_UNKNOWN); }
/// <summary> /// 清空CAN缓冲区 /// </summary> /// <returns></returns> public uint ClearBuffer() { if (CANDLL.ClearBuffer((UInt32)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("channel[{0}] clear buffer successful.", m_ChannelName)); return((uint)CAN_RESULT.SUCCESSFUL); } uint result = (uint)CAN_RESULT.ERR_UNKNOWN; CAN_ERR_INFO pCANErrorInfo = new CAN_ERR_INFO(); if (ReadErrInfo(ref pCANErrorInfo) == (uint)CAN_RESULT.SUCCESSFUL) { result = pCANErrorInfo.ErrCode; } Logger.Info(string.Format("channel[{0}] clear buffer failed: [0x{1}].", m_ChannelName, result.ToString("x"))); return(result); }
/// <summary> /// 设置CAN的指定类型配置参数 /// </summary> /// <param name="refType">参数类型</param> /// <param name="data">设置的值</param> /// <returns></returns> unsafe public uint SetReference(uint refType, byte *data) { if (CANDLL.SetReference((uint)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex, refType, data) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("channel[{0}] set reference successful.", m_ChannelName)); return((uint)CAN_RESULT.SUCCESSFUL); } uint result = (uint)CAN_RESULT.ERR_UNKNOWN; CAN_ERR_INFO pCANErrorInfo = new CAN_ERR_INFO(); if (ReadErrInfo(ref pCANErrorInfo) == (uint)CAN_RESULT.SUCCESSFUL) { result = pCANErrorInfo.ErrCode; } Logger.Info(string.Format("channel[{0}] set reference failed: [0x{1}].", m_ChannelName, result.ToString("x"))); return(result); }
/// <summary> /// 读取板卡信息 /// </summary> /// <returns></returns> private uint ReadBoardInfo() { if (CANDLL.ReadBoardInfo((UInt32)m_DeviceType, m_DeviceIndex, ref p_DeviceInfo) == CAN.CAN_DLL_RESULT_SUCCESS) { Logger.Info(string.Format("device[{0}] read board info successful", this.GetDeviceName())); return((uint)CAN_RESULT.SUCCESSFUL); } uint result = (uint)CAN_RESULT.ERR_UNKNOWN; CAN_ERR_INFO pCANErrorInfo = new CAN_ERR_INFO(); if (CANDLL.ReadErrInfo((UInt32)m_DeviceType, m_DeviceIndex, -1, ref pCANErrorInfo) == CAN.CAN_DLL_RESULT_SUCCESS) { result = pCANErrorInfo.ErrCode; } Logger.Info(string.Format("device[{0}] read board info failed: [0x{1}]", this.GetDeviceName(), result.ToString("x"))); return(result); }
/// <summary> /// 接收CAN帧数据 /// </summary> /// <param name="pCanFrames">接收到的帧数组</param> /// <param name="length">需要接收的帧数</param> /// <param name="waitMilliseconds">等待时间ms</param> /// <returns>实际接收的帧数</returns> private uint Receive(out CAN_FRAME[] pCanFrames, uint length, int waitMilliseconds) { uint realRcvNum = 0; pCanFrames = null; try { IntPtr pReceive = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CAN_OBJ)) * (Int32)length); realRcvNum = CANDLL.Receive((uint)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex, pReceive, length, waitMilliseconds); if (realRcvNum != uint.MaxValue) { Logger.Info(string.Format("channel[{0}] receive [{1}/{2}] messages successful.", m_ChannelName, realRcvNum, length)); pCanFrames = new CAN_FRAME[realRcvNum]; for (int index = 0; index < realRcvNum; index++) { CAN_OBJ pCANObj = (CAN_OBJ)Marshal.PtrToStructure((IntPtr)((UInt32)pReceive + index * Marshal.SizeOf(typeof(CAN_OBJ))), typeof(CAN_OBJ)); pCanFrames[index] = new CAN_FRAME(pCANObj, DateTime.Now, CAN_FRAME_DIRECTION.RECEIVE, CAN_FRAME_STATUS.SUCCESS); } Marshal.FreeHGlobal(pReceive); return(realRcvNum); } Marshal.FreeHGlobal(pReceive); CAN_ERR_INFO pCANErrorInfo = new CAN_ERR_INFO(); uint result = (uint)CAN_RESULT.ERR_UNKNOWN; if (ReadErrInfo(ref pCANErrorInfo) == (uint)CAN_RESULT.SUCCESSFUL) { result = pCANErrorInfo.ErrCode; } Logger.Info(string.Format("channel[{0}] receive failed: [0x{1}].", m_ChannelName, result.ToString("x"))); return(realRcvNum); } catch (Exception e) { Logger.Error(string.Format("channel[{0}] receive exception.", m_ChannelName), e); return(realRcvNum); } }
/// <summary> /// 读取CAN接收到的帧数 /// </summary> /// <returns></returns> private uint GetReceiveNum() { return(CANDLL.GetReceiveNum((uint)p_ParentDevice.DeviceType, p_ParentDevice.DeviceIndex, m_ChannelIndex)); }