Beispiel #1
0
 public static extern UInt32 VCI_InitCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_INIT_CONFIG pInitConfig);
        /// <summary>
        /// 连接/断开按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_open_Click(object sender, RoutedEventArgs e)
        {
            if (mbopen == 1)                                         //如果设备已开启就关闭设备
            {
                controlCAN.VCI_CloseDevice(mdevicetype, mdeviceind); //关闭设备
                timer.Stop();
                mbopen = 0;
                //Btn_Connect.Content = "连接";
            }
            else
            {
                UInt32 rel = controlCAN.VCI_OpenDevice(mdevicetype, mdeviceind, mdevicereserved);//打开设备
                if (rel != 1)
                {
                    MessageBox.Show(("打开设备失败!"), ("警告"),
                                    MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                VCI_INIT_CONFIG config = new VCI_INIT_CONFIG();                                    //初始化can参数的结构体

                config.AccCode = 0x00000000;                                                       //验收码
                config.AccMask = 0xffffffff;                                                       //屏蔽码
                config.Timing0 = 0x00;                                                             //波特率
                config.Timing1 = 0x1c;                                                             //500k
                config.Filter  = 1;                                                                //滤波方式,单滤波
                config.Mode    = 0;                                                                //模式,正常模式

                UInt32 ret = controlCAN.VCI_InitCAN(mdevicetype, mdeviceind, mcanind, ref config); //初始化can
                if (ret != 1)
                {
                    MessageBox.Show("InitCAN失败", "错误",
                                    MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                mbopen = 1;


                //}

                if (istarted)
                {
                    controlCAN.VCI_ResetCAN(mdevicetype, mdeviceind, mcanind);
                }
                else
                {
                    UInt32 rtn = controlCAN.VCI_StartCAN(mdevicetype, mdeviceind, mcanind);
                    istarted = !istarted;
                    if (rtn != 1)
                    {
                        MessageBox.Show("StartCAN失败", "错误",
                                        MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }

                //实例化对象
                timer = new DispatcherTimer();
                //设置触发时间
                timer.Interval = TimeSpan.FromSeconds(0.0001);
                //设置触发事件
                timer.Tick += timer_Tick;
                //启动
                timer.Start();

                //Btn_Connect.Content = "断开";
            }


            Btn_Open.Content = mbopen == 1 ? "断开" : "连接";//调节按钮
        }