Pages: [1]   Go Down
Author Topic: Serial.read input from processing  (Read 999 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello everyone

i have a problem with receiving data from processing.
this is how i send it:
Code:
 port = new Serial(this, Serial.list()[0], 9600);
  port.write(1086);
and this is how i receive it in the arduino code:
Code:
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:
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
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: January 22, 2007, 07:23:37 pm by tripmaster » Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: