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 & HelpSyntax Questions › read out serial port (byte)
Page Index Toggle Pages: 1
read out serial port (byte) (Read 634 times)
read out serial port (byte)
Sep 19th, 2006, 2:31pm
 
hello everybody, I have a problem with my code. I want to read out the serial port. It works, but the problem is that processing doesn't wait until the whole value is already there. therefore I want to check the byte with an if query. but now I get an error message.


the problem is this line in my code:
if( inByte != 10 || inByte != 13 )  // doesn't work


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


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;

    }
    else
    {
        println( buf );
        buf = "";
       
    };
 };

thanks in advance, koji.
Re: read out serial port (byte)
Reply #1 - Sep 19th, 2006, 3:00pm
 
The problem is that inByte is an array (note how it is declared with brackets), meaning a list of bytes.  With arrays, you must refer to individual elements of the array, via an index value.  An array of 10 elements has indices 0-9, for example.  So more likely you want to do something like:

Code:

// checking the first element
if (inByte[0] != 10) {
}

//checking the last element
if (inByte[inByte.length-1] != 10) {
}


You could also use the read() method, which would allow you to read the values in one at a time (not into an array).

http://processing.org/reference/libraries/serial/Serial_read_.html
Re: read out serial port (byte)
Reply #2 - Sep 19th, 2006, 10:31pm
 
hey shiffman: thank you, I made it now like this but still have one problem:

   while( port.available() > 0 )
   {      
      char inChar = port.readChar();
     
      String buf = "";
     
      if( inChar != 'e' )
      {
        String myString = new String( inChar );
        buf += myString;

      }
      else
      {
        println( buf );
        buf = "";
       
      };

 };  


I send a termination character in the end ( in my case 'e').
the new problem is now the transformation from an char in an String which I probably need:
String myString = new String( inChar );

can you please help me once more and if you have any other suggestion that you would do different, please let me know, thanks. koji.


edit: this also doesn't work unfortunately:

      if( inChar != 'e' )
      {
        buf += str( inChar );



Re: read out serial port (byte)
Reply #3 - Sep 19th, 2006, 11:24pm
 
In Java, you can concatenate a String together out of characters, you don't need to do any conversion.

Code:

buf += inChar;


will be fine as long as you have declared buf to be an empty String (String buf = "";)
Re: read out serial port (byte)
Reply #4 - Sep 19th, 2006, 11:34pm
 
hey shiffman, it unfortunately doesn't work.
I get an empty string (buf) in the end.
I tested it with the change of this:

String buf = ""; into String buf = "w";

then it traces out w in the end. it seems it doesn't add anything to the string.
I also put an println into the if query and it goes into the query:
if( inChar != 'e' )
{
  println( inChar ); //works


thanks, koji.

Re: read out serial port (byte)
Reply #5 - Sep 20th, 2006, 3:46am
 
Hey Koji,

In reference to your code you posted above, I notice that you're calling this...

Code:
String buf = "";

... every time you iterate through the while loop.

This will reset the string to "" everytime the loop is run. But, if you want to add to the string with every iteration, you'll have to declare buf outside of the loop.

Code:
String buf = ""; 
while( port.available() > 0 )
{
char inChar = port.readChar();
if( inChar != 'e' )
{
String myString = new String( inChar );
buf += myString;
}
else
{
println( buf );
buf = "";
};
};

Your else{} condition will take care of resetting buf.
Re: read out serial port (byte)
Reply #6 - Sep 20th, 2006, 9:55am
 
oh damn, that's it. thanks so much. weirdly, sometimes I still  don't get the whole value and sometimes it prints an empty string, which can't be true.

if I check this I get each number back to back:

char inChar = port.readChar();  
if( inChar != 'e' )  
{  
   println( inChar );

}



but with the code below, it prints sometimes a empty string or not the whole value, do you still see any bugs?:
 


   String buf = "";
   
   while( port.available() > 0 )
   {      
      char inChar = port.readChar();
     
      if( inChar != 'e' )
      {
        buf += inChar;

      }
      else
      {
        println( buf );
        buf = "";
       
      };
   };

 
sorry for the long walk, thank you & best regards,
koji
Page Index Toggle Pages: 1