Click here to Skip to main content
11,481,404 members (62,060 online)
Click here to Skip to main content

Arduino, C#, and Serial COM Ports

, 10 Oct 2012 GPL3 106.8K 9.1K 132
Rate this:
Please Sign up or sign in to vote.
How to send a command to an Arduino board using C# and a COM port.
This is an old version of the currently published article.

Introduction

The Arduino boards provide an easy-to-use environment to control electrical components such as LEDs and motors. I have done this project with an Arduino Mega 2560 microcontroller board, but you can use nearly any official Arduino board.

What do you need for the tutorial:

  • An Arduino board, connected to your computer by USB
  • The official Arduino IDE (download from here)
  • The Arduino board driver must be installed as described here

Background

For a project I had to learn how to deal with COM ports and C/C++. However, to bring some fun into the whole story, I decided to buy an Arduino Mega 2560 board. It has an USB hardware plug to upload the software. The USB plug can be used to send and receive commands on the board by using a COM Port.

About the board

The Arduino Mega 2560 board has 54 I/O pins. If you are interested in more details, you can find them at the Arduino Product Page.

Using the code

The Arduino source code

Please note: I wrote the source code with the official Arduino IDE (download from here). When you use the official Arduino IDE, every Arduino program must have at least two methods: setup() and loop().

Code in the setup() method is processed right after connecting the board to power, or updating the software on the board. After the code in the setup() method is processed, the board starts processing the code in the loop method. Code in the loop method processed continously forever (yes, something like a while(true)-loop) until you plug out the power.

First, we are going to define our constants:

/*   CONSTANTS                                                   */
#define LED_TURN_ON_TIMEOUT  1000 //defines how long the LED stays powered on
#define LED_PIN 13                //Pin number on which the LED is connected
#define SERIAL_BAUDRATE 9600      //Baud-Rate of the serial Port

Afterwards, we have to write our setup()-method:

/*****************************************************************
The setup method is executed once after the bootloader is done
with his job.
******************************************************************/
void setup(){
  //setup the LED pin for output
  pinMode(LED_PIN, OUTPUT);
  //setup serial pin
  Serial.begin(SERIAL_BAUDRATE);
}
 
pinMode(LED_PIN,OUTPUT);

This method defines that the pin number 13 (constant LED_PIN has the value 13) on the Arduino board is used for output ( though in our case it is an LED, but it could be for example a relais too).

Serial.begin(SERIAL_BAUDRATE)

This method initalizes the serial USB port on the board to listen for incoming information.

So far so good. But now, we need a method which is listening for the information from the USB serial device.

Because I don't want to get this whole thing more complicated as it already is, I write the whole code into the loop()-method:

/*****************************************************************
The loop method is executed forever right after the setup method
is finished.
******************************************************************/
void loop(){
  int incomingByte = 0;
  
  if (Serial.available() > 0) {//Only when serial port is avaible
     // read the incoming byte:
     incomingByte = Serial.read(); //read incoming serial data
     
     //Light up LED for specified amount of milliseconds
     digitalWrite(LED_PIN, HIGH); 
     delay(LED_TURN_ON_TIMEOUT);
     digitalWrite(LED_PIN, LOW);
  }
}
incomingByte = Serial.read(); //read incoming serial data

incomingByte = Serial.read(); //read incoming serial data

This command reads the serial input. Maybe I am going to add an explanation about how to process different commands.

At this point, the execution of the sketch code is stopped until the board receives any information from the serial interface.

After the board has received information, we can continue with turning the LED on and off:

//Light up LED for specified amount of milliseconds     

digitalWrite(LED_PIN, HIGH); 
delay(LED_TURN_ON_TIMEOUT);
digitalWrite(LED_PIN, LOW);

Afterwards, the board starts the execution of the loop()-function again.

The C#/WPF source code

The C# code is a simple thing, you can send commands to the board by using the SerialPort class which is avaible in the namespace System.IO.Ports of the .NET Framework. More information about the port object is avaible on the corresponding MSDN Page.

Points of Interest

Arduino provides a huge amount of possibilities on working with hardware and connection to a computer. Last but not least, at the official Arduino homepage is a large community support avaible.

