parsing integers through serial

I realise that this is probably a very basic question, however I've spent the last couple of days trying to figure it out by myself and have figured that my total lack of basic low-lvl programming is telling me not to skip over the fundamentals.

Problem is that I need to parse an integer value from python on my computer to an Arduino Diecimila through the serial connection and I'm amazingly confused about when I'm parsing strings, bits, bytes, integers and whatever other types there are.

What I need is a value between 700 and 2400 (pwm ms signal) and I'd like to parse it as 2 bytes instead of a 4 character string containing numbers, but I can't seem to figure out how to convert an integer in python to 2 bytes that I can send to the arduino and then convert them back to a value again?

I've probably missed something obvious in which case I'm sorry for wasting your time but I haven't been able to find any documentation that I could make sense of.

Thanks in advance. cheers

Usually you only send one byte at a time over serial.

What you could do to send over larger values is something like this:

To send:
unsigned int value = 65535;
byte dataByte1 = value & B11111111;
byte dataByte2 = (value >> 8) & B11111111;
Serial.print(dataByte1);
Serial.print(dataByte2);

To read:

byte dataByte1 = Serial.read();
byte dataByte2 = Serial.read();
unsigned int value = dataByte1;
value = value | (dataByte2 << 8);

Or, a little bit shorter if you like:
unsigned int value = 65535;
Serial.print(value & 0xFF);
Serial.print((value >> 8) & 0xFF);

And
unsigned int value = Serial.read() | Serial.read() << 8;

This is Arduino code, and data types, because I’m not sure how to do this in Python. I hope you’ll get the idea and write the python code yourself.

Sounds like you want to learn about highByte() and lowByte(). Under "Bits and Bytes" section here... http://arduino.cc/en/Reference/Extended

It’s a good idea in general to learn how to do these things, because it teaches you how the systems really work. What the previous posters are explaining is that an integer in the range you are trying to send can be broken into bytes by “shift and mask” operations. The “mask” is an AND operator ("&"), the shift is the “>>” and “<<” operators. You mask off the bottom byte, then you shift and mask the top byte to encode, then you shift and combine, using the OR ("|") operator to combine them on the Arduino. If you convert numbers to binary, and use the basic AND/OR operations, you can work out how this does what you want. Do watch out about signed vs unsigned quantities here: you want unsigned bytes.

Having said that, you are probably better actually sending the 4 character string. It’s easier, it’s clearer, and it’s easier to debug. It certainly is less efficient, but in this case, what is more important?

i had a similar questions a few days ago, see if this helps:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1264962652