private ScannerConnectResult InitScanners() { string strGetPortName = string.Empty; foreach (var port in SerialPort.GetPortNames()) { strGetPortName = port; } if (strGetPortName.Length == 0) { return(null); } ScannerConnectResult result = new ScannerConnectResult(); string strErrMessgae = this.BaseClass.GetResourceValue("ERR_BARSCN_DEVICE_NOT_FOUND"); try { var strPortName = string.Empty; SerialPortInfo _SerialPortInfo = this.SerialPortLoad(); if (_SerialPortInfo != null) { if (this.g_serialPort == null) { this.g_serialPort = new SerialPort(); var strSerialPortNo = $"COM{_SerialPortInfo.PortNo.ToWhiteSpaceOrString()}"; this.g_serialPort.PortName = strSerialPortNo; this.g_serialPort.BaudRate = _SerialPortInfo.BaudRate; this.g_serialPort.DataBits = 8; this.g_serialPort.StopBits = StopBits.One; this.g_serialPort.Parity = Parity.None; this.g_serialPort.Handshake = Handshake.None; this.g_serialPort.DataReceived += SerialPort_DataReceived; this.g_serialPort.Open(); } if (this.g_serialPort?.IsOpen == true) { result.Result = true; } else { result.Result = false; result.Message = strErrMessgae; } } } catch (System.IO.IOException ex) { result.Result = false; result.Message += "\r\n" + ex.Message; } catch (Exception ex) { result.Result = false; result.Message = ex.Message; } return(result); }
public ScannerConnectResult ConnectScanner() { this.g_connectResult = InitScanners(); return(this.g_connectResult); }