Esempio n. 1
0
 /// <summary>
 /// Opens the connection to an Arduino board
 /// </summary>
 public void OpenArduinoConnection()
 {
     WeatherDataItem item = new WeatherDataItem();
     item.Date = DateTime.Now;
     item.TemparatureCelsius = (float)5.5;
     weatherDataItems.Add(item);
     item = new WeatherDataItem();
     item.Date = DateTime.Now;
     item.TemparatureCelsius = (float)10;
     weatherDataItems.Add(item);
     item = new WeatherDataItem();
     item.Date = DateTime.Now;
     item.TemparatureCelsius = (float)11.3;
     weatherDataItems.Add(item);
     item = new WeatherDataItem();
     item.Date = DateTime.Now;
     item.TemparatureCelsius = (float)8.55;
     weatherDataItems.Add(item);
     if(!arduinoBoard.IsOpen)
     {
         arduinoBoard.DataReceived += arduinoBoard_DataReceived;
         arduinoBoard.PortName =  ConfigurationSettings.AppSettings["ArduinoPort"];
         arduinoBoard.Open();
     }
     else
     {
         throw new InvalidOperationException("The Serial Port is already open!");
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Reads weather data from the arduinoBoard serial port
 /// </summary>
 void arduinoBoard_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     string data = arduinoBoard.ReadTo("\x03");//Read until the EOT code
     //Split into 'date=temparature' formatted text
     string[] dataArray = data.Split(new string[] {"\x02", "$" }, StringSplitOptions.RemoveEmptyEntries);
     //Iterate through the splitted data and parse it into weather data items
     //and add them to the list of received weather data.
     foreach (string dataItem in dataArray.ToList())
     {
         WeatherDataItem weatherDataItem = new WeatherDataItem();
         weatherDataItem.FromString(dataItem);
         weatherDataItems.Add(weatherDataItem);
     }
     if(NewWeatherDataReceived != null)//If there is someone waiting for this event to be fired
     {
         NewWeatherDataReceived(this, new EventArgs()); //Fire the event, indicating that new WeatherData was added to the list.
     }
 }