Talking to an Arduino from .NET / C#
I’ve been meaning to post this for a while but got caught up with work stuff again. Thinking that talking to an Arduino from C# should be fairly easy, I spent a good hour or so fiddling with it because I could never get the Arduino to run my sketch after establishing the serial connection. Turns out, the DTR Enable line needs to be set to high for it to start executing the program. Here’s a quick hack which interacts with the program I posted previously:
private static System.IO.Ports.SerialPort serialPort1; static void Main(string[] args) { System.ComponentModel.IContainer components = new System.ComponentModel.Container(); serialPort1 = new System.IO.Ports.SerialPort(components); serialPort1.PortName = "COM4"; serialPort1.BaudRate = 9600; serialPort1.Open(); if (!serialPort1.IsOpen) { Console.WriteLine("Oops"); return; } // this turns on ! serialPort1.DtrEnable = true; // callback for text coming back from the arduino serialPort1.DataReceived += OnReceived; // give it 2 secs to start up the sketch System.Threading.Thread.Sleep(2000); using (serialPort1) { Console.WriteLine("sending green command"); serialPort1.Write(new byte[] { 114 } , 0, 1); System.Threading.Thread.Sleep(1000); Console.WriteLine("sending red command"); serialPort1.Write(new byte[] { 103 }, 0, 1); System.Threading.Thread.Sleep(15000); } } private static void OnReceived(object sender, SerialDataReceivedEventArgs c) { int ch; try { // write out text coming back from the arduino Console.Write(serialPort1.ReadExisting()); } catch (Exception exc) { } }
February 16, 2009 | Filed Under Electronics
Comments
8 Responses to “Talking to an Arduino from .NET / C#”
Leave a Reply
rad! I’ve really got to get back into EE stuff, and Arduino seems pretty awesome.
my buddy Ada does some cool electronics stuff, you can find some of her projects here: http://www.ladyada.net/make/index.html
You actually know Ada? Yeah, been to that web site a ton of times. Cool stuff, small world.
I’ve been hacking C code here at the hotel, insane fun — next steps to get my own PCBs printed :-)
Hi,
I’m trying to learn how to get my Sanguino to communicate with my computer using your example but I keep getting errors. I’m using visual c# 2008 express and the AVR software but I’m a novice in both. Any suggestions?
Thanks,
-Wes
I’m not sure how compatible the Sanguino is with the Arduino and it’s bootloader software. I don’t have one to try.
Hi,
I’ve got the Sanguino portion runnig fine. I’m having troubles with the C# code. The Sanguino is just Arduinos big brother, I used the c program from you privous post and compiled it in the AVR without any problems
Hmmm. What errors are you getting? In the simplest case your Sanguino might just not be on COM4 ;-)
The errors I get are in Visual Studio C# 2008 express when I’m try to debug the c# code. If you send me an email or let me know where to send you an email. I’ll send you a screen shot with the errors I’m getting in VS.
your code was just what i was looking for, working great, thanks man!