Sie sind auf Seite 1von 4

Connect to the Arduino with C# http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

Home Physical Computing Connect to the Arduino with C#


MAIN MENU
Home
Sunday, 13 September 2009 12:12 John Spicer
Products

Physical Computing One of the coolest things about the Arduino environment is its compatibility with almost
anything. After doing a few projects using only the board and some other components, I
Excel Programming
started to realise the limitations of only using such a hardware based environment. Being
About Technicana able to make a robot was one thing, but wouldn’t it be much cooler to be able to, say, email
Technicana's Blog you when something happened or control it from a computer program?

Links That is what I’ve done on this project. Using an Arduino board, Visual C# and a tutorial
from the Microsoft 'Coding for Fun' site, I’ve made a simple program thats controlled from a
Windows Form. The user can turn the LED on an Arduino board on and off, then e-mail the
state of it to their e-mail address. It’s a proof of concept project, that could hopefully be
applied to more useful things.

Before going on to explaining the program, I’d like to say a bit about what I think is the
coolest platform for this sort of thing - C#. I spent hours trying to send e-mails using
command prompt and php scripts, but C# deals with all the tricky low level coding so it’s a
lot easier to make much cooler programs. For home robotics projects, I’d recommend C# to
absolutely anyone.

Right then, on to the project. Below is the form as it would appear when it first opens. The
user can press the ‘On’ button, which would then turn the LED on the arduino board on.

Once the ‘On’ button has been pressed, the ’Off’ button becomes pressable:

And once the ‘Off’ button is pressed and an e-mail address is entered:

2 af 6 20-08-2010 15:59
Connect to the Arduino with C# http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

Arduino Code

void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}

void loop()
{
if(Serial.available())
{
int c = Serial.read();
if (c == '1')
{
digitalWrite(13,HIGH);
}
else if (c == '0')
{
digitalWrite(13,LOW);
}
}
}

C# Code

I'd recommend putting in some error checking if this were a real project, which I haven't
done here. The two main areas for checking are the e-mail address and the connection to
the serial port.

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

namespace SerialCommunication
{
public partial class Form1 : Form

3 af 6 20-08-2010 15:59
Connect to the Arduino with C# http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

{
bool ledState = false;

public Form1()
{
InitializeComponent();

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

private void Form1_FormClosing(object sender,


FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}

private void buttonOn_Click(object sender, EventArgs e)


{
serialPort1.Write("1");
textBox1.Text = "LED is on!";
buttonOn.Enabled = false;
buttonOff.Enabled = true;
ledState = true;
}

private void buttonOff_Click(object sender, EventArgs e)


{
serialPort1.Write("0");
textBox1.Text = "LED is off!";
buttonOn.Enabled = true;
buttonOff.Enabled = false;
ledState = false;
}

private void send_mail(string address)


{
System.Net.Mail.MailMessage message = new
System.Net.Mail.MailMessage();
message.To.Add(address);
message.Subject = "Hi there.";
message.From = new System.Net.Mail.MailAddress("
you@example.com ");

if (ledState == true)
{
message.Body = "The LED is on";
}

if (ledState == false)
{
message.Body = "The LED is off";
}
System.Net.Mail.SmtpClient smtp = new
System.Net.Mail.SmtpClient("smtp.ukgateway.net");

4 af 6 20-08-2010 15:59
Connect to the Arduino with C# http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

smtp.Send(message);
}

private void buttonSend_Click(object sender, EventArgs e)


{
send_mail(textBox_mail.Text);
}
}
}

Incidentally, the following book is an excellent primer for getting started with the Arduino
board.

Comments (3)
You're Welcome 3. Thursday, 19 August 2010 22:48
John Spicer
No problem guys. Thanks for the feedback.
thanks 2. Sunday, 18 July 2010 23:38
Administrator
thanks man. I was searching a project simple like yours to communicate with the arduino in
c#. hugs
Thank 1. Thursday, 01 April 2010 17:23
Administrator
Than man, great collaboration, i`m begin with arduino, i`m from Chile, if you have more
stuff about this, i like if you can send me this. Mi intention is learn to programar this micro
and appli this un proyect to make businnes, if you whant to go with me, this be great. Mi
email i chinodread@hotmail.com Bye...

Add your comment


Your name:

Subject:

Comment:

POST PREVIEW

yvComment v.1.24.0

5 af 6 20-08-2010 15:59

Das könnte Ihnen auch gefallen