Number up to 180 from C# serial print mapped to servo movement

Okay, I'm thinking the title probably isn't very descriptive but it's late and it's the best I can do. Basically, I've written a program in C# (I shall unveil this on here in a few days) that ouputs a value between 0 and 180 based on certain conditions. I want to be able to take this value and print it over the serial and read it at the Arduino end as an integer, so i can just have myServo.write(int).

At the moment, in C# I'm using sp.Write(int.ToString()); but I can use the same command to write a byte array and a char array.

Based on this, first of all, what's the bend form to send it through the serial in? Second of all, how do I pick that up at the other end and make it back into an integer? I'm fairly new to the Arduino side of this so sorry for no copies of my attempts at my own Arduino code..!

Many thanks,
Toby 

I can’t see why you would

I can’t see why you would send it like a string in the first place. On the write method there is an overload for sending any number of bytes to the serial.
http://msdn.microsoft.com/en-us/library/ms143551.aspx
So if you put your value in a 1 element array and just send the value instead of the string representation, the Arduino could just read it directly.

I admit not knowing enough to be really helpful, but,

couldn’t you just send the ascii character? An arduino could surely decode an ascii character to a number after it is sent, right?

This is what I thought, but

This is what I thought, but I don’t have a clue about how sending values over the serial works so I’m not sure how to do it! Would I just put the integer value into a byte array using something like this?

            byte[] bytes = BitConverter.GetBytes(servoPosInt);
if (BitConverter.IsLittleEndian)Array.Reverse(bytes);
byte[] servoPosBytes = bytes;
sp.Write(servoPosBytes, 0, servoPosBytes.Length);

Or is there a simpler way?

Thanks for your help!

Toby

I did try that, and for

I did try that, and for numbers from 1 to 9 that works fine, as you can just do Serial.read() - 48 to convert the ASCII into an integer. Couldn’t make it work though for anything bigger, but I might have been approaching it from the wrong angle!

Okay, so that definitely

Okay, so that definitely works - if I send 123 and tell the Arduino to light the LED on pin 13 if Serial.read() == 123, the LED lights. However, if I tell it to turn off if Serial.read() == 321 nothing happens. I’m presuming this is because I need to clear the serial buffer? I tried using Serial.flush() but that didn’t work…

This is my really rather basic Arduino code:

  val = Serial.read();
if (val == 123)
{
digitalWrite(13, HIGH);
}
if (val == 321)
{
digitalWrite(13, LOW);
}
Serial.flush();

.

Not here

If you are using standard serial

of start bit, 8 bits data and stop bit then 255 is maximum you can send. To send 321 would require 9 bits.

Ah, I see! I tried changing

Ah, I see! I tried changing 321 to 180, which is the maximum I need for the servo and it worked perfectly. Thanks for your help!

Edit: woops, this was meant to be a reply to merser.