Click here to Skip to main content
15,892,161 members
Articles / Internet of Things / Arduino

Arduino, C#, and Serial Interface

Rate me:
Please Sign up or sign in to vote.
4.97/5 (64 votes)
10 Mar 2016CPOL11 min read 310.1K   22.7K   196  
Serial communication with an Arduino Board via C# and the SerialPort class from the System.IO.Ports namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChartPainter3D;

namespace Arduino_SerialInterface
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Contains a collection of <see cref="WeatherDataItem"/>
        /// and control methods to send commands to the Arduino Board
        /// </summary>
        WeatherDataContainer weatherData = new WeatherDataContainer();
        /// <summary>
        /// Constructor
        /// </summary>
        public MainWindow()
        {
            weatherData.NewWeatherDataReceived += weatherData_NewWeatherDataReceived;
            InitializeComponent();
            try
            {
                weatherData.OpenArduinoConnection();
            }
            catch (Exception)
            {
                MessageBox.Show("Error: Can not connect to the Arduino Board - Configure the COM Port in the app.config file and check whether an Arduino Board is connected to your computer.");
            }
        }
        /// <summary>
        /// OnWeatherDataReceived event is catched in
        /// order to update the weather data display on the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void weatherData_NewWeatherDataReceived(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(new ThreadStart(DrawChart));
            Dispatcher.BeginInvoke(new ThreadStart(() =>
                weatherDataGrid.ItemsSource = weatherData.WeatherDataItems));

        }
        /// <summary>
        /// Click-Event for the Button to get weather data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getWeatherData_Click(object sender, RoutedEventArgs e)
        {
            weatherData.GetWeatherDataFromArduinoBoard();
        }

        private void DrawChart()
        {
            List<double> temparatures = new List<double>();
            List<string> captions = new List<string>();
            foreach (WeatherDataItem item in weatherData.WeatherDataItems)
            {
                temparatures.Add(item.TemparatureCelsius);
                captions.Add(string.Format("{0}\n{1} C", item.Date.ToShortDateString(), item.TemparatureCelsius.ToString()));
            }
            ChartDataContainer container = new ChartDataContainer();
            container.Data = temparatures;
            container.DataCaptions = captions;
            container.MaxSize = new Point(800, 300);
            container.XAxisText = "Date / Temparature (Celsius)";
            container.YAxisText = "";
            container.Offset = new Point(20, 20);
            container.ChartElementColor = Colors.BurlyWood;
            CylinderChartPainter.DrawChart(container, weatherChartCanvas);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions