7 years, 4 months ago.

I have a problem wit serial.readable()

Hi!

I try to read data from a GPS board (https://www.adafruit.com/product/746) using the serial link of my mbed. This works not very well, even if a good library has been developped for mbed.

The problem is my GPS board can be turned-on or turned-off. To detect its state, I use the function serial.readable(). This works sometimes and sometimes not.

For instance, when the GPS board is turned-off, serial.readable() returns me 0. When I switch-on the GPS board, serial.readable() returns me 1. But when I come back to the GPS state turn-off, serial.readable() keep returning me 1. I have tried to clear the serial buffer (using this technique https://developer.mbed.org/forum/bugs-suggestions/topic/1037/?page=1#comment-20144), but this did not work. An idea of how I could proceed ?

Thanks.

The function I add in Adafruit Ultimate GPS Breakout v3 mbed library / serialGPS.ccp


int SerialGPS::enable()
{
    LPC_UART0->FCR |= 0x06; // clear serial buffer
    if(_gps.readable()) {
        return 1;
    } else {
        return 0;
    }

and the library I use : https://developer.mbed.org/components/Adafruit-Ultimate-GPS-Breakout-v3/

1 Answer

7 years, 4 months ago.

The serial port has no connection state and so it cannot checked by the readable() method. When readable() is true, it means there is a character in the buffer and this will remain until you read it. Clearing some hardwarebits is not nice, that will not clear the software buffer. Clearing the buffer could be done by a dummy read loop, but when the device is sending continous and fast this will fail also.

  while(_gps.readable())
    _gps.getch();

I would suggest to connect the power pin of the GPS module to a DigitalIn on the mbec mcu, then you can check the state simply by reading the input state.