I am working with arduino duemilanove and some serial communication. What happens is, lets say arduino is suppose to send "abc123" to my pc as part of serial communication, but it sends "abc1" "\n(new line)" "23". This happens less frequently, but I want full proof serial communication. I don't know why this happens, as it breaks the chain of communication.
The following is my code
void sp(String str)
{
delay(10);
Serial.print(str);
delay(500);
Serial.flush();
}
I am working with arduino duemilanove and some serial communication. What happens is, lets say arduino is suppose to send "abc123" to my pc as part of serial communication, but it sends "abc1" "\n(new line)" "23". This happens less frequently, but I want full proof serial communication. I don't know why this happens, as it breaks the chain of communication.
I suspect it's not actually working as you think. I suspect what's actually happening is that your PC application is reading a message and then printing it with a trailing line feed. Sometimes, it reads the serial port while a message is being received and gets a partial message. The real problem in this case is that you are relying on the delay between messages to separate them. The proper solution is to detect the start and end of each message and buffer the received bytes until you have a complete message before processing it. This may mean changing your message format to make it possible to determine when each message starts and ends. If you're using an ASCII message format, this might be as simple as sending a newline character sequence at the end of each message and using that to detect the start and end of each received message.