The below code works perfectly on my ubuntu desktop but fails on BBB ubuntu! In the code I’m reading from usb GPS device /dev/ttyUSB0. Loop breaks when device is unplugged and waits 90 seconds to reopen the device and start reading. On BBB when GPS device is unplugged CPU is 100% used
void main(){ opendevice: FILE *f = fopen("/dev/ttyUSBS0", "r"); c = (char) getc(f); while (c != EOF) { if (c == '@') { i = 0; c = getc(f); while (c != '#') { if (c == EOF) { break; } buf[i] = c; i++; c = getc(f); } buf[i] = '\0'; printf("%s\n",buf); } else if (c == -1) { break; } c = (char) getc(f); } sleep(90); goto opendevice; }
I’m speculating that getc function is not returning correct EOF character on device unplug.
Speculating? Isn’t this pretty easy to test?
I never debugged on beaglebone till 14hrs back. I had been debugging my code on ubuntu desktop and when it worked then I compiled and ran it on beaglebone. So I could only speculate whats happening on beaglebone! Now I debugged on beaglebone… Its producing very weird result! When I put watch on c and I unplug the device then c=255, so I replaced c!=EOF to c!=255 and debugged again, behold, watch on c is c=255 but c!=255 returns true! And the loop never returns! I posted a question regarding it at http://stackoverflow.com/questions/19838702/in-c-how-to-detect-eof-in-arm-architecture . I tried using fstream fin(“ttyUSB0”, fstream::in) and now when I unplug the device fin>>c never returns and CPU usage is 100% so there should have been an infinite loop inside fin! It might be a bug in the ARM build.