I am reading NMEA data from the usb input of an Intel Edison. Everything is working fine except for one problem. If I disconnect the RS232 to USB cable from the other two cables that lead to the bulkhead connector, I get a flood of characters sent to the software. They seem to be a large repetition of the last character sent before the cable was disconnected. I assume this is from some kind of interference introduced on the 10 inches or so of open cable. I would like to write some code so that the software can recover but as it is, I need to power cycle the box. Killing and restarting the software does not work. I have tried to just read and dump all the characters coming in until I get a valid pair but this does not work. Here is what I have tried:
while(1){
while (new_nmea_read_sentence == 0){
while(read(tty_fd, &aa, 1) == -1); // read 1 character from stream (blocking call)
if(aa != '\n') {
local_buffer[idx++] = aa;
if(idx > 400){
idx = 0;// prevent segmentation faults
printf("\nlocal_buffer overflow in read nmea.\n");
int flag = 1;
char old = 'x';
while (flag == 1){
read(tty_fd, &aa, 1);
if (aa == '\n' && old == '\r') flag = 0;
//printf("<%c>",aa);
old = aa;
}
}
}
else{// deal with valid nmea sentence
This thread is a forever loop reading the data. Other threads deal with it and they pass a volatile flag back and fourth. When this problem happens, the first thing I see is an overflow and I trap that in the code if(idx > 400). Without that I get a segmentation fault. The code below while (flag == 1) is looking for a which I would get if I got any valid data. If the input is an endless string of characters, I continue to get characters even after reconnecting the cable. Restarting the software produces no input.
My only solution is to tell the user to power cycle the box. Not very nice.