Pages: [1]   Go Down
Author Topic: C# communication help  (Read 435 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
Karma: 1
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Offline Offline
God Member
Karma: 0
Posts: 731
skcor oniudrA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Here's a c++ example

You might be able to re write it for c#

Austin, TX USA
Offline Offline
God Member
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial with C# is pretty easy.  No need to hunt around for libraries -- .NET is already there!

using System.IO.Ports;

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

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

  // 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.


« Last Edit: February 19, 2009, 05:37:31 pm by mikalhart » Logged

Pages: [1]   Go Up
Jump to: