Simple C# Example

Does anyone have a simple C# example of pushing a command to an arduino board?

Just switching the pin13 led on or off would do as I can't seem to get the board to do a thing over the serial port (apart from freeze).

I need a simple sketch and some basic c# code as I am sure once I get it functioning at a very basic level I will be able to work the rest out.

Also is there any way of seeing what serial instructions are being received by the board? This way I can at least make sure the serial commands are even getting sent properly.

I do. Send me a PM with your e-mail address, and I'll send it to you. The application just sends what you type, like the serial monitor, and displays the reply that the Arduino generates.

You can send anything you like, and have the Arduino collect, parse, and use the data. "Hey duino, turn pin 13 on, please" or "13,1". Whatever is convenient for you to collect and parse and understand.

Thank you for sending across the code.

However, I can't get it to do anything. my board is running the uno firmata sketch provided with the ide (I assume this is the sketch your code is communicating with?).

My board flashes its pin13 LED as stated in the firmata sketch but as soon as I click on send in your app the LED goes off and there is no received data.

in fact if I start the serial monitor in the IDE it automatically receives the following message without me doing anything

A ! ! ! n

(The n has a spanish style accent over it)

When I send a string from this monitor the same thing happens as it does with the c# app (ie the boards light goes off (or stays on depending on the timing of the send) and I receive no message back.

However, I can’t get it to do anything. my board is running the uno firmata sketch provided with the ide (I assume this is the sketch your code is communicating with?).

Nope. You need to write a sketch for the Arduino that reads the serial data that you send it, and sends replies.

A simple test:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
   if(Serial.available() > 0)
   {
      char aChar = Serial.read();
      Serial.print(aChar);
   }
}

Fantastic! The serial monitor now works a treat and your c# app works once I made a change in the Read method.

It was timing out on the port.ReadLine() command. I changed it to the following and it works

byte[] s=new byte[3]; port.Read(s, 0, 3);

string message =Encoding.UTF8.GetString(s); this.SetText(message);

Obviously it only works if I enter 3 characters but it should not be hard to modify to read various numbers of characters. However do you have any idea why the readline would not work?

However do you have any idea why the readline would not work?

Perhaps because ReadLine() is waiting for a carriage return/line feed, and the Arduino is not sending one. The Read() function does not block waiting for a carriage return/line feed.