/// <summary> /// 通过USB端口检查仪器状态的Thread /// </summary> private void CheckInstrumentState() { bool oldUsbState = false; while (true) { bool curUsbState = false; System.Management.ManagementObjectSearcher wmiSearcher = new System.Management.ManagementObjectSearcher("Select * From WIN32_USBControllerDevice"); foreach (System.Management.ManagementObject wmi_USB in wmiSearcher.Get()) { string strPath = wmi_USB.Path.RelativePath; if (strPath.Contains(SettingData.settingData.runing_para.usbDeviceID)) //这是VSPEC仪器的Device ID(硬件管理器-详细信息-硬件ID { curUsbState = true; break; } } //没有检测到USB设备,表示仪器没有连接上 if (curUsbState == false) { Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new UpdateInstrumentDeletage(UpdateInstrument), false); oldUsbState = false; } else if (oldUsbState == false) //如果上次已经连接了,不用再连接 { oldUsbState = Common.VspecInstrument.Connect(); //读取Json string string paraString = VspecInstrument.GetParametersTable(); //读取SystemType string[] systemTypeString = paraString.Split(',')[0].Split(':'); string systemType = systemTypeString[1].ToString(); switch (systemType) { case "1": Common.SettingData.systemType = Common.SystemTypeEnum.Fibre; break; case "2": Common.SettingData.systemType = Common.SystemTypeEnum.IntegrateSphere; break; default: return; } //Common.SettingData.systemType = systemType == "1" ? Common.SystemTypeEnum.Fibre : SystemTypeEnum.IntegrateSphere; //读取仪器序列号 string para = VspecInstrument.GetParametersTable(); string temp = para.Split(',')[1].Split(':')[1].Replace('\"', ' '); // temp=temp.Substring(1,temp.Length-2); SettingData.settingData.runing_para.serialNo = System.Text.RegularExpressions.Regex.Replace(temp, @"\s", "");// para == null ? "读取仪器序列号失败!" : temp; Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new DisplayModelDelegate(DisplayModel), SettingData.settingData.runing_para.serialNo); Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new UpdateInstrumentDeletage(UpdateInstrument), oldUsbState); } else { Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new UpdateInstrumentDeletage(UpdateInstrument), true); } Thread.Sleep(3 * 1000); //5秒检查一次 } }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { //如果从Setup面板中直接退出,要保存设置后的数据 if (curOperatePanel is NirIdentifier.SystemSetup.SetupPanel) { SettingData.settingData.Serialize(SettingData.settingData.filename); } //终止状态检查线程 if (instrumentTread != null && instrumentTread.IsAlive) { instrumentTread.Abort(); } //断开仪器连接 VspecInstrument.Disconnect(); }