private static BarcodeScanner CreateBarcodeScannerObject(BarcodeScannerConfig scannerConfig, BarcodeScanner barcodeScanner, ManagementBaseObject detectedDevice) { Dictionary <string, string> scannerProperties = new Dictionary <string, string>(); foreach (var propertyName in scannerConfig.PropertyNames) { if (detectedDevice[propertyName] != null) { var name = detectedDevice[propertyName]; scannerProperties.Add(propertyName, detectedDevice[propertyName].ToString()); } } string re1 = ".*?"; // Non-greedy match on filler string re2 = "((?:[a-z][a-z]*[0-9]+[a-z0-9]*))"; // Alphanum 1 Regex r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline); Match m = null; foreach (var propValPair in scannerProperties) { m = r.Match(propValPair.Value); if (m.Success) { break; } } if (m != null && m.Success) { barcodeScanner = new BarcodeScanner(); barcodeScanner.COMPortName = m.Groups[1].ToString(); barcodeScanner.Description = scannerProperties[scannerConfig.Key2]; barcodeScanner.PNPDeviceID = scannerProperties[scannerConfig.Key1]; barcodeScanner.Properties = scannerProperties; } else { var cantFindBarcodeScanner = new Exception("Could Not find " + scannerConfig.Value2 + " " + scannerConfig.Key1 + " with Value: " + scannerConfig.Value1 + "\n Please Check the configurations!"); cantFindBarcodeScanner.WriteLog().SaveToDataBase().Display(); } return(barcodeScanner); }
public async Task <string> GetScannedBarcode(BarcodeScanner barcodeScanner, CancellationToken token) { OpenSerialPort(barcodeScanner.SerialPort, barcodeScanner.COMPortName); List <string> rxVals = new List <string>(); bool FinishedReading = false; string rxString = string.Empty; while (!FinishedReading) { rxVals.AddRange(await readStringsfromSerialPortAsync(barcodeScanner, token)); rxString = string.Join("", rxVals.ToArray()); int bytesInBuffer = barcodeScanner.SerialPort.BytesToRead; FinishedReading = bytesInBuffer == 0; } CloseSerialPort(barcodeScanner.SerialPort, barcodeScanner.COMPortName); return(rxString); }