I had a FTDI breakout board as a serial receiver.
An arduino board transmitter sends a line(at 9600 baud) to the receiver in 3 parts every 3 seconds.
"T= 19.00 z= 43984 N= 4889 S= 4764 e= -51 w= 5.88....z= -25065 E= 4141 W= 4261 e= 8 w= -3.35.....Aw= 6 Dw= 29"
So the transmit line is high most of the time.
3 times in each line some characters are sent - the last characters are CR LF to start on a new line.
This works fine for weeks with millions of characters received without a single error. (I have other code checking the data)
I then wanted to use an arduino board as the receiver so using digital 2 as the receive line, I used the following code:
#include <NewSoftSerial.h>
NewSoftSerial mySerial(2, 3);
void setup()
{
Serial.begin(9600);
// set the data rate for the NewSoftSerial port
mySerial.begin(9600);
}void loop() // run over and over again
{
if (mySerial.available()) //send each character as its received
Serial.print((char)mySerial.read());
}
Now at random times I get a received character error. Very random it can be 1 in a 100 or 1 in 1000.
The error always occurs at the end of one of the three groups of characters.
"T= 19.0? z= 43984 N= 4889 S= 4764 e= -51 w= 5.88....z= -25065 E= 4141 W= 4261 e= 8 w= -3.35.....Aw= 6 Dw= 29"
OR
"T= 19.00 z= 43984 N= 4889 S= 4764 e= -51 w= 5.8?....z= -25065 E= 4141 W= 4261 e= 8 w= -3.35.....Aw= 6 Dw= 29"
Note a ? has appeared instead of a numerial which would be the last character in a group.
It sure looked like a timing problem. The interupt is enabled soon after the last character has been received.
Maybe we are catching the stop bit transition back to a high(PCINT18).
This would give an apparent start edge and the data would be all 1's ie 0x7f(? is 0x3f - its close)
I notice that the examples use 4800 baud maybe 9600 is too fast.
I tried a fix. At 9600 baud the individual pulse width is 0.104 ms(1.04 ms for start, 8 data & stop). So I slip in a 5 ms delay this would well and truly get pass the stop bit(I tried smaller delays but no joy):
void loop() // run over and over again
{
if (mySerial.available()) //send each character as its received
{
delay(5); //wait to empty buffer
char t = (char)mySerial.read();
Serial.print(t);
}//end of print character
}
It seems to be working sofar.
As the error is alway ? and q does not occur anywhere else I could replace "?" with "0". I'm not happy with that fix.
I am not happy with my explaination or the fix's.
How about some ideas?