Arduino Forum

Forum 2005-2010 (read only) => Software => Interfacing => Topic started by: tripmaster on Jan 22, 2007, 08:29 pm

Title: Serial.read input from processing
Post by: tripmaster on Jan 22, 2007, 08:29 pm
hello everyone

i have a problem with receiving data from processing.
this is how i send it:
Code: [Select]
 port = new Serial(this, Serial.list()[0], 9600);
 port.write(1086);

and this is how i receive it in the arduino code:
Code: [Select]
serbyte = Serial.read();
if (serbyte != -1) {
     dataArrayH[0] = serbyte-1000;
}


this works fine but i get a problem when i try to make this:
Code: [Select]
serbyte = Serial.read();
if (serbyte != -1) {
   if (serbyte >= 1000 && serbyte < 2000) {
      dataArrayH[0] = serbyte-1000;
   }
}


after searching the forum i think the problem is that the Serial.read has ASCII output
but i still didn't get my code to work.

could anyone axplain me why this: " if (serbyte >= 1000 && serbyte < 2000) {" is not working?

thx a lot
Title: Re: Serial.read input from processing
Post by: kg4wsv on Jan 22, 2007, 11:54 pm
Quote
could anyone axplain me why this: " if (serbyte >= 1000 && serbyte < 2000) {" is not working?

Because serbyte is a byte, and Serial.read() returns a byte, and a byte can only hold 255 possible values, so it will never be >= 1000.

Not sure how port.write works, but 1086 will be at least 2 bytes if it's in binary and 4 bytes if it's an ASCII rperesentation of a binary number.

-j
Title: Re: Serial.read input from processing
Post by: tripmaster on Jan 23, 2007, 12:45 am
well thx for that
so the idea i hade to solve my problem was wrong. or is there a was to transform the incoming bytes to integers?

i have to transfair 8 integers between 0 and 255 from processing to an array in arduino.
because arduino can not read arrays form the serial port, i thought that by adding 1000 to the first 2000 to the second and so on in processing and then check if the serial.read is between 1000 and 2000 for the first 2000 and 3000 for the second... i could fill my array in the correct order.

can you suggest me an other way to do that?
Title: Re: Serial.read input from processing
Post by: kg4wsv on Jan 23, 2007, 01:41 am
sorry, but I don't know how Processing handles integers, so I can't say for sure.  Maybe someone who knows processing can jump in here.

-j
Title: Re: Serial.read input from processing
Post by: mellis on Jan 23, 2007, 09:43 am
If you don't actually need all 256 values, you could use one of them (e.g. 0 or 255) to indicate the start of a transfer, then send all 8 values.  Otherwise, you could send the values as ASCII (with serial.write(str(123)); ), with some kind of separator and delimitor, e.g. a comma between each of the numbers and a newline after.  Although that requires a bit of work to reconstruct the numbers in Arduino.  
Title: Re: Serial.read input from processing
Post by: tripmaster on Jan 23, 2007, 12:28 pm
i need all 256 values
to make a string and send that could be a solution.
however i have no clue how to do this, but i try it.

thx