public void CloseCANDevice() { try { _mConnected = false; Thread.Sleep(300); //等待接收线程退出 CANApi.VCI_CloseDevice(_mDeviceType, _canSetting.DeviceIndex); } catch (Exception e) { LogHelper.WriteInfoLog("接收线程退出\n" + e.Message); } }
/// <summary> /// 连接CAN设备 /// </summary> public void ConnectCANDevice() { try { if (_mConnected) { return; } if (!_mConnected) { //open device _openCode = CANApi.VCI_OpenDevice(_mDeviceType, _canSetting.DeviceIndex, 0); if (_openCode != CANApi.STATUS_OK) { throw new CANexception("打开设备失败,VCI_OpenDevice 返回值:" + _openCode); } //init device _CanConfig = _canSetting.GetConfig(); _initCode = CANApi.VCI_InitCAN( _mDeviceType, _canSetting.DeviceIndex, _canSetting.CanIndex, ref _CanConfig); if (_initCode != CANApi.STATUS_OK) { LogHelper.WriteErrorLog("初始化CAN失败,VCI_InitCAN 返回值:" + _initCode); CANApi.VCI_CloseDevice(_mDeviceType, _canSetting.DeviceIndex); } if (_openCode == CANApi.STATUS_OK && _initCode == CANApi.STATUS_OK) { _mConnected = true; } } } catch (CANexception e) { LogHelper.WriteErrorLog(e.Message); MessageBox.Show(e.Message); } }