public virtual bool TryCreate(string portName, out SerialApi result, Action <SerialPort> configure = null) { if (!IsValidPortName(portName) || SerialPorts.ContainsKey(portName)) { result = null; return(false); } var serialPort = new SerialPort(portName); configure?.Invoke(serialPort); var serialApi = new SerialApi(serialPort); var bResult = SerialPorts.TryAdd(portName, serialApi); if (bResult) { result = serialApi; } else { result = null; serialApi.Dispose(); } return(bResult); }
public virtual bool TryGet(string portName, out SerialApi result) { if (!IsValidPortName(portName)) { result = null; return(false); } return(SerialPorts.TryGetValue(portName, out result)); }