Read serial from an Arduino

Here I will try to explain how I do when I want to read the serial output from one of my Arduino’s. This is in C#, and isn’t for someone who don’t know anything about it.

The first I do is to put a label on the form, just to have a place where the text can be shown, and for this example I am am sending a value to the C# so it can be shown as a progressbar, so putting one of those on there too.

Next step is to add a serial port to the form, by simply finding it in the menu and dragging it onto the form.

Then some code to handle it all.

The first thing I do is to put this into public Form1()

serialPort1.PortName = "COM4";
serialPort1.BaudRate = 9600;
serialPort1.DtrEnable = true;
serialPort1.Open();

You can set it in the editor in the bottom right corner too, but I like to do it like this.

We now got two options, either having a timer running to check if we got a message, or create an interrupt for the serial port. And here I like to use the interrupt because it is only triggered when it is in use, instead of the timer running over and over to see if there are anything there.

To create the interrupt, put this under the code we put in before.

serialPort1.DataReceived += serialPort1_DataReceived;

It now knows what to do when it receives a message, but can’t find the event to trigger, so we put that in too.

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    string line = serialPort1.ReadLine();
    this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
}

But we can’t use it like this because it isn’t the same thread, so to get it back to the same as the gui, we use this

private delegate void LineReceivedEvent(string line);
private void LineReceived(string line)
{
    //What to do with the received line here
    label1.Text = line;

    progressBar1.Value = int.Parse(line);
}

The finished result should be something like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Read_serial
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            serialPort1.PortName = "COM4";
            serialPort1.BaudRate = 9600;
            serialPort1.DtrEnable = true;
            serialPort1.Open();

            serialPort1.DataReceived += serialPort1_DataReceived;
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string line = serialPort1.ReadLine();
            this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
        }

        private delegate void LineReceivedEvent(string line);
        private void LineReceived(string line)
        {
            //What to do with the received line here
            label1.Text = line;

            progressBar1.Value = int.Parse(line);
        }
    }
}

We are now ready to test it out, but something on the arduino to return the value would be nice…

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(analogRead(0));
  delay(500);
}

This just prints the value of the analog pin 0 every 500 millisecond.

Notice! That the arduino restarts when you open the program, and can in some cases that can be read as some garbage by the C# program, and then might crash if it is told to parse it to an integer.

  • Download example

One comment

Leave a Reply