We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › how does serial write split ints
Page Index Toggle Pages: 1
how does serial write split ints? (Read 711 times)
how does serial write split ints?
Mar 20th, 2008, 7:23am
 
hello,
im asking this more out of curiosity than anything else since i solved my problem, but i would still like to know how this works.

when i send an integer above 255 it gets split to two bytes before it gets sent, and i would like to know how that works so i can reassemble it in arduino.

right now i split it myself with the following code:

void send(int byte16)
{
 int half1=byte16>>8&255;
 int half2=byte16&255;
 myPort.write(half1);
 myPort.write(half2);
}

then put it back together like:

if (Serial.available() > 1) {
   int byte1 = Serial.read();
   int byte2 = Serial.read();
   int byte16=byte1<<8|byte2;
}

which works just fine:). but, im still curious.

tnx
Page Index Toggle Pages: 1