Example #1
0
        public bool Open(UInt32 DeviceType, ref INIT_CONFIG init)
        {
            if (IsOpened)
            {
                return(true);
            }

            if (ECANDLL.OpenDevice(DeviceType, 0, 0) != ECANStatus.STATUS_OK)
            {
                MessageBox.Show("Usb Can打开失败", "错误", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }

            if (ECANDLL.InitCAN(DeviceType, 0, 0, ref init) != ECANStatus.STATUS_OK)
            {
                MessageBox.Show("Usb Can初始化失败", "错误", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }

            if (ECANDLL.ClearBuffer(DeviceType, 0, 0) != ECANStatus.STATUS_OK)
            {
                MessageBox.Show("清除CAN缓冲区失败", "错误", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }

            if (ECANDLL.StartCAN(DeviceType, 0, 0) != ECANStatus.STATUS_OK)
            {
                MessageBox.Show("启动CAN失败", "错误", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }
            else
            {
                opened = true;
                Console.WriteLine("USB CAN open successfully!");
                return(true);
            }
        }
Example #2
0
        private bool OpenCan()
        {
            if (ECANDLL.OpenDevice(1, 0, 0) != ECAN.ECANStatus.STATUS_OK)
            {
                return(false);                               //若未打开usb转can设备则返回false
            }

            if (ECANDLL.InitCAN(1, 0, 0, ref init_config) != ECAN.ECANStatus.STATUS_OK)
            {
                ECANDLL.CloseDevice(1, 0);
                return(false);                               //若未能初始化CAN设备,则关闭can设备并返回false
            }

            if (ECANDLL.StartCAN(1, 0, 0) != ECAN.ECANStatus.STATUS_OK)
            {
                ECANDLL.CloseDevice(1, 0);
                return(false);                               //若未能启动CAN设备,则关闭can设备并返回false
            }

            return(true);
        }
Example #3
0
        public void openDevice()
        {
            try {
                INIT_CONFIG init_config = new INIT_CONFIG();

                init_config.AccCode = 0;
                init_config.AccMask = 0xffffff;
                init_config.Filter  = 0;
                init_config.Timing0 = 0x00;
                init_config.Timing1 = 0x1c;
                init_config.Mode    = model;
                if (ECANDLL.OpenDevice(4, 0, 0) != ECAN.ECANStatus.STATUS_OK)
                {
                    openResult(false);
                    return;
                }
                //Set can1 baud
                if (ECANDLL.InitCAN(4, 0, 0, ref init_config) != ECAN.ECANStatus.STATUS_OK)
                {
                    openResult(false);
                    ECANDLL.CloseDevice(1, 0);
                    return;
                }
                if (ECANDLL.StartCAN(1, 0, 0) == ECAN.ECANStatus.STATUS_OK)
                {
                    //MessageBox.Show("Start CAN1 Success");
                    openResult(true);
                }
                else
                {
                    openResult(false);
                }
            }catch (Exception e) {
                logger.Error("ECAN 打开失败: " + e);
            }
        }
Example #4
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);
            }
        }
Example #5
0
        /// <summary>
        /// open and close can device, baud is 500kbps
        /// </summary>
        private void canOpenClose()
        {
            if (m_connect == 1)
            {
                m_connect             = 0;
                this.mCan.EnableProc  = false;
                this.mCan.EnableProc2 = false;

                this.can_device_open = false;
                ECANDLL.CloseDevice(1, 0);
                btnInit.Text = "打开CAN";

                this.can_device_open = false;
                this.stopAllThread();

                /*
                 * btnReset.Enabled = false;
                 * btnWrite.Enabled = false;
                 * btnSendFile.Enabled = false;
                 * checkBoxCan1.Enabled = true;
                 * checkBoxCan2.Enabled = true;
                 */
                return;
            }

            INIT_CONFIG init_config = new INIT_CONFIG();

            init_config.AccCode = 0;
            init_config.AccMask = 0xffffff;
            init_config.Filter  = 0;

            switch (m_Baudrate)
            {
            case 0:     //1000

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

            case 1:     //800

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

            case 2:     //666

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

            case 3:     //500

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

            case 4:    //400

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

            case 5:    //250

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

            case 6:    //200

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

            case 7:    //125

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

            case 8:    //100

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

            case 9:    //80

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

            case 10:    //50

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

            init_config.Mode = 0;


            if (ECANDLL.OpenDevice(1, 0, 0) != ECAN.ECANStatus.STATUS_OK)
            {
                MessageBox.Show("Open device fault!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }
            //Set can1 baud
            if (ECANDLL.InitCAN(1, 0, 0, ref init_config) != ECAN.ECANStatus.STATUS_OK)
            {
                MessageBox.Show("Init can fault!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                ECANDLL.CloseDevice(1, 0);
                return;
            }


            //set can2 baud

            switch (m_Baudrate2)
            {
            case 0:     //1000

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

            case 1:     //800

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

            case 2:     //666

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

            case 3:     //500

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

            case 4:    //400

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

            case 5:    //250

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

            case 6:    //200

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

            case 7:    //125

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

            case 8:    //100

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

            case 9:    //80

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

            case 10:    //50

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

            init_config.Mode = 0;

            if (cmbCanPorts.SelectedItem.ToString() == "CAN-1")
            {
                if (ECANDLL.InitCAN(1, 0, 1, ref init_config) != ECAN.ECANStatus.STATUS_OK)
                {
                    MessageBox.Show("Init can fault!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    ECANDLL.CloseDevice(1, 0);
                    return;
                }
            }


            // enable the timer to rec can data frame from can device.
            m_connect            = 1;
            this.can_device_open = true;
            if (cmbCanPorts.SelectedItem.ToString() == "CAN-0")
            {
                this.mCan.EnableProc = false;

                //this.mCan.ThreadReadDataFormDevice.Start();
                //this.ThreadUpdateTable.Start();
                //this.ThreadGetCanDataBuff.Start();
                this.startAllThread();

                DateTimeStamp = DateTime.Now;  // this is the time base of the charts
            }
            else
            {
                this.mCan.EnableProc = false;
            }
            if (cmbCanPorts.SelectedItem.ToString() == "CAN-1")
            {
                this.mCan.EnableProc2 = false;
                DateTimeStamp         = DateTime.Now;
            }
            else
            {
                this.mCan.EnableProc2 = false;
            }

            btnInit.Text = "关闭CAN";

            /*
             * btnReset.Enabled = true;
             * btnWrite.Enabled = true;
             * btnSendFile.Enabled = true;
             * IncludeTextMessage("Open Success");
             *
             * if (checkBoxCan2.Checked)
             *  btnWrite2.Enabled = true;
             *
             * checkBoxCan1.Enabled = false;
             * checkBoxCan2.Enabled = false;
             * */
            cmbCanPorts.Enabled = false;

            if (m_connect == 0)
            {
                MessageBox.Show("Not open device!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            //Start Can1
            if (cmbCanPorts.SelectedItem.ToString() == "CAN-0")
            {
                if (ECANDLL.StartCAN(1, 0, 0) == ECAN.ECANStatus.STATUS_OK)
                {
                    //IncludeTextMessage("Start CAN1 Success");
                    tmrRead.Enabled = false;
                }

                else
                {
                    //IncludeTextMessage("Start Fault");
                }
            }
            //start CAN2
            if (cmbCanPorts.SelectedItem.ToString() == "CAN-1")
            {
                if (ECANDLL.StartCAN(1, 0, 1) == ECAN.ECANStatus.STATUS_OK)
                {
                    //IncludeTextMessage("Start CAN2 Success");
                    tmrRead.Enabled = false;
                }
                else
                {
                    //IncludeTextMessage("Start Fault");
                }
            }
        }