Instructables

Ever wanted to make your own application (*.exe) to work with your arduino (or other serial communicating device)?

This instructable requires:

  - Visual Studio 2008 or later* (I am using 2010 RC, some options may differ between versions)
                       OR
  - Visual C# Express Edition 2008 or later*


  - An Arduino (Any type) or other kind of serial communicating device
  - A basic knowledge of the C# Language
 

* An earlier edition of VS may work, but I am not sure if it has the SerialPort library.

 
Remove these adsRemove these ads by Signing Up

Step 1: Create a new application

Open Visual Studio and create a new Windows Forms Application. Then when form 1 comes up, add as many controls as you would like, starting with the SerialPort class.

If you are adding the Arduino support to your pre-made program, then just add the SerialPort class. If you are more advanced, you may want to make a plain code file with just the SerialPort library, so that you don't keep defining it.

Step 2: Configure your serial port

Picture of Configure your serial port
The only things that ever need to be changed are the
   -BaudRate (Change this to match the Arduino code (Serial.begin(this is your baud rate))
   -Port Name (When compiling and uploading you need to select a port, usually starts with COM)
   -Maybe Read buffer size, and write buffer size, only if you intend on reading/writing more data than 4096Bytes reading or 2048Bytes writing to/from the arduino. Usually this setting can stay the same.
mykiscool2 years ago
I am trying to read a speed value from the arduino, which is serial.println, I only get the initial value each time I open my program, example when I open my program if the speed sensor is 0.00 that's what it says, but when it changes to let's say a number like 20.5 it still says 0.00, but when I reopen the program, it will say 20.5. Also with it cuts off the first digit sometimes, for example I will have a number like 2.65 for my speed, but when I make i open the program I made, it shows only .65. I would really appreciate help. Code is below.


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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

try
{
if (arduinoCom.IsOpen == false)
{
arduinoCom.Open();
}
}

catch
{
MessageBox.Show("Serial Error: Is your serial plugged in?");
}
string speedReading = arduinoCom.ReadLine();
speedText.Text = (speedReading);

}
}
}
padão6 months ago

Hi mykiscool,

I think you must do something like this

while(true)

{

string speedReading = arduinoCom.ReadLine();
speedText.Text = (speedReading);

System.Threading.Thread.Sleep(1000); //waiting for 5 seconds

}

padão6 months ago
Nice work!! =)

Do you have a link for the source code?
Hi, do you how to interface arduino with a C# program using WiFi?
watatayaya2 years ago
Thanks so much for this tutorial! I'm a beginner in both Arduino and Microsoft Visual Studio C#. I would like to ask whether is it possible to read multiple data? For my project, i have 2-3 sensors on the Arduino board, and i need to read the data/information from all the 3 sensors in one Arduino program, and display it out separately on Microsoft Visual Studio.
chrisgward (author)  watatayaya2 years ago
Yes, it's very possible. Use Serial.write(value 1); Serial.write(','); and repeat that a few times on the arduino. Then in C#, use String[] array = Serial.ReadLine().split(','); and then use array[0] to get the first value, [1] to get the second and so on.
wow thank you so much!!!!! i'll give it a try! :D
chrisgward (author)  watatayaya2 years ago
You're welcome. Some time in the near future I will update this tutorial, covering more on how to read from the Arduino, as well as sending/receiving multiple sets of data. Until then, feel free to contact me with any further questions :D
thank you so much! i'm so glad to hear that! my project is due 3 weeks later and my group is still struggling alot and all of us are quite new to microsoft visual studio and arduino!

I do have one question. I'm now able to display the data from the sensor i'm using on the textbox. However, it only display integer value. i can't display double data type (example, values like 25.6, 34.8. It only display 25, 34). The codes below is the program i have typed.

if (!serialPort1.IsOpen)
{
try
{
serialPort1.Open();
double c = serialPort1.ReadByte();
AirspeedTextBox1.Text = c.ToString("N2");
serialPort1.Close();
}
catch
{
MessageBox.Show("There was an error. Please make sure that the correct port was selected, and the device, plugged in.");
}

Is there any ways, or other ways to display the decimal values out? Thank you so much again!
chrisgward (author)  watatayaya2 years ago
ReadByte reads a single 8 bit byte from the Arduino. This can become a problem when reading values over 255, I'll fire up Visual Studio and Arduino, have a poke around and get back to you soon.
Hello! i managed to solve the decimal part! what i did was this:

string[] c = serialPort1.ReadExisting().Split(',');
AirspeedTextBox.Text = c[0];
SonarTextBox.Text = c[1];

i used string to read the data and i was shocked when it could display the decimal place.

And thanks to your help, i manage to read and display multiple data! :D thanks thanks thanks! :D

however, i do have another problem that i encounter. I also do need to keep updating the data and display on the textbox. I used the following codes to display the data:


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 AirSpeed
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
try
{
serialPort1.Open();
}
catch
{
MessageBox.Show("There was an error. Please make sure that the correct port was selected, and the device, plugged in.");
}
}

private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
serialPort1.Close();
}

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}

private void DoUpdate(object s, EventArgs e)
{
string[] c = serialPort1.ReadExisting().Split(',');
AirspeedTextBox.Text = c[0];
SonarTextBox.Text = c[1];
}
}
}

when i run this program, it can display the data and refresh it smoothly, and after a while, the whole form hanged, and informing me that Index was outside the bounds of the array at "this.Invoke(new EventHandler(DoUpdate));" codes (seen in the attached image).

Any idea what caused the problem and how do i troubleshoot it? :) meanwhile i'll try to troubleshoot too! :)

thanks once again! :D
problem.jpg
chrisgward (author)  watatayaya2 years ago
IndexOutOfBounds is called when not enough data has been sent from the arduino to split it correctly. What you can do, is use ReadLine() instead of ReadExisting(), and use \n in the arduino code.

My apologies for not responding previously
Chris
yay! i managed to solve the problem with your help! thanks so much! :D
okay thank you! and sorry for taking up your time to do that! At the same time i'll try to figure out too! :)
chrisgward (author) 2 years ago
Hello,
A forms load event is only ever called once (there are exceptions, but this instance covers none). I recommend you use the timer control to query the Arduino every half a second or so. If you'd like me to explain this further, please let me know :)
Ok thank, I'll try that.
Thank you so much! Works perfectly now. I will favorite your article.
uncledj2 years ago
I am c# programmer, I waste a lot of time try the c# in ardunio, at the end, I give up. mainly, many HW library is not equip as ardunio library, even I know how to convert back to c# code. But what is the point for my objective, learn the language or make sth happen in ardunio, and is stupied idea add extra code in the assamble code.
chrisgward (author)  uncledj2 years ago
I'm not quite sure what you mean. C# is run on the computer, and the Arduino runs standard Arduino code, which uses the serial library to communicate with the PC.
nice job
redryno12214 years ago
Awesome I've been looking for a tut on just this...