History

  • 10-October-2012: Created the article.
  • 10-October-2012: Updated (thanks to Mike Hankey and OriginalGriff).

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Marco Bertschi
Software Developer (Junior)
Switzerland Switzerland
Software Developer (Swiss Federal VET Diploma), experienced with Qt C++, C#, RFC 5424 and Arduino Boards.
Music enthusiast, runner, part-time psychologist for friends, awesome guy and Air Force Sergeant First Class.
Follow on   Twitter

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionFirst connection Pin
Member 112468614-Dec-14 6:09
memberMember 112468614-Dec-14 6:09 
GeneralMy vote of 5 Pin
Member 1102292519-Aug-14 13:00
memberMember 1102292519-Aug-14 13:00 
GeneralRe: My vote of 5 Pin
Marco Bertschi24-Aug-14 7:52
protectorMarco Bertschi24-Aug-14 7:52 
Question3DChart missing? Pin
Reactory2-Jun-14 14:39
professionalReactory2-Jun-14 14:39 
AnswerRe: 3DChart missing? Pin
Marco Bertschi3-Jun-14 2:03
protectorMarco Bertschi3-Jun-14 2:03 
GeneralRe: 3DChart missing? Pin
Reactory3-Jun-14 7:33
professionalReactory3-Jun-14 7:33 
GeneralRe: 3DChart missing? Pin
Marco Bertschi3-Jun-14 8:37
protectorMarco Bertschi3-Jun-14 8:37 
QuestionEOT... standard? Pin
Member 1084688827-May-14 8:50
memberMember 1084688827-May-14 8:50 
AnswerRe: EOT... standard? Pin
Marco Bertschi27-May-14 9:02
protectorMarco Bertschi27-May-14 9:02 
QuestionMessage Automatically Removed Pin
23-Apr-14 10:06
memberISpliter23-Apr-14 10:06 
GeneralMy vote of 5 Pin
Volynsky Alex21-Apr-14 22:49
professionalVolynsky Alex21-Apr-14 22:49 
GeneralTEMPARATURE TEMPARATURE TEMPARATURE Pin
André Pereira25-Jan-14 3:38
memberAndré Pereira25-Jan-14 3:38 
GeneralRe: TEMPARATURE TEMPARATURE TEMPARATURE Pin
Marco Bertschi25-Jan-14 4:28
professionalMarco Bertschi25-Jan-14 4:28 
Newsexactly what i was looking for... Pin
~dgn1274-Dec-13 19:11
professional~dgn1274-Dec-13 19:11 
GeneralRe: exactly what i was looking for... Pin
Marco Bertschi4-Dec-13 19:47
professionalMarco Bertschi4-Dec-13 19:47 
QuestionArduino hardware Pin
Plamen Kovandjiev28-Nov-13 2:43
memberPlamen Kovandjiev28-Nov-13 2:43 
AnswerRe: Arduino hardware Pin
Marco Bertschi28-Nov-13 4:32
professionalMarco Bertschi28-Nov-13 4:32 
QuestionYou mention my Article!! Pin
glennPattonWork26-Nov-13 23:58
professionalglennPattonWork26-Nov-13 23:58 
AnswerRe: You mention my Article!! Pin
Marco Bertschi27-Nov-13 2:58
professionalMarco Bertschi27-Nov-13 2:58 
GeneralMy vote of 5 Pin
Abinash Bishoyi9-Apr-13 7:01
memberAbinash Bishoyi9-Apr-13 7:01 
QuestionCommands? Pin
kovacsgellert6-Jan-13 5:14
memberkovacsgellert6-Jan-13 5:14 
AnswerRe: Commands? [modified] Pin
glennPattonWork27-Nov-13 4:00
professionalglennPattonWork27-Nov-13 4:00 
QuestionRe:Arduino, C#, and serial interface Pin
iykedapotential19-Nov-12 2:55
memberiykedapotential19-Nov-12 2:55 
AnswerRe:Arduino, C#, and serial interface Pin
glennPattonWork27-Nov-13 0:03
professionalglennPattonWork27-Nov-13 0:03 
QuestionLicensing?? Pin
Steve Maier6-Nov-12 9:34
memberSteve Maier6-Nov-12 9:34 
AnswerRe: Licensing?? Pin
Marco Bertschi23-Nov-13 11:03
professionalMarco Bertschi23-Nov-13 11:03 
QuestionExcellent job Pin
Mike Hankey14-Oct-12 12:06
memberMike Hankey14-Oct-12 12:06 
GeneralMy vote of 5 Pin
OriginalGriff13-Oct-12 23:13
mvpOriginalGriff13-Oct-12 23:13 
GeneralMy vote of 5 Pin
DaveAuld13-Oct-12 9:20
mentorDaveAuld13-Oct-12 9:20 
GeneralMy vote of 5 Pin
DrABELL10-Oct-12 11:19
memberDrABELL10-Oct-12 11:19 
GeneralRe: My vote of 5 Pin
Marco Bertschi10-Oct-12 23:25
memberMarco Bertschi10-Oct-12 23:25 
GeneralRe: My vote of 5 Pin
DrABELL11-Oct-12 2:27
memberDrABELL11-Oct-12 2:27 
GeneralRe: My vote of 5 Pin
Marco Bertschi12-Oct-12 5:33
memberMarco Bertschi12-Oct-12 5:33 
GeneralRe: My vote of 5 Pin
DrABELL12-Oct-12 11:31
memberDrABELL12-Oct-12 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 10 Oct 2012
Article Copyright 2014 by Marco Bertschi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid