Exemplo n.º 1
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="baudrate">波特率</param>
        /// <param name="boardInfo">设备信息</param>
        /// <returns></returns>
        public EcanXcpResult GcCanInitialize(out BOARD_INFO boardInfo)
        {
            INIT_CONFIG   initConfig   = new INIT_CONFIG();   // 创建CAN初始化配置结构体
            FILTER_RECORD filterRecord = new FILTER_RECORD(); // 定义CAN滤波器的滤波范围

            boardInfo.hw_Version     = 0x00;
            boardInfo.fw_Version     = 0x00;
            boardInfo.dr_Version     = 0x00;
            boardInfo.in_Version     = 0x00;
            boardInfo.irq_Num        = 0x00;
            boardInfo.can_Num        = 0x00;
            boardInfo.str_Serial_Num = new byte[] { 0x00 };
            boardInfo.str_hw_Type    = new byte[] { 0x00 };
            boardInfo.Reserved       = new ushort[] { 0x00 };

            // 滤波设置
            initConfig.AccCode = ((UInt32)(slaveID) << 21);
            initConfig.AccMask = 0x001FFFFF;
            // 初始化配置滤波使能
            initConfig.Filter = 1;

            filterRecord.ExtFrame = 0;
            filterRecord.Start    = masterID;
            filterRecord.End      = slaveID;

            // 初始化配置波特率
            switch (baudrate)
            {
            case "1000K":

                initConfig.Timing0 = 0;
                initConfig.Timing1 = 0x14;
                break;

            case "800K":

                initConfig.Timing0 = 0;
                initConfig.Timing1 = 0x16;
                break;

            case "666K":

                initConfig.Timing0 = 0x80;
                initConfig.Timing1 = 0xb6;
                break;

            case "500K":

                initConfig.Timing0 = 0;
                initConfig.Timing1 = 0x1c;
                break;

            case "400K":

                initConfig.Timing0 = 0x80;
                initConfig.Timing1 = 0xfa;
                break;

            case "250K":

                initConfig.Timing0 = 0x01;
                initConfig.Timing1 = 0x1c;
                break;

            case "200K":

                initConfig.Timing0 = 0x81;
                initConfig.Timing1 = 0xfa;
                break;

            case "125K":

                initConfig.Timing0 = 0x03;
                initConfig.Timing1 = 0x1c;
                break;

            case "100K":

                initConfig.Timing0 = 0x04;
                initConfig.Timing1 = 0x1c;
                break;

            case "80K":

                initConfig.Timing0 = 0x83;
                initConfig.Timing1 = 0xff;
                break;

            case "50K":

                initConfig.Timing0 = 0x09;
                initConfig.Timing1 = 0x1c;
                break;
            }

            // 初始化配置为正常模式
            initConfig.Mode = 0;

            // 打开CAN
            if (ECANDLL.OpenDevice(deviceType, deviceIndex, canIndex) == ECAN.ECANStatus.STATUS_OK)
            {
                // 初始化CAN
                if (ECANDLL.InitCAN(deviceType, deviceIndex, canIndex, ref initConfig) == ECAN.ECANStatus.STATUS_OK)
                {
                    // 设置CAN 滤波器的滤波范围 实测不起作用
                    if (ECANDLL.SetReference(deviceType, deviceIndex, canIndex, 1, ref filterRecord) == ECAN.ECANStatus.STATUS_OK)
                    {
                        // 启动USBCAN设备的通道0
                        if (ECANDLL.StartCAN(deviceType, deviceIndex, canIndex) == ECAN.ECANStatus.STATUS_OK)
                        {
                            if (ECANDLL.ReadBoardInfo(deviceType, deviceIndex, out boardInfo) == ECAN.ECANStatus.STATUS_OK)
                            {
                                return(EcanXcpResult.XCP_ERR_OK);
                            }
                            else
                            {
                                return(EcanXcpResult.XCP_ERR_GENERIC);
                            }
                        }
                        else
                        {
                            return(EcanXcpResult.XCP_ERR_GENERIC);
                        }
                    }
                    else
                    {
                        return(EcanXcpResult.XCP_ERR_GENERIC);
                    }
                }
                else
                {
                    ECANDLL.CloseDevice(deviceType, deviceIndex);
                    return(EcanXcpResult.XCP_ERR_GENERIC);
                }
            }
            else
            {
                return(EcanXcpResult.XCP_ERR_GENERIC);
            }
        }