Example #1
0
        /// <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);
            }
        }
Example #2
0
 public CAN_FRAME(CAN_OBJ pCANObj, DateTime time, CAN_FRAME_DIRECTION direction, CAN_FRAME_STATUS status)
 {
     this.CANObj     = pCANObj;
     this.Time       = time;
     this.Direction  = direction;
     this.Status     = status;
     this.CANErrInfo = new CAN_ERR_INFO();
 }
Example #3
0
        /// <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);
            }
        }
Example #4
0
 public static extern UInt32 Transmit(UInt32 DeviceType, UInt32 DeviceIndex, UInt32 CANIndex, ref CAN_OBJ pSend, UInt32 Length);