public SerialPortConfig[] Detect(IEnumerable <SerialPortConfig> configs) { lock (_locker) { List <SerialPortConfig> detected = new List <SerialPortConfig>(); string[] ports = GetPortNames(); int total = ports.Length * configs.Count(); int tested = 0; OnProgress(0, detected.Select(c => new SerialPortConfig(c))); bool cancel = false; Parallel.ForEach(ports, port => { if (!cancel) { for (int j = 0; j < configs.Count(); j++) { SerialPortConfig config = new SerialPortConfig(configs.ElementAt(j)); config.Port = port; if (CheckConfig(config)) { detected.Add(config); Interlocked.Add(ref tested, configs.Count() - j); break; } Interlocked.Increment(ref tested); // compute progress information and fire event cancel = OnProgress((double)tested / total, detected.Select(c => new SerialPortConfig(c))); if (cancel) { break; } } } }); if (cancel) { //System.Windows.MessageBox.Show("Detection was canceled"); } else { OnProgress(1, detected.Select(c => new SerialPortConfig(c)).ToArray()); } return(detected.ToArray()); } }
public SerialPortWraper(SerialPortConfig cfg) : base(cfg.Port, cfg.BaudRate, cfg.Parity, cfg.DataBits, cfg.StopBits) { //ReadTimeout = (int)TimeSpan.FromSeconds(3).TotalMilliseconds; //WriteTimeout = (int)TimeSpan.FromSeconds(3).TotalMilliseconds; //NewLine = "\r\n"; //WriteBufferSize = OptimalBufferSize; //ReadBufferSize = OptimalBufferSize; //ReceivedBytesThreshold = 65535; // We don't need this event, so max out the threshold //Encoding = Encoding.Default; DtrEnable = false; RtsEnable = false; Handshake = Handshake.None; }
private bool CheckConfig(SerialPortConfig config) { return(true); }