public LedDevice(string portName, LedDevice original) { if (portName != null) { _port = new SerialPort(portName, 115200) { DataBits = 8, StopBits = StopBits.One, Parity = Parity.None, Encoding = Encoding.ASCII, ReadTimeout = 3000, WriteTimeout = 3000 }; _port.Open(); } try { Write(new byte[256 * 3]); _port?.Write("LEDS"); WriteByte(0); ReadResponse(); Ticks.Add(); } catch { _port?.Close(); throw; } if (original != null) { _colors = original._colors.ToArray(); _fadeStartTime = original._fadeStartTime; _fadeDuration = original._fadeDuration; _fadeEndValue = original._fadeEndValue; _fadeStartValue = original._fadeStartValue; } _thread = new Thread(LedDeviceThreadProc) { IsBackground = true }; _thread.Start(); }
public static LedDevice Detect(LedDevice original, Action <LogLevel, string> onLog) { if (Settings.Current.LedDevice.Port != null) { return(new LedDevice(Settings.Current.LedDevice.Port, original)); } var result = SerialPort.GetPortNames().Select(portName => { try { return(new LedDevice(portName, original)); } catch (UnauthorizedAccessException) { onLog?.Invoke(LogLevel.Info, $"COM{portName} seems to be used by another process"); return(null); } catch (LedDeviceException) { onLog?.Invoke(LogLevel.Info, $"COM{portName} is available but device doesn't seem to be connected"); return(null); } catch (Exception) { return(null); } }).FirstOrDefault(_ => _ != null); if (result != null) { onLog?.Invoke(LogLevel.Info, $"LED device detected at {result.PortName}"); } else { onLog?.Invoke(LogLevel.Warn, "LED device was not detected"); } return(result); }