Go Down

Topic: wait until full value is send: (Read 2201 times) previous topic - next topic

koji

Sep 18, 2006, 10:08 pm Last Edit: Sep 18, 2006, 10:43 pm by koji Reason: 1
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?

koji

#1
Sep 19, 2006, 11:53 am Last Edit: Sep 19, 2006, 11:54 am by koji Reason: 1
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 = "";
         
     };
 };

mellis

It should be:

Code: [Select]

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


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

koji

#3
Sep 19, 2006, 12:03 pm Last Edit: Sep 19, 2006, 12:23 pm by koji Reason: 1
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

mellis

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();

koji

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


mellis

How about:

Code: [Select]

char inChar = port.readChar();


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

koji

#7
Sep 19, 2006, 03:34 pm Last Edit: Sep 19, 2006, 03:35 pm by koji Reason: 1
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.

mellis

#8
Sep 19, 2006, 04:55 pm Last Edit: Sep 19, 2006, 04:56 pm by mellis Reason: 1
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.

koji

#9
Sep 19, 2006, 07:53 pm Last Edit: Sep 19, 2006, 07:53 pm by koji Reason: 1
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

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
       }
    }
 }

Go Up