another serial question, sigh

Sorry if this is redundant. I found some similar posts but none that answered my stupid question.

I have my arduino printing intigers to serial (0-999), and everything is fine when I use the serial monitor in arduino. For now I’m just using a constant int (as in Serial.println(666)) but this will be input from sensors. So, as expected, in the serial monitor I get:

  
  
666  
666  
666  
666  
...  
  

Using this code in OF:

  
  
void testApp::update() {  
      
    // (1) write the letter "!" to serial -   
    //this will tell the arduino we ready to get data:  
    serial.writeByte('!');  
  
    // (2) read  
    // we try to read 2 bytes  
  
    //clear our variables  
    int nTimesRead  = 0;  
    int nBytesRead  = 0;  
    int nRead       = 0;  // a temp variable to keep count per read  
  
    //we are going to read a 0 - 1024 number as two bytes  
    //we need a buffer to store the two bytes and a second   
    //buffer with space for the terminating zero byte   
    unsigned char bytesReturned[2];  
    unsigned char bytesReadString[3];  
  
    //clear our buffers  
    memset( bytesReadString, 0, 3 );  
    memset( bytesReturned, 0, 2 );  
  
    //we read as much as possible so we make sure we get the newest data  
    while ( (nRead = serial.readBytes( bytesReturned, 2)) > 0 ) {  
        nTimesRead++;     
        nBytesRead = nRead;  
    }  
  
    //if we have got both bytes  
    if ( nBytesRead == 2 ) {  
        //lets update our buffer  
        memcpy( bytesReadString, bytesReturned, 2 );  
        printf( "%s", bytesReadString );  
    }  
}  
  

… which is of course just the sample code for serial. so, the printf() method outputs this:

  
  
6  
66  
6  
666666666  
66  
  
66666  
6  
  
  
66  
666  
  
  
66  
6  
  
  
6  
6  
  
  
66666666  
6  
666  
666  
  
  
  
66  
6666  
666666666  
66666  
666  
6  
66  
666  
666  
666  
666  
66  
666  
666  
666  
  
666  
666  
6666  
66  
  
666  
6666  
66  
666  
66  
666  
6  
666  
6666  
  
666  
666  
  
666  
66666  
...  
  

They should be parsed properly like, 666, 666, 666, etc… I just don’t understand what is going on here. Any thoughts are appreciated… I’m guessing it’s a very basic question :slight_smile:

Don’t use println() on the Arduino side, you want to just use Serial.write() and use a control character or message to indicate the end of a line, so your code looks like this:

  
  
Arduino.write(msg);  
Arduino.write('X'); // or something that you're not using in your protocol, \0 is always a classic  
  

Another thing is that if you know you’re expecting 3 bytes (or 4 with a control character), go ahead and read 3 bytes on the OF side:

  
  
    //we read as much as possible so we make sure we get the newest data  
    while ( (nRead = serial.readBytes( bytesReturned, 3)) > 0 ) {  
        nTimesRead++;     
        nBytesRead = nRead;  
    }  
  

the readBytes() call blocks, but if you know that the Arduino will always be writing values it should be ok. You can also read 2 bytes at a time if you want and check for your control character, i.e.

  
  
if(bytesReturned[2] == 'X')  
  

Excellent. Thank you kindly! I should have read the docs more closely on println(). Doh!