7 years, 3 months ago.

The readable() function does not work once the serial port received more than one byte

The readable() function does not seem to work once the serial port received more than one byte. When I feed the Nucleo F757ZI single characters, it works like expected, but when I send a string of bytes, the readable() function always returns 0. I have also tested the code on the Nucleo F446RE board where it does work as expected: of course it skips some characters, but when I send new data, it is captured in the program. What could be going on and what could be done about this other than using interrupts (and hoping that they keep working when the program gets more complex)?

Used code

#include "mbed.h"
Serial pc(SERIAL_TX, SERIAL_RX);

int main(){
    pc.printf("Hello world!\n");
    while(1) {
        pc.printf("%d,", pc.readable());
        while (pc.readable()>0){
            pc.putc(pc.getc());
            wait_ms(2);
        } 
        wait_ms(10);  
    }
}

Question relating to:

STM32 Nucleo-144 development board with STM32F767ZIT6 MCU, supports Arduino, ST Zio and morpho connectivity

Suggest not print the pc.readable() eaery10ms. There will be too much "0," printed which makes it hard to know whether the pc.readbale detected any thing valueable. Try to print the pc.readable() only after it detected something. Then test again to see whether this API still works.

posted by Fred Li 05 Jan 2017

Of course I've tried it without printing the the pc.readable() first. This was just to see whether it would return 0, -1 or a larger number to account for more bytes in the buffer. In fact, printing 100x '0' a second is not so hard to inspect, I suggest you try it: anything other than '0' stands out clearly. So it gives one '1' for the string of bytes and then all '0' no matter what you send to it.

posted by Gerwin V 05 Jan 2017
Be the first to answer this question.