Mysterious serial println sign somewhere?

Hi there,

I’m working on a simple example for my ofxSimpleSerial addon and I’m running into a strange problem.
The example exists of communicating one character over a serial communication from an Arduino to openFrameworks.
When you have multiple variables to send I think it’s “best practice” to separate them with a , and end it with a Serial.println.
But when I only have one var to send from the Arduino and work with the println I don’t simply get this one character. I know this because I can’t compare the message between the \n’s.

Just getting the first character doesn’t help.
&message.at(0)

Splitting messages with a | sign for example does work, but I would like to work with existing best practices

You can find the current version of my addon, with examples on google code:
http://code.google.com/p/ofxsimpleserial/

Do you guys / girls have any clue?

Code summary:
My Arduino code:

  
int switchPinR = 2;  
int switchPinG = 3;  
int switchPinB = 4;  
int ledPin = 11;     
  
void setup()  
{  
  pinMode(switchPinR, INPUT);  
  digitalWrite(switchPinR, HIGH);  
  pinMode(switchPinG, INPUT);  
  digitalWrite(switchPinG, HIGH);  
  pinMode(switchPinB, INPUT);  
  digitalWrite(switchPinB, HIGH);  
    
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);  
}  
  
void loop()                       
{  
  if(Serial.available() > 0 && Serial.read() == 'r')  
  {  
    int switchValueR = digitalRead(switchPinR);  
    int switchValueG = digitalRead(switchPinG);  
    int switchValueB = digitalRead(switchPinB);  
   
    if(switchValueR == LOW)  
    {  
      Serial.println('r');  
      digitalWrite(ledPin, HIGH);  
    }  
    else if(switchValueG == LOW)  
    {  
      Serial.println('g');  
      digitalWrite(ledPin, HIGH);  
    }  
    else if(switchValueB == LOW)  
    {  
      Serial.println('b');  
      digitalWrite(ledPin, HIGH);  
    }  
    else  
    {  
      Serial.println('x');  
      digitalWrite(ledPin, LOW);  
    }  
    Serial.flush();  
  }  
    
  delay(100);  
}  

My openFrameworks read function

  
void ofxSimpleSerial::read()  
{  
	// if we've got new bytes  
	if(available() > 0)  
	{  
	   // we wil keep reading until nothing is left  
		while (available() > 0)  
		{  
		   // we'll put the incoming bytes into bytesReturned  
		   readBytes(bytesReturned, NUM_BYTES);  
		  
		   // if we find the splitter we put all the buffered messages   
		   //   in the final message, stop listening for more data and   
		   //   notify a possible listener  
		   // else we just keep filling the buffer with incoming bytes.   
		   if(*bytesReturned == '\n')  
			{  
			   message = messageBuffer;  
			   messageBuffer = "";  
			   ofRemoveListener(ofEvents.update, this, &ofxSimpleSerial::update);  
			   ofNotifyEvent(NEW_MESSAGE,message,this);  
			     
			   break;  
			}  
			else   
			{  
			   messageBuffer += *bytesReturned;  
			}  
		   cout << "  messageBuffer: " << messageBuffer << "\n";  
		}  
	     
		// clear the message buffer  
		memset(bytesReturned,0,NUM_BYTES);  
	}  
}  

My example where I just want to compare this one character:

  
void testApp::onNewMessage(string & message)  
{  
	cout << "onNewMessage, message: " << message << "\n";  
	  
	red = (message == "r");  
	green = (message == "g");  
	blue = (message == "b");  
}  

I don’t know if this helps, but Serial.println() appends a \r (ascii 13) followed by a \n (ascii 10) to the end of the string in the parameter, aka CRLF. See http://www.arduino.cc/en/Serial/Println

Though it’s kind of wonky and untested b/c I don’t have my arduino here with me to test, you might try adding a blank println("") before your call to Serial.flush(). I seem to remember that helping with similar issues.

I guess I ow you guys an apology for not reading the reference well enough, I guess didn’t suspected it was something like this.
Just filtering out the \r makes it work perfectly.

I removed the Serial.flush() call, it didn’t seem to make sense if I was already reading out the incoming data.