public SerialPortCommunicator(ISerialPortDataReceiver dataPointReceiver)
        {
            LocalSerialPort = new SerialPort();
            this.LocalSerialPort.BaudRate = 9600;
            this.LocalSerialPort.PortName = "COM5";
            this.LocalSerialPort.Encoding = Encoding.ASCII;

            this.LocalSerialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(dataPointReceiver.DataRecieved);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            arduinoDataReceiver = new ArduinoDataReceiver(ConfigurationManager.AppSettings.Get("connStringFile"));

            serialPortCommunicator = new SerialPortCommunicator(arduinoDataReceiver);

            try
            {
                if (!serialPortCommunicator.LocalSerialPort.IsOpen)
                {
                    serialPortCommunicator.LocalSerialPort.Open();
                }
            }
            catch (Exception ex)
            {
                //todo add more exception handling
                throw ex;
            }


            while (true)
            {
                //Thread.Sleep(2050);

                if (lastTimeGathered == null || DateTime.Now > lastTimeGathered.AddSeconds(30))
                {
                    lastTimeGathered = DateTime.Now;
                    showLight        = 1;
                    serialPortCommunicator.LocalSerialPort.WriteLine(showLight.ToString());
                }
                else if (DateTime.Now > lastTimeGathered.AddSeconds(2) && showLight == 1)
                {
                    showLight = 0;
                    serialPortCommunicator.LocalSerialPort.WriteLine(showLight.ToString());
                }
                else if (DateTime.Now > lastTimeGathered.AddSeconds(2) && DateTime.Now < lastTimeGathered.AddSeconds(3) && showLight == 0)
                {
                    showLight = 0;
                    serialPortCommunicator.LocalSerialPort.WriteLine(showLight.ToString());
                }
            }
        }