我的USB条形码扫描仪有点问题。我正在使用带有“SerialPort”类的Scanner: this._barcodeScanner = new SerialPort(comPort, 9600, Parity.None, 8, StopBits.One) { Handshake = Handshake.None, ReadTimeout = 500, WriteTimeout = 500 }; this._barcodeScanner.Open(); this._barcodeScanner.DataReceived += BarcodeScannerCallback;如果我通过“SerialPort”类打开USB设备,我就无法正常关闭软件,虚拟端口永远保持打开状态,或直到我重新启动整个计算机。所以我的问题是,在我通过C#代码拔出设备后,有没有办法关闭虚拟端口?问候[编辑#1]好的,还有一些代码:这样,如果插入设备,我每隔10秒检查一次: private bool CheckUsbDeviceAvailability() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName WHERE PortName = '" + this.PortName + "'"); if (searcher.Get().Count > 0) return true; return false; }那是串口的Callback-Event:void BarcodeScannerCallback(object sender, SerialDataReceivedEventArgs e) { Thread.Sleep(500); string data = this._barcodeScanner.ReadExisting().Replace(Convert.ToChar(2), Convert.ToChar(32)).Trim(); if (data.StartsWith("AX")) { string[] arrData = data.Split('\n'); this._barcodeScanner.StopAvailabilityThread(); Barcode code = new Barcode(arrData[0].Replace("\r", "")); if (CheckIfBarcodeExists(code)) this.UpdateBarcodeNode(code); else this.CreateBarcodeNode(code); BarcodeScannerCallbackEvent(sender, e, code); this._barcodeScanner.StartAvailabilityThread(); } this._barcodeScanner.ComDevicePluggedIn = ScannerDevice.ComAvailabilityState.Available; }如果它不再回答它将触发“DeviceNotAvailableEvent()”: void BarcodeScannerDeviceNotAvailableEvent() { this._barcodeScanner.Close(); this._barcodeScanner.Dispose(); }我已经覆盖了“SerialPort”类的Dispose事件,以便它将中止Thread:protected override void Dispose(bool isDisposing) { if (isDisposing) { this._deviceAvailableThread.Abort(); } base.Dispose(isDisposing); }
- 3 回答
- 0 关注
- 425 浏览
添加回答
举报
0/150
提交
取消