Tutorial: C# Communicating with Arduino over Serial

11

Category : Arduino, Electronics, Windows

In this tutorial I’ll try to show you how to easily tell an arduino what to do from a C# program using serial communication. The only thing the C# program does is send a byte to the arduino, and the arduino program will then set pin 13 to HIGH or LOW.

Lets start with the C# sourcecode, it’s written in C# Express 2010 which you can download for free here. Get the C# project files:

Download CSharpEasySerialSource.zip

I tried to comment the code so that it should explain most of the stuff going on. But it’s pretty basic, you select the current comport then hit Connect. If you selected the correct port that the arduino is using it should now be connected. The textbox only takes numbers, writing text there will only result in it being converted to bytes.

When you click Send it will try to send the contents of the textbox to the arduino. In my example code I used “0” and “1” for turning off an on the LED. Lets look at the Ardunio example:

What the program does it’s listening on the serial port. And when it receives a “1” pin 13 goes HIGH, and if it receives “0” it goes LOW. Pin 13 is in most cases a LED thats on the arduino board, so you only need a bare arduino to test it. And if you don’t know any C# and just want to use the program to control a arduino I’ve created a zip file containing only the .exe file for the program:

Download CSharpEasySerial.zip