public SerialPortWatcher(SerialPortSettings portOptions)
 {
     // copy in the settings
     _serialPortSettings = portOptions;
     // setup blocking
     _stopThreadEvent = new ManualResetEvent(false);
     // setup the thread
     SetupThread();
 }
        public void CreateDefaultSettings()
        {
            WriteLog = false;
            SerialPort = new SerialPortSettings();
            Commands = new CommandDictionary();

            SerialPort defaultConfig = new SerialPort();

            defaultConfig.Encoding = Encoding.ASCII;

            var portNames = System.IO.Ports.SerialPort.GetPortNames();
            if (portNames.Count() == 0)
                throw new Exception("No ports found");

            SerialPort.PortName = portNames[0];
            SerialPort.BaudRate = (BaudRate)defaultConfig.BaudRate;
            SerialPort.Parity = defaultConfig.Parity;
            SerialPort.DataBits = defaultConfig.DataBits;
            SerialPort.StopBits = defaultConfig.StopBits;
            SerialPort.Handshake = defaultConfig.Handshake;
        }
        protected SerialPort SetupSerialPort(SerialPortSettings settings)
        {
            // Configure a standard serial port based on the options
            SerialPort serialPort = new SerialPort();

            serialPort.PortName = settings.PortName;
            serialPort.BaudRate = (int)settings.BaudRate;
            serialPort.Parity = settings.Parity;
            serialPort.DataBits = settings.DataBits;
            serialPort.StopBits = settings.StopBits;
            serialPort.Handshake = settings.Handshake;

            serialPort.Encoding = Encoding.ASCII;

            // ends a transmission
            serialPort.NewLine = "\r\n";

            serialPort.ReadTimeout = 500;
            serialPort.WriteTimeout = 500;

            return serialPort;
        }
        public void Load()
        {
            SerialPort = new SerialPortSettings();
            Commands = new CommandDictionary();
            if (SettingsFileExists)
            {
                XElement spcElement = XDocument.Load(_xmlFilePath).Element("SerialPortControl");
                XElement settingsElement = spcElement.Element("Settings");
                XElement serialPortElement = settingsElement.Element("SerialPort");
                var commandElements = spcElement.Element("Commands").Elements("Command");

                WriteLog = (bool)settingsElement.Element("WriteLog");
                Listening = (bool)settingsElement.Element("Listening");

                SerialPort.PortName = serialPortElement.Element("PortName").Value;
                SerialPort.BaudRate = (BaudRate)Convert.ToInt32(serialPortElement.Element("BaudRate").Value);
                SerialPort.Parity = serialPortElement.Element("Parity").ToEnumValue<Parity>();
                SerialPort.DataBits = Convert.ToInt32(serialPortElement.Element("DataBits").Value);
                SerialPort.StopBits = serialPortElement.Element("StopBits").ToEnumValue<StopBits>();
                SerialPort.Handshake = serialPortElement.Element("Handshake").ToEnumValue<Handshake>();

                foreach (var element in commandElements)
                {
                    Command command = new Command
                    {
                        IncomingCommand = element.Element("IncomingCommand").Value,
                        Target = element.Element("Target").Value,
                        Arguments = element.Element("Arguments").Value,
                        StartInDirectory = element.Element("StartInDirectory").Value
                    };
                    Commands.Add(command.IncomingCommand, command);
                }
            }
        }