Pages: [1]   Go Down
Author Topic: wait until full value is send:  (Read 1811 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello everybody, I send a value through this code to the serial port:  Serial.print( data, DEC );
my question is the part in processing: I catch the value with this code:

  while( port.available() > 0 )
  {
    byte[] inByte = port.readBytes();
  
    if (inByte != null)
    {
       String myString = new String( inByte );
       println( myString );
      
    };

the problem is that processing sometimes don't wait until the whole value is already there.
for example if I send 300 sometimes I get 3 or 30. how do you handle it to wait for the whole value?

thanks. koji

PS: can I somehow send a zero byte and check if inByte == null?
and or what does this means:
Serial.println() just prints a carriage return and a linefeed.  ?

and another question ist delay at the end of a loop ( delay(100); )
why do I have to use this? and how do you know the value how long the programm should wait before the next execution starts?
« Last Edit: September 18, 2006, 03:43:15 pm by koji » Logged

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

ok I think I understand println(). I use it so: Serial.println( data, DEC );

but how can I test the incoming byte? this doesn't work  :-/:
if( inByte != 10 || inByte != 13 )  // doesn't work  

koji.



the whole code:

while( port.available() > 0 )
  {
    byte[] inByte = port.readBytes();
    
    if( inByte != null )
    {
      String buf = "";
      println( inByte  ); // traces "wanted value", 10 & 13
      
      if( inByte != 10 || inByte != 13 )  // doesn't work
      {
          String myString = new String( inByte );
          buf += myString;
          println( "yeah" );
      }
      else
      {
          println( buf );
          buf = "";
          
      };
  };
« Last Edit: September 19, 2006, 04:54:21 am by koji » Logged

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

It should be:

Code:
if( inByte != 10 && inByte != 13)

That is "and" instead of "or".  Since one of inByte != 10 or inByte != 13 will always be true.
Logged

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

ah yes, thanks but it also doesn't work with an and :

if( inByte != 10 && inByte != 13)

error message:

Error: The type of the left sub-expression, "byte[]", is not compatible with the type of the right sub-expression, "int".


this unfortunately doesn't work too:

byte a = 10;
byte b = 13;
            
if( inByte != a && inByte != b )  // doesn't work
« Last Edit: September 19, 2006, 05:23:57 am by koji » Logged

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

Ah, right.  You probably want to read one byte at a time.  I'm not sure what the Processing syntax is, but maybe something like:
byte inByte = port.readByte();
Logged

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

no, I tried this already, this doesn't work saddly neither. when I use " byte inByte = port.readByte(); "

I get the error message:
Error: The type of the left sub-expression, "byte", is not compatible with the type of the right sub-expression, "null".

Logged

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

How about:

Code:
char inChar = port.readChar();

More info here: http://processing.org/reference/libraries/serial/index.html
Logged

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

Hello Mellis, thank you but the problem is the same. I have to check in processing that the value is completely there. I know it would be possible to send an character in the end of the value which will be checked in processing and recognized as termination character, but it would be possible that my value is sometimes the same one like the termination character, which would be undesireable.
« Last Edit: September 19, 2006, 08:35:32 am by koji » Logged

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

You should be able to use port.readChar(), as Serial.print(number, DEC) sends a series of ASCII digits (e.g. '1', '2', '3' which is 49, 50, 51) which will never have the same value as a newline or carriage return.
« Last Edit: September 19, 2006, 09:56:34 am by mellis » Logged

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

hey mellis, I am sorry I didn't get it. can you explain it a little further. what do you mean with this:

" a series of ASCII digits (e.g. '1', '2', '3' which is 49, 50, 51) which will never have the same value as a newline or carriage return. "

do you mean to use Serial.println(number, DEC) twice? & do you know if there is a way to send a zero byte in arduino? thanks so much.

koji
« Last Edit: September 19, 2006, 12:53:57 pm by koji » Logged

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

I used this code in Processing to receive an integer value (as an ASCII string) from Arduino. Basically I wait for something to appear on the serial port. If it is the letter 'r' (which indicates a sensor reading in my app), then start accumulating characters if they are numbers, until I get something which is not a number. So far it works very well.

while (serialPort.available() == 0) {
}; // wait for something to come back

incomingByte = serialPort.read();
if (incomingByte == 114){ // got "r", we are receiving a reading
  while (!done) {
    if (serialPort.available()>0){
      incomingByte = serialPort.read();
      if (incomingByte >= 48 && incomingByte <= 57) { // it's a number
        returnValue = returnValue * 10 + (incomingByte - 48);
        }
      else {
        done = true; // or else we're done
        }
     }
  }
Logged

Pages: [1]   Go Up
Jump to: