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) { }
}

Comments

8 Responses to “Talking to an Arduino from .NET / C#”

  1. Mike Barrett on February 16th, 2009 8:14 pm

    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

  2. jochen on February 18th, 2009 10:23 pm

    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 :-)

  3. Wes on February 19th, 2009 1:06 am

    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

  4. jochen on February 19th, 2009 9:45 am

    I’m not sure how compatible the Sanguino is with the Arduino and it’s bootloader software. I don’t have one to try.

  5. wes on February 19th, 2009 1:02 pm

    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

  6. jochen on February 19th, 2009 2:49 pm

    Hmmm. What errors are you getting? In the simplest case your Sanguino might just not be on COM4 ;-)

  7. wes on February 19th, 2009 6:57 pm

    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.

  8. teh b on December 10th, 2009 4:56 pm

    your code was just what i was looking for, working great, thanks man!

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

-->