Ejemplo n.º 1
0
        /// <summary>
        /// Scan configuration to scan all available serial ports (ScanPort == null)
        /// </summary>
        public ScanConfigurationUART()
        {
            Port = null;

            // Default: 9600 Baud, 8N1, no handshake
            ComPortSettings = new ComPortSettings();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Applies settings from string, i.e. "9600,8,N,1" or "9600 8N1" or "9600_8N1".
        /// </summary>
        /// <param name="settings">The settings to apply, i.e. "9600,8,N,1" or "9600 8N1" or "9600_8N1".</param>
        public static ComPortSettings FromString(string settings)
        {
            // "9600,8,N,1" --> "9600,8,N,1" (no change) "9600 8N1" --> "9600,8N1" "9600_8N1" --> "9600,8N1"
            string work = settings.Replace(' ', ',').Replace('_', ',');

            if (string.IsNullOrEmpty(work) || !work.Contains(","))
            {
                return(null);
            }

            // "9600,8,N,1" --> "9600" "9600,8N1" --> "9600"
            string baudRate = work.Split(',')[0];

            // "9600,8,N,1" --> "8N1" "9600 8N1" --> "8N1"
            work = work.Substring(baudRate.Length).Replace(",", "");
            if (3 != work.Length)
            {
                return(null);
            }

            try
            {
                ComPortSettings cps = new ComPortSettings();
                cps.Baudrate = Convert.ToInt32(baudRate);
                switch (work[0])
                {
                case '8': cps.Databits = 8; break;

                case '7': cps.Databits = 7; break;

                default: return(null);
                }
                switch (work[1])
                {
                case 'N': cps.Parity = System.IO.Ports.Parity.None; break;

                case 'E': cps.Parity = System.IO.Ports.Parity.Even; break;

                case 'O': cps.Parity = System.IO.Ports.Parity.Odd; break;

                //case 'M': cps.Parity = Parity.Mark; break;
                //case 'S': cps.Parity = Parity.Space; break;
                default: return(null);
                }
                switch (work[2])
                {
                case 'N': cps.Stopbits = System.IO.Ports.StopBits.None; break;

                case '1': cps.Stopbits = System.IO.Ports.StopBits.One; break;

                //case '5': cps.Stopbits = StopBits.OnePointFive; break;
                case '2': cps.Stopbits = System.IO.Ports.StopBits.Two; break;

                default: return(null);
                }
                return(cps);
            }
            catch { }
            return(null);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Copy constructor.
 /// </summary>
 public ScanConfigurationUART(ScanConfigurationUART other)
 {
     if (other != null)
     {
         Port            = other.Port;
         ComPortSettings = new ComPortSettings(other.ComPortSettings);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Copies from other <see cref="ComPortSettings"/> object.
 /// </summary>
 /// <param name="other">The other (source) <see cref="ComPortSettings"/> object.</param>
 public void CopyFrom(ComPortSettings other)
 {
     this.Baudrate      = other.Baudrate;
     this.Databits      = other.Databits;
     this.Stopbits      = other.Stopbits;
     this.Parity        = other.Parity;
     this.Handshake     = other.Handshake;
     this.TxTimeoutMSec = other.TxTimeoutMSec;
     this.RxTimeoutMSec = other.RxTimeoutMSec;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current
 /// <see cref="T:System.Object"/>.
 /// </summary>
 /// <param name="obj">
 /// The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.
 /// </param>
 /// <exception cref="T:System.NullReferenceException">
 /// The <paramref name="obj"/> parameter is null.
 /// </exception>
 /// <returns>
 /// true if the specified <see cref="T:System.Object"/> is equal to the current <see
 /// cref="T:System.Object"/>; otherwise, false.
 /// </returns>
 public override bool Equals(Object obj)
 {
     if (obj is ComPortSettings)
     {
         ComPortSettings other = (ComPortSettings)obj;
         return(this.Baudrate.Equals(other.Baudrate) &&
                this.Databits.Equals(other.Databits) &&
                this.Stopbits.Equals(other.Stopbits) &&
                this.Parity.Equals(other.Parity) &&
                this.Handshake.Equals(other.Handshake) &&
                this.TxTimeoutMSec.Equals(other.TxTimeoutMSec) &&
                this.RxTimeoutMSec.Equals(other.RxTimeoutMSec));
     }
     return(base.Equals(obj));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Scan configuration to scan given serial port.
 /// </summary>
 /// <param name="port">The serial port</param>
 /// <param name="settings">Serial port settings</param>
 public ScanConfigurationUART(string port, ComPortSettings settings)
 {
     Port            = port;
     ComPortSettings = settings;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComPortSettings"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public ComPortSettings(ComPortSettings other)
 {
     CopyFrom(other);
 }