Esempio n. 1
0
        /// <summary>
        /// Constructor taking a SerialCommunicator object so as to allow COM settings to be
        /// implemented.
        /// </summary>
        /// <param name="communicator"></param>
        public SerialDataHandler(SerialCommunicator communicator, Rule rule, Value value)
        {
            //Create temp variable to read the required values.
            var comm = (SerialCommunicator) communicator;

            //Initialise the COM Port settings.
            _sPort = new SerialPort(comm.ComPort)
            {
                BaudRate = comm.BaudRate,
                Parity = Parity.None,
                StopBits = StopBits.One,
                DataBits = comm.DataBits,
                Handshake = Handshake.None,
                DtrEnable=comm.IsDTR,
                RtsEnable = comm.IsRTS
            };

            //Make the Value available.
            _value = value;

            //Output the data required - any instance of [val] is replaced with the actual value
            StringBuilder builder = new StringBuilder(rule.Action.OutputValue);
            builder.Replace("[val]", _value.StringValue);

            //Output the string
            OutputValue(builder.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Constructor taking a SerialCommunicator object so as to allow COM settings to be
        /// implemented. Listening starts immediately.
        /// </summary>
        /// <param name="communicator"></param>
        public SerialDataHandler(SerialCommunicator communicator)
        {
            Communicator = communicator;

            _sPort = new SerialPort(Communicator.ComPort);

            _sPort.BaudRate = 9600;
            _sPort.Parity = Parity.None;
            _sPort.StopBits = StopBits.One;
            _sPort.DataBits = 8;
            _sPort.Handshake = Handshake.None;

            _sPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            try
            {
                if (_portOutOfService)
                {
                    _sPort.DataReceived -= DataReceivedHandler;
                    throw new IOException("Port stopped responding. Closing connection on " + Communicator.ComPort);
                }

                ThreadPool.QueueUserWorkItem(new WaitCallback(CheckConnectionState));
            }
            catch(System.IO.IOException ioe)
            {
                DebugOutput.Print(_sPort.PortName + " has an IO error! ", ioe.Message);
            }
            catch(UnauthorizedAccessException uae)
            {
                DebugOutput.Print(_sPort.PortName + " has an Access error! ", uae.Message);
            }
            catch(Exception e)
            {
                DebugOutput.Print(_sPort.PortName + " has a general error! ", e.Message);
            }
        }