C# to Arduino Serial connection for servo control

Using a webcam and AForge.net (very good btw) to determine the position of people in the webcams FoV. I have the C# program showing the variables I’m sending to the Arduino and the data received from the Arduino and they match but the servo is not moving in relation.

Control Process
The C# program is send the integer as a string
The Arduino is reading the serial and sending it back (so I can check it is correct)
The Arduino then moves the servo according to the value (but it isn’t :~)

Click picture to see the program functioning perfectly whilst the Arduino just doesn’t want to. Console Output is Image.Width average, Servo rotation average, Servo rotation integer. The one saying Read: first is the returned integer by the Arduino.

Full Arduino code

#include <Servo.h>
Servo head;

void setup()
{
  Serial.begin(57600);
  head.attach(5);
}

void loop()
{
  int angle;
  if (Serial.available() > 0) {
    angle = Serial.read();
    Serial.println(angle);
    head.write(angle);
  }
}

The C# program is send the integer as a string

And yet you read a single character and assume that that character represents the angle:

  int angle;
  if (Serial.available() > 0) {
    angle = Serial.read();

That single character doesn't represent the whole value. You need to include some sort of marker to indicate the end of the value string.

See all the . above, that indicate the end of sentences, and all the spaces that indicate the end of words? The Arduino will happily keep reading characters and appending them to an array until the array overflows, without ever knowing where one value ends and another begins, unless you help it out.

Realising the stupidity of C# sending a string to the Arduino and then returning the string so willingly I have made modifications.
The C# program now always creates a three digit number for the integer angle. ie 23 = 023.
The Arduino sketch reads into a 3 character buffer and then run it through atoi to produce the proper int (new code below).
As I understand this it should now work given the correct use of variable types and all but I now cannot upload to the Arduino.
It is a 2560 and I’m running 7 32-bit.
I have tried to upload a small sketch as advised in some posts but just cannot. I receive this error now :S

void loop()
{
  int i;
  char a[3];
  if (Serial.available()>0) {
    for(i=0; i<3; i++){
      a[i] = Serial.read();
    }
    int b = atoi(a);
    Serial.println(b);
    head.write(b);
  }
}

I receive this error now

This error?

  char a[3];
  if (Serial.available()>0) {
    for(i=0; i<3; i++){
      a[i] = Serial.read();
    }
    int b = atoi(a);

You have an array that can hold three characters. If there is at least one character to be read, you read all three of them. Well, I don’t think that is right.

Then, you pass a char array to a function that expects a string, and you expect decent results back. I don’t think that will happen.

The difference between a char array and a string is that the string is a char array that is NULL terminated. Three characters plus a NULL requires an array that can hold at least 4 characters. And, of course, it requires that you set the last position to NULL.

Ok so I have flashed the new firmware to the 8u2 and its stopped the unable to upload glitch.
From further searching I found out about the null termination although first mention was \0 which totally confused me. I am forming a three character string in the C# app adding the ‘\0’ to the fourth digit of the array.
A bufferUntil would be nice so I could vary the string length and add the null where needed to. Anyway for Arduino to do that?

C# transmits “085”
Arduino transmits “0” “8” “5” I’m guessing this is thanks to println

int angle;
int i;
char buffer[3];

void setup()
{
  Serial.begin(57600);
  head.attach(5);
}

void loop()
{
  if (Serial.available())
  {
    for (i=0;i<3;i++)
    {
      buffer[i] = Serial.read();
    }
    buffer[3] = '\0';
    angle = atoi(buffer);

    Serial.println(angle);
    head.write(angle);
  }
}

A bufferUntil would be nice so I could vary the string length and add the null where needed to. Anyway for Arduino to do that?

Sure.

    for (i=0;i<3;i++)
    {
      buffer[i] = Serial.read();
      buffer[i+1] = '\0';  // The buffer is now always NULL terminated
    }

There is still a problem. You are testing that there is at least one character available to read, and then reading 3 of the 1 available characters. Can you not see that this is a problem?

C# transmits “085”
Arduino transmits “0” “8” “5” I’m guessing this is thanks to println

No, it isn’t. It is due to you reading before data has arrived.