Beispiel #1
0
        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);
        }
Beispiel #2
0
        public virtual bool TryGet(string portName, out SerialApi result)
        {
            if (!IsValidPortName(portName))
            {
                result = null;
                return(false);
            }

            return(SerialPorts.TryGetValue(portName, out result));
        }