/// <summary> /// 打开设备和初始化CAN0&CAN1 /// </summary> private bool OpendeDeviceAndCAN() { #region 打开设备和初始化CAN0&CAN1 if (VCI_OpenDevice(m_devtype, m_devind, 0) != 1) { if (MessageBox.Show("打开设备失败,请检查设备类型和设备索引号是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) { return false; } } m_bOpen = 1; VCI_INIT_CONFIG config = new VCI_INIT_CONFIG(); config.AccCode = m_acccode; config.AccMask = m_accmask; config.Timing0 = m_timer0; config.Timing1 = m_timer1; config.Filter = m_filter; config.Mode = m_mode; if (VCI_InitCAN(m_devtype, m_devind, can0_index, ref config) != 1) { if (MessageBox.Show("初始化CAN 1失败,请检查连接是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) { return false; } } if (VCI_InitCAN(m_devtype, m_devind, can1_index, ref config) != 1) { if (MessageBox.Show("初始化CAN 2失败,请检查连接是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) { return false; } } if (VCI_StartCAN(m_devtype, m_devind, can0_index) != 1) { if (MessageBox.Show("启动CAN 1失败,请检查连接是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) { return false; } } if (VCI_StartCAN(m_devtype, m_devind, can1_index) != 1) { if (MessageBox.Show("启动CAN 2失败,请检查连接是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) { return false; } } return true; #endregion }
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 btnConnect_Click(object sender, EventArgs e) { if (m_bOpen == 1) { VCI_CloseDevice(m_devtype, m_devind); m_bOpen = 0; } else { m_devtype = m_arrdevtype[comboBox_devtype.SelectedIndex];//comboBox_devtype m_devind = (UInt32)comboBox_DevIndex.SelectedIndex; m_canind = (UInt32)comboBox_CANIndex.SelectedIndex; if (VCI_OpenDevice(m_devtype, m_devind, 0) == 0) { MessageBox.Show("打开设备失败,请检查设备类型和设备索引号是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } m_bOpen = 1; VCI_INIT_CONFIG config = new VCI_INIT_CONFIG(); config.AccCode = System.Convert.ToUInt32("0x" + textBox_AccCode.Text, 16); config.AccMask = System.Convert.ToUInt32("0x" + textBox_AccMask.Text, 16); config.Timing0 = System.Convert.ToByte("0x" + textBox_Time0.Text, 16); config.Timing1 = System.Convert.ToByte("0x" + textBox_Time1.Text, 16); config.Filter = (Byte)comboBox_Filter.SelectedIndex; config.Mode = (Byte)comboBox_Mode.SelectedIndex; VCI_InitCAN(m_devtype, m_devind, m_canind, ref config); } btnConnect.Text = m_bOpen == 1 ? "断开" : "连接"; timer_rec.Enabled = m_bOpen == 1 ? true : false; }