Go Down

Topic: C# communication help (Read 442 times) previous topic - next topic

vinhtvu2

Hey guys, I'm new here, just bought a couple of Arduino to play around with.

I'm a C# programmer at heart, so I'd like to learn how to use the Arduino with C# so I can write GUI programs and such for it.

My programming would be pretty simple. I saw a video on youtube a C# program controlling a couple LEDs... when you click on the program, it turns on the LEDs on the Arduino. Sounds simple... but how do I control the Arduino with C#????

The website for the project was no help since it was in Chinese...
Anyhelp would be appreciated... or at least point me in the right direction. I've done quite a bit of searching and hasn't found much.

John_Ryan

Hunt around for a C# serial library, then talk to Arduino over usb.

Here's a c++ example

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1159335334/4#4

You might be able to re write it for c#

mikalhart

#2
Feb 19, 2009, 11:33 pm Last Edit: Feb 19, 2009, 11:37 pm by mikalhart Reason: 1
Serial with C# is pretty easy.  No need to hunt around for libraries -- .NET is already there!

Code: [Select]
using System.IO.Ports;

 // to open a port
 SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
 port.Open();

 // example: forever displaying incoming characters
 while (true)
   if (port.BytesToRead > 0)
   {
     char c = (char)port.ReadByte();
     Console.Write(c);
   }

 // example: writing a few bytes to the port
 byte[] arr = new byte[4];
 arr[0] = arr[1] = arr[2] = arr[3] = c;
 port.Write(arr, 0, 4); // write the 4 bytes

 // example: writing a string
 port.Write("Hello, world!");


Just look in the documentation for System.IO.Ports.SerialPort for more goodies.

Mikal


Go Up