public void SetNewPortMode(SerialPortModes SerialPortMode)
        {
            if (IsOpen)
            {
                throw new Exception("Serial port is open!");
            }

            if (SerialPortMode == SerialPortModes.RSMode)
            {
                serialPortMode = SerialPortModes.RSMode;
                if (tcpMaster != null)
                {
                    tcpMaster = null;
                }
            }
            else
            {
                serialPortMode = SerialPortModes.TCPMode;
            }
        }
        public void LoadSettingsFromFile(string FileName)
        {
            XDocument document;

            try
            {
                document = XDocument.Load(FileName);
            }
            catch
            {
                throw new Exception("Error open xml file!");
            }

            try
            {
                XElement element = document.Root.Element("ComSettings");
                PortName = Convert.ToString(element.Attribute("PortName").Value);

                PortNum   = Convert.ToUInt16(element.Attribute("IPPortNum").Value.ToString());
                IpAddress = element.Attribute("IPAddr").Value.ToString();

                if (element.Attribute("PortMode").Value.ToString().ToUpper() == "RSMODE")
                {
                    serialPortMode = SerialPortModes.RSMode;
                }
                else
                {
                    serialPortMode = SerialPortModes.TCPMode;
                }

                BaudRate  = Convert.ToInt32(element.Attribute("BaudRate").Value.ToString());
                SlaveAddr = Convert.ToByte(element.Attribute("SlaveAddr").Value);

                string strPar, strStops;

                strPar   = Convert.ToString(element.Attribute("Parity").Value);
                strStops = Convert.ToString(element.Attribute("StopBits").Value);

                if (strPar == "None")
                {
                    Parity = System.IO.Ports.Parity.None;
                }
                else if (strPar == "Even")
                {
                    Parity = System.IO.Ports.Parity.Even;
                }
                else
                {
                    Parity = System.IO.Ports.Parity.Odd;
                }

                if (strStops == "Two")
                {
                    StopBits = System.IO.Ports.StopBits.Two;
                }
                else
                {
                    StopBits = System.IO.Ports.StopBits.One;
                }
            }

            catch
            {
                throw new Exception("Error load settings from file!");
            }
        }
        public TechnicianModeSettings(byte terminalId = 0,
                                      SerialPortModes serialPort0Mode = SerialPortModes.Host,
                                      SerialPortModes serialPort1Mode = SerialPortModes.FingerprintUnit,
                                      SerialPortModes serialPort2Mode = SerialPortModes.Printer,
                                      int serialPort0BaudRate = 19200,
                                      int serialPort1BaudRate = 57600,
                                      int serialPort2BaudRate = 19200,
                                      bool hasModem = false,
                                      byte modemRingsToAnswer = 0,
                                      NetworkCardTypes networkCardType = NetworkCardTypes.F_Ethernet100Mbps)
        {
            if (terminalId > 31)
                throw new ArgumentException("Invalid Terminal ID.  Terminal ID should be 0 to 31.", "terminalId");
            _terminalId = terminalId;

            if (serialPort0Mode  != SerialPortModes.Host)
                throw new ArgumentException("Invalid mode for serial port 0.", "serialPort0Mode");
            _serialPort0Mode = serialPort0Mode;

            if (serialPort1Mode != SerialPortModes.FingerprintUnit && serialPort1Mode != SerialPortModes.CardReader)
                throw new ArgumentException("Invalid mode for serial port 1.", "serialPort1Mode");
            _serialPort1Mode = serialPort1Mode;

            if (serialPort2Mode != SerialPortModes.Printer && serialPort2Mode != SerialPortModes.CardReader)
                throw new ArgumentException("Invalid mode for serial port 2.", "serialPort2Mode");
            _serialPort2Mode = serialPort2Mode;

            if (!BaudRates.IsValid(serialPort0BaudRate))
                throw new ArgumentException("Invalid baud rate for serial port 0.", "serialPort0BaudRate");
            _serialPort0BaudRate = serialPort0BaudRate;

            if (!BaudRates.IsValid(serialPort1BaudRate))
                throw new ArgumentException("Invalid baud rate for serial port 1.", "serialPort1BaudRate");
            _serialPort1BaudRate = serialPort1BaudRate;

            if (!BaudRates.IsValid(serialPort2BaudRate))
                throw new ArgumentException("Invalid baud rate for serial port 2.", "serialPort2BaudRate");
            _serialPort2BaudRate = serialPort2BaudRate;

            _hasModem = hasModem;
            if (hasModem)
            {
                if (modemRingsToAnswer > 9)
                    throw new ArgumentException("Modem rings to answer must be 0 through 9", "modemRingsToAnswer");
                _modemRingsToAnswer = modemRingsToAnswer;
            }

            if (!Enum.IsDefined(typeof (NetworkCardTypes), networkCardType))
                throw new ArgumentException("Invalid network card type.", "networkCardType");
            _networkCardType = networkCardType;
        }
        private TechnicianModeSettings(string data)
        {
            var subcode = data.Substring(0, 3);
            if (subcode != "HPG")
                throw new InvalidDataException(
                    string.Format("Expected sub-code of \"{0}\" but got \"{1}\".", "HPG", subcode));

            if (!byte.TryParse(data.Substring(3, 2), NumberStyles.None, CultureInfo.InvariantCulture, out _terminalId))
                throw new InvalidDataException("Couldn't parse terminal id from technician mode settings data.");

            byte b;
            if (!byte.TryParse(data.Substring(5, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
                throw new InvalidDataException("Couldn't parse serial port 0 mode from technician mode settings data.");
            _serialPort0Mode = (SerialPortModes)b;

            if (!byte.TryParse(data.Substring(6, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
                throw new InvalidDataException("Couldn't parse serial port 1 mode from technician mode settings data.");
            _serialPort1Mode = (SerialPortModes)b;

            if (!byte.TryParse(data.Substring(7, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
                throw new InvalidDataException("Couldn't parse serial port 2 mode from technician mode settings data.");
            _serialPort2Mode = (SerialPortModes)b;

            if (!byte.TryParse(data.Substring(8, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
                throw new InvalidDataException("Couldn't parse serial port 0 baud rate from technician mode settings data.");
            _serialPort0BaudRate = BaudRates.Get(b);

            if (!byte.TryParse(data.Substring(9, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
                throw new InvalidDataException("Couldn't parse serial port 1 baud rate from technician mode settings data.");
            _serialPort1BaudRate = BaudRates.Get(b);

            if (!byte.TryParse(data.Substring(10, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
                throw new InvalidDataException("Couldn't parse serial port 2 baud rate from technician mode settings data.");
            _serialPort2BaudRate = BaudRates.Get(b);

            _hasModem = data[11] != 'N';
            if (_hasModem)
            {
                if (!byte.TryParse(data.Substring(11, 1), NumberStyles.None, CultureInfo.InvariantCulture, out _modemRingsToAnswer))
                    throw new InvalidDataException("Couldn't parse modem rings to answer from technician mode settings data.");
            }

            _networkCardType = (NetworkCardTypes)data[12];
        }
        public TechnicianModeSettings(byte terminalId = 0,
                                      SerialPortModes serialPort0Mode = SerialPortModes.Host,
                                      SerialPortModes serialPort1Mode = SerialPortModes.FingerprintUnit,
                                      SerialPortModes serialPort2Mode = SerialPortModes.Printer,
                                      int serialPort0BaudRate         = 19200,
                                      int serialPort1BaudRate         = 57600,
                                      int serialPort2BaudRate         = 19200,
                                      bool hasModem                    = false,
                                      byte modemRingsToAnswer          = 0,
                                      NetworkCardTypes networkCardType = NetworkCardTypes.F_Ethernet100Mbps)
        {
            if (terminalId > 31)
            {
                throw new ArgumentException("Invalid Terminal ID.  Terminal ID should be 0 to 31.", "terminalId");
            }
            _terminalId = terminalId;

            if (serialPort0Mode != SerialPortModes.Host)
            {
                throw new ArgumentException("Invalid mode for serial port 0.", "serialPort0Mode");
            }
            _serialPort0Mode = serialPort0Mode;

            if (serialPort1Mode != SerialPortModes.FingerprintUnit && serialPort1Mode != SerialPortModes.CardReader)
            {
                throw new ArgumentException("Invalid mode for serial port 1.", "serialPort1Mode");
            }
            _serialPort1Mode = serialPort1Mode;

            if (serialPort2Mode != SerialPortModes.Printer && serialPort2Mode != SerialPortModes.CardReader)
            {
                throw new ArgumentException("Invalid mode for serial port 2.", "serialPort2Mode");
            }
            _serialPort2Mode = serialPort2Mode;

            if (!BaudRates.IsValid(serialPort0BaudRate))
            {
                throw new ArgumentException("Invalid baud rate for serial port 0.", "serialPort0BaudRate");
            }
            _serialPort0BaudRate = serialPort0BaudRate;

            if (!BaudRates.IsValid(serialPort1BaudRate))
            {
                throw new ArgumentException("Invalid baud rate for serial port 1.", "serialPort1BaudRate");
            }
            _serialPort1BaudRate = serialPort1BaudRate;

            if (!BaudRates.IsValid(serialPort2BaudRate))
            {
                throw new ArgumentException("Invalid baud rate for serial port 2.", "serialPort2BaudRate");
            }
            _serialPort2BaudRate = serialPort2BaudRate;

            _hasModem = hasModem;
            if (hasModem)
            {
                if (modemRingsToAnswer > 9)
                {
                    throw new ArgumentException("Modem rings to answer must be 0 through 9", "modemRingsToAnswer");
                }
                _modemRingsToAnswer = modemRingsToAnswer;
            }

            if (!Enum.IsDefined(typeof(NetworkCardTypes), networkCardType))
            {
                throw new ArgumentException("Invalid network card type.", "networkCardType");
            }
            _networkCardType = networkCardType;
        }
        private TechnicianModeSettings(string data)
        {
            var subcode = data.Substring(0, 3);

            if (subcode != "HPG")
            {
                throw new InvalidDataException(
                          string.Format("Expected sub-code of \"{0}\" but got \"{1}\".", "HPG", subcode));
            }

            if (!byte.TryParse(data.Substring(3, 2), NumberStyles.None, CultureInfo.InvariantCulture, out _terminalId))
            {
                throw new InvalidDataException("Couldn't parse terminal id from technician mode settings data.");
            }

            byte b;

            if (!byte.TryParse(data.Substring(5, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
            {
                throw new InvalidDataException("Couldn't parse serial port 0 mode from technician mode settings data.");
            }
            _serialPort0Mode = (SerialPortModes)b;

            if (!byte.TryParse(data.Substring(6, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
            {
                throw new InvalidDataException("Couldn't parse serial port 1 mode from technician mode settings data.");
            }
            _serialPort1Mode = (SerialPortModes)b;

            if (!byte.TryParse(data.Substring(7, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
            {
                throw new InvalidDataException("Couldn't parse serial port 2 mode from technician mode settings data.");
            }
            _serialPort2Mode = (SerialPortModes)b;

            if (!byte.TryParse(data.Substring(8, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
            {
                throw new InvalidDataException("Couldn't parse serial port 0 baud rate from technician mode settings data.");
            }
            _serialPort0BaudRate = BaudRates.Get(b);

            if (!byte.TryParse(data.Substring(9, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
            {
                throw new InvalidDataException("Couldn't parse serial port 1 baud rate from technician mode settings data.");
            }
            _serialPort1BaudRate = BaudRates.Get(b);

            if (!byte.TryParse(data.Substring(10, 1), NumberStyles.None, CultureInfo.InvariantCulture, out b))
            {
                throw new InvalidDataException("Couldn't parse serial port 2 baud rate from technician mode settings data.");
            }
            _serialPort2BaudRate = BaudRates.Get(b);

            _hasModem = data[11] != 'N';
            if (_hasModem)
            {
                if (!byte.TryParse(data.Substring(11, 1), NumberStyles.None, CultureInfo.InvariantCulture, out _modemRingsToAnswer))
                {
                    throw new InvalidDataException("Couldn't parse modem rings to answer from technician mode settings data.");
                }
            }

            _networkCardType = (NetworkCardTypes)data[12];
        }