Example #1
0
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (m_bOpen == 1)
            {
                VciNativeMethods.VCI_CloseDevice(m_devtype, m_devind);
                m_bOpen = 0;
            }
            else
            {
                m_devtype = (uint)m_arrdevtype[comboBox_devtype.SelectedIndex];

                m_devind = (UInt32)comboBox_DevIndex.SelectedIndex;
                m_canind = (UInt32)comboBox_CANIndex.SelectedIndex;
                var rc = VciNativeMethods.VCI_OpenDevice(m_devtype, m_devind, 0);
                if (rc == 0)
                {
                    MessageBox.Show("打开设备失败,请检查设备类型和设备索引号是否正确", "错误",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                //rc = VciNativeMethods.VCI_OpenDevice(m_devtype, m_devind, 0);
                //var error = new VCI_ERR_INFO();
                //VciNativeMethods.VCI_ReadErrInfo(m_devtype, m_devind, 0, ref error);

                rc = VciNativeMethods.VCI_ClearBuffer(m_devtype, m_devind, m_canind);

                m_bOpen = 1;
                var config = new VCI_INIT_CONFIG();
                config.AccCode = Convert.ToUInt32("0x" + textBox_AccCode.Text, 16);
                config.AccMask = Convert.ToUInt32("0x" + textBox_AccMask.Text, 16);
                config.Timing0 = Convert.ToByte("0x" + textBox_Time0.Text, 16);
                config.Timing1 = Convert.ToByte("0x" + textBox_Time1.Text, 16);
                config.Filter  = (FilterType)comboBox_Filter.SelectedIndex;
                config.Mode    = (WorkMode)comboBox_Mode.SelectedIndex;
                VciNativeMethods.VCI_InitCAN(m_devtype, m_devind, m_canind, ref config);

                // 读取板卡信息
                var boardInf = new VCI_BOARD_INFO();
                VciNativeMethods.VCI_ReadBoardInfo(m_devtype, m_devind, ref boardInf);
                //var serialNo = Encoding.ASCII.GetString(boardInf.str_Serial_Num, 0, boardInf.str_Serial_Num.Length);
                //Console.WriteLine(serialNo);
            }

            buttonConnect.Text = m_bOpen == 1 ? "断开" : "连接";
            timer_rec.Enabled  = m_bOpen == 1;
            //button_StartCAN.Enabled = m_bOpen == 1;
            //button_StopCAN.Enabled = m_bOpen == 1;
        }