hey folks, i've got a sketch that records all sorts of data from CANBus, GPS, and some other sensors. All of the data is being written to a MicroSD card via the SdFat library.
I'm trying to read the data off of the SD card via the Serial port connected to a computer over USB.
I have my serial port set to 115200bps, but i'm having the same problem (though less often) even when i set the serial port to 9600bps.
Basically, i setup a simple protocol where i say "G " (obviously is replaced by the name of the file, and the format of the filename is something like "00023-LG.CSV".
My sketch code looks like this:
void setup() {
#define MAX_BUFSIZE 25
char *buf_ptr;
char buffer[MAX_BUFSIZE];
...
Serial.begin ( 115200 );
}
void loop() {
...
buf_ptr = buffer;
if ( Serial.available() > 0 ) {
while ( Serial.available() ) {
*buf_ptr = Serial.read();
//Serial.print ( "read: " );
//Serial.println ( *buf_ptr );
if ( ( *buf_ptr == '\r' ) || ( *buf_ptr == '\n' ) ) {
*buf_ptr = NULL;
}
buf_ptr++;
}
*buf_ptr = NULL;
Serial.print ( "COMMAND: " );
Serial.println ( buffer );
// more processing here...
}
}
I commented out the Serial.print() and Serial.println() for every character read since i was concerned that affecting the issue. It may have some bearing on it, but i still see the problem when the code is commented out.
The problem i see is that when i print out buffer at the end of reading data, i always get the first character, and sometimes there are missing characters thereafter. However the last character is almost always present.
So for example, if i do "G 00023-LG.CSV", sometimes buffer will be correct at "G 00023-LG.CSV", but often times it will be "G SV", or "G 00V", or "G "
Originally, i thought it was that i was trying to read the data off of the serial port too quickly on the arduino, however, since most often the last character is in the buffer, i don't think that's the case.
Any ideas? Thanks!