I’m trying to record incoming data on a serial port from a GPS receiver to a file on an NXP LPC1768 board. For context, the GPS sends 4-6 lines of text (NMEA) every second, each around 30-75 characters long. In other words, I can expect to receive up to 450 characters per second, arriving in short bursts. The baud rate of 9600 can’t be changed easily.
When using UnbufferedSerial the program prints and records data without crashing, but it very often loses characters. The output is incomplete and invalid. There seems to be a pattern to it, since it only loses the end of some of the lines, never the beginning. It’s as if the program can’t keep up, even when commenting out fprintf()
and doing nothing more than GPS.read()
and printf()
.
When using BufferedSerial all the data is received and printed to the terminal, as long as fprintf()
is commented out. As soon as fprintf()
is introduced, the program crashes shortly or immediately after receiving data. The error code is 0x80FF013D (System is out of memory). Considering how little memory the program allocates, this seems odd. Figured it might refer to the serial buffer usage, but increasing drivers.uart-serial-rxbuf-size
to 512 or even 1024 doesn’t help.
Any ideas how to resolve this issue? What am I doing wrong?
#include "mbed.h"
BufferedSerial GPS(p13, p14, 9600);
LocalFileSystem local("local");
FILE* fd;
int main()
{
fd = fopen("local/log.txt", "w");
if (fd==NULL) {
printf("fopen() failed\n");
}
else
{
char input;
while (1)
{
GPS.read(&input, 1);
printf("%c", input);
// Program crashes when fprintf is introduced while using BufferedSerial.
// Error code 0x80FF013D (System is out of memory)
fprintf(fd, "%c", input);
if (input == '\n')
{
// Closing and re-opening on every line-break, to save the
// data and make it available to the computer
fclose(fd);
fd = fopen("local/log.txt", "a");
if (fd == NULL)
{
printf("Re-opening failed\n");
break;
}
}
}
}
}
Error message:
++ MbedOS Fault Handler ++
FaultType: HardFault
Context:
R 0: 00000009
R 1: 100027AC
R 2: 10002B28
R 3: 00000001
R 4: 00000009
R 5: 100027AC
R 6: 00000000
R 7: 80000000
R 8: 00000000
R 9: 00000000
R 10: 00000000
R 11: 00000000
R 12: 00000E65
SP : 100027A8
LR : 000021F5
PC : 00003360
xPSR : 01000000
PSP : 10002788
MSP : 10007FA0
CPUID: 412FC230
HFSR : 80000000
MMFSR: 00000000
BFSR : 00000000
UFSR : 00000000
DFSR : 0000000A
AFSR : 00000000
Mode : Thread
Priv : Privileged
Stack: PSP
-- MbedOS Fault Handler --
++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x3360
Error Value: 0x100028D0
Current Thread: main Id: 0x100011A4 Entry: 0x1C25 StackSize: 0x1000 StackMem: 0x100018D0 SP: 0x100027A8
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=LPC1768
-- MbedOS Error Info --