Exemple #1
0
        ///////////////////////////////////////////////////////////////////////////////////////////
        public Channel(uint mChannelIndex, string mChannelName, Device pParentDevice)
        {
            this.m_IsStarted     = false;
            this.m_IsInitialized = false;

            this.p_StatusTimer          = new Timer();
            this.p_StatusTimer.Interval = DEFAULT_STATUS_ERRORINFO_TIMER_INTERVAL;
            this.p_StatusTimer.Elapsed += StatusTimer_Elapsed;

            this.p_RcvTimer          = new Timer();
            this.p_RcvTimer.Interval = DEFAULT_RCV_TIMER_INTERVAL;
            this.p_RcvTimer.Elapsed += RcvTimer_Elapsed;

            this.p_SendTimer          = new Timer();
            this.p_SendTimer.Interval = DEFAULT_SEND_TIMER_INTERVAL;
            this.p_SendTimer.Elapsed += SendTimer_Elapsed;

            this.m_ChannelIndex = mChannelIndex;
            this.m_ChannelName  = mChannelName;
            this.p_ParentDevice = pParentDevice;

            this.p_InitConfig = CAN.CreateBasicInitConfig();
            this.m_BaudRate   = CAN.CHANNEL_DEFAULT_BAUDRATE;

            this.InitCAN(this.m_BaudRate, ref this.p_InitConfig);

            this.p_StatusTimer.Start();
            this.p_RcvTimer.Start();
            this.p_SendTimer.Start();
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        public static INIT_CONFIG CreateBasicInitConfig()
        {
            INIT_CONFIG pInitConfig = new INIT_CONFIG();

            pInitConfig.AccCode = CAN.CHANNEL_DEFAULT_ACC_CODE;
            pInitConfig.AccMask = CAN.CHANNEL_DEFAULT_ACC_MASK;
            pInitConfig.Filter  = CAN.CHANNEL_DEFAULT_FILTER;

            ConfigBaudRate(CAN.CHANNEL_DEFAULT_BAUDRATE, ref pInitConfig);
            ConfigMode(CAN_MODE.NORMAL, ref pInitConfig);

            return(pInitConfig);
        }
Exemple #4
0
 public static extern UInt32 InitCAN(UInt32 DeviceType, UInt32 DeviceIndex, UInt32 CANIndex, ref INIT_CONFIG pInitConfig);
Exemple #5
0
        public static void ConfigBaudRate(int mBaudRate, ref INIT_CONFIG pInitConfig)
        {
            switch (mBaudRate)
            {
            case 1000:
                pInitConfig.Timing0 = 0x00;
                pInitConfig.Timing1 = 0x14;
                break;

            case 800:
                pInitConfig.Timing0 = 0x00;
                pInitConfig.Timing1 = 0x16;
                break;

            case 666:
                pInitConfig.Timing0 = 0x80;
                pInitConfig.Timing1 = 0xb6;
                break;

            case 500:
                pInitConfig.Timing0 = 0x00;
                pInitConfig.Timing1 = 0x1c;
                break;

            case 400:
                pInitConfig.Timing0 = 0x80;
                pInitConfig.Timing1 = 0xfa;
                break;

            case 250:
                pInitConfig.Timing0 = 0x01;
                pInitConfig.Timing1 = 0x1c;
                break;

            case 200:
                pInitConfig.Timing0 = 0x81;
                pInitConfig.Timing1 = 0xfa;
                break;

            case 125:
                pInitConfig.Timing0 = 0x03;
                pInitConfig.Timing1 = 0x1c;
                break;

            case 100:
                pInitConfig.Timing0 = 0x04;
                pInitConfig.Timing1 = 0x1c;
                break;

            case 80:
                pInitConfig.Timing0 = 0x83;
                pInitConfig.Timing1 = 0xff;
                break;

            case 50:
                pInitConfig.Timing0 = 0x09;
                pInitConfig.Timing1 = 0x1c;
                break;

            case 40:
                pInitConfig.Timing0 = 0x87;
                pInitConfig.Timing1 = 0xff;
                break;

            case 20:
                pInitConfig.Timing0 = 0x18;
                pInitConfig.Timing1 = 0x1c;
                break;

            case 10:
                pInitConfig.Timing0 = 0x31;
                pInitConfig.Timing1 = 0x1c;
                break;

            case 5:
                pInitConfig.Timing0 = 0xbf;
                pInitConfig.Timing1 = 0xff;
                break;

            default:
                pInitConfig.Timing0 = 0x00;
                pInitConfig.Timing1 = 0x14;
                break;
            }
        }
Exemple #6
0
 public static void ConfigMode(CAN_MODE mode, ref INIT_CONFIG pInitConfig)
 {
     pInitConfig.Mode = (byte)mode;
 }