parsing integers through serial


i realise basic question, i've spent last couple of days trying figure out myself , have figured total lack of basic low-lvl programming telling me not skip on fundamentals.

problem need parse integer value python on computer arduino diecimila through serial connection , i'm amazingly confused when i'm parsing strings, bits, bytes, integers , whatever other types there are.

what need value between 700 , 2400 (pwm ms signal) , i'd parse 2 bytes instead of 4 character string containing numbers, can't seem figure out how convert integer in python 2 bytes can send arduino , convert them value again?

i've missed obvious in case i'm sorry wasting time haven't been able find documentation make sense of.

thanks in advance.
cheers

usually send 1 byte @ time on serial.

what send on larger values 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, little bit shorter if like:
unsigned int value = 65535;
serial.print(value & 0xff);
serial.print((value >> 8) & 0xff);

and
unsigned int value = serial.read() | serial.read() << 8;

this arduino code, , data types, because i'm not sure how in python. hope you'll idea , write python code yourself.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > parsing integers through serial


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums