C# Serial Library

I have an Uno board and I am trying to simply light up the built in led on pin 13.

I have the following code.

SerialPort p = new SerialPort("COM3");
p.Open();

//Set pin mode to output.
byte[] buffer = new byte[] { 244,13, 1 };
p.Write(buffer, 0, 3);

//set pin 13 to 'on'
byte[] buffer2 = new byte[] { 144, 32, 0 };
p.Write(buffer2, 0, 3);
p.Close();

The tx light flashes a couple of times (presumably for each buffer write) but nothing else happens.

What am I doing wrong?

Just as an aside, am I even understanding the digital pin properly.

the Firmata spec says the following:

/* two byte digital data format

  • 0 digital data, 0x90-0x9F, (MIDI NoteOn, but different data format)
  • 1 digital pins 0-6 bitmask
  • 2 digital pins 7-13 bitmask
    */

So does this mean that the first byte is 144 to denote Digital Data, the second byte is the first bank of pins and the third byte is the second bank of pins?

ie byte[] buffer2 = new byte[] { 144, 1, 0 }; would switch on digital pin 0
byte[] buffer2 = new byte[] { 144, 0, 1 }; would switch on digital pin 7
byte[] buffer2 = new byte[] { 144, 64, 0 };would switch on digital pin 13

Any help on this would be great as I don't know if it is merely my code that is wrong or my whole understanding that is incorrect.

Ok, I missed on very big thing in my process and that was that I did not realise I needed to upload firmata to the board for the code to work.

However I have now done this and it blinks, so I know it has uploaded fine. The code still does not work though. I have even downloaded pre written code to control the board but nothing works. What else have I done wrong if other known working C# code is not controlling my board?

I am good at c# (it is my job) but this is my first foray into serial communication and it is driving me mad!

You also need to set the parametres of the port, like baudrate etc. in the c# code

I have set the ports parameters.

Also when using the pre-written .net libraries this is all set in the constructor.

It looks like the serial port is working as when I write a buffer to the board the tx light on the board lights up briefly for each serial buffer write.

Ok, I have now found something very odd (which may or may not point towards the issue)

If I reset the board the built in led on pin 13 flashes (as specified in the firmata sketch), if I send a buffer to the board the board freezes in the state it is in, ie if the led is on at the time I send the buffer the led then remains permanently on, or if it is off when I write the buffer the led remains permanently off.

What could cause this behaviour?

I have also realised that if I upload the standard firmata sketch rather than the uno specific sketch the led flash a few times then the tx light flashes (presumably signifying some sort of signal has been received) at which point the led stops flashing.

Not sure if this is significant or not......

You should ditch Firmata. I really don't understand why so many people try to use that poorly implemented protocol when developing one of your own is so simple.

Create a sketch, to start with, that simply echoes what is received on the serial port.

When the PC receives the same data back that it sent, you can be assured that the settings are all correct.

Then, have the Arduino collect the data, up to an end-of-packet marker into an array. Print the array to the serial port, to assure your self that the collection code is working.

Then, use strtok() and atoi() to parse the data and light the appropriate pin.

The issue is that this is the first time I have ever dabbled with serial ports and the first time I have used an external board.

Firmata seemed the obvious choice as there is so much material to be found on the net. I have run the Firmata sketch provided with Arduino and a .net firmata library that is used by many people with great success but for some reason the board is not behaving as expected.......

I would not have a clue where to start in writing my own serial interface sketch.

Surely even if Firmata is not perfect it should be able to handle switching on a digital pin?