Hello!
Could anybody help me please with the problem of uart data recieve at arduino nano.
The problem is - I've connect GPS (for example) that send GPGGA data to soft serial of arduino nano every 1 second,
then I try to find the end of the string of gps data (search for "CR") and place it to char array.
And finnaly print it to serial monitor.
But I place a delay for 7 second between every print to monitor.
So if there is a delay between of them I recieve at serial monitor sometimes two sticked GPGGA strings.
Could anybody please help me to understand why is it happend?
My code is (I simplified my code for this example.):
#include <SoftwareSerial.h>
#define RXD 2
#define TXD 3
SoftwareSerial soft_serial(RXD, TXD);
unsigned char serial_buffer[100];
void setup(){
Serial.begin(9600);
soft_serial.begin(9600);
}
void loop(){
if (Read_UART(soft_serial.read(), serial_buffer, 100) >0) {
Serial.write(serial_buffer,100);
Serial.println();
memset(serial_buffer,0,100);
delay(7000);
}
}
int Read_UART(int source, unsigned char *buffer, unsigned int len)
{
static unsigned int pos = 0;
unsigned int rpos;
if (source > 0) {
switch (source) {
case '\n':
break;
case '\r':
rpos = pos;
pos = 0;
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = source;
buffer[pos] = 0;
}
}
}
return -1;
}