Exemple #1
0
        public bool Equals(SerialSettings obj)
        {
            if (obj == null)
            {
                return(false);
            }

            return(this.BaudRate == obj.BaudRate &&
                   this.Parity == obj.Parity &&
                   this.StopBits == obj.StopBits &&
                   this.DataBits == obj.DataBits);
        }
Exemple #2
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            SerialSettings other = obj as SerialSettings;

            if (other == null)
            {
                return(false);
            }

            return(Equals(other));
        }
Exemple #3
0
        public void ConnectPort(string portName, SerialSettings settings, Action <byte[], int> action = null)
        {
            // Do some parameter checking
            if (string.IsNullOrWhiteSpace(portName))
            {
                throw new ArgumentNullException(portName);
            }

            if (!AllPorts.Contains(portName))
            {
                throw new ArgumentException("The given port name doesn't exist", "portName");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            // We should be able to work with the parameters given now.

            // First check that if we have opened this port, the settings are compatible
            if (portSettings.ContainsKey(portName))
            {
                if (portSettings[portName] != settings)
                {
                    // Settings don't match
                    throw new InvalidOperationException(string.Format("The port {0} has already been opened with different settings.", portName));
                }
                else
                {
                    // It's open and the settings match
                    SerialPort port = ports[portName];

                    // Add the handler
                    portHandlers[port].Add(action);
                }
            }
            else
            {
                SerialPort port = new SerialPort(
                    portName,
                    settings.BaudRate,
                    settings.Parity,
                    settings.DataBits,
                    settings.StopBits);

                // TODO: This could very well throw an exception, but we want to only
                // deal with the ones we have to, so we will deal with them when they come up
                port.Open();

                if (port.IsOpen)
                {
                    // Success?
                    ports.Add(portName, port);
                    portSettings.Add(portName, settings);
                    List <Action <byte[], int> > handlers = new List <Action <byte[], int> >();
                    handlers.Add(action);
                    portHandlers.Add(port, handlers);

                    port.DataReceived += this.DataReceived;
                }
                else
                {
                    // Failure
                    port.Close();
                }
            }

            if (timer == null)
            {
                timer          = new System.Timers.Timer();
                timer.Interval = 1000;
                timer.Elapsed += RecheckPorts;
                timer.Start();
            }
        }