Inherit from Serial and use software buffers for TX and RX. This allows the UART peripherals to operate in a IRQ driven mode. Overrides most (but not all) stdio functions as Serial did

Dependencies:   Buffer

Dependents:   buffered_serial_test BLE_Police_HRM evena_BLE_Police_HRM df-2014-workshop-rfid-case-generator-k64f ... more

Issue: readable() does not work

I am using a PSoC 6 WIFI-BY Pioneer kit Short version: the below code does not work.

static BufferedSerial pc( USBTX, USBRX);

DigitalOut pc_activity(LED1);

// Blinking rate in milliseconds
#define BLINKING_RATE     200ms

int main()
{
    printf( "start\n" );
    fflush( stdout );

    while (1)
    {
        print( "." );
        if( pc.readable() )
        {
            printf( " XXX " );
            pc_activity = !pc_activity;
        }
    }
}

I tried:

pc.set_blocking( false );
if( pc.read() > 0
if( pc.read() != (-EAGAIN) )

In fact, you can give it input, and the returning count is always == (-EAGAIN)

       int count = pc.read( pcbuf, 1 );
        printf( "[%04x]", count );
        printf( "[%04x]", (-EAGAIN) );

without joy.

3 comments:

08 Sep 2020

Basically, pc.read() looks at the number of bytes in the RX FIFO - which is always == 0 It looks like the RX interrupts are disabled/blocked. example code files attached

14 Sep 2020

Are you are using the version of BufferedSerial that is part of Mbed OS and not this library? If so, its probably best to report the issue on github (https://github.com/armmbed/mbed-os) where the development of the latest version of this library takes place. If I've misunderstood what you are doing, please publish an example program that demonstrates the failure with this library.

26 Nov 2021 This post is awaiting moderation

Hey Sam, I am trying to use .readable() with an .attach interrupt following the instructions in this URL: https://forums.mbed.com/t/interrupt-driven-serial-required-in-os6/10041/4. However when I run the program and it enters the interrupt it never stops printing to the console. Also it doesn't do what's inside the pc.readable(). Do you think is some kind of problem with the library? Here's my code for a Nucleo F446RE:

 #include "mbed.h"
#include "BufSerial.h"

BufSerial pc(USBTX, USBRX);

void onSerialRxNucleo() {
    while(pc.readable()){
pc.putc('e');}
pc.printf("Interrupt\n");
 }

int main()
{
    pc.baud(9600);
    pc.format(8,SerialBase::None,1);
    //Serial interrupts when data is received in Rx 
    pc.attach(&onSerialRxNucleo,RawSerial::RxIrq);
    while (1) {
        /*Timer   s;

        s.start();
        pc.printf("Hello World - buff\n");

        int buffered_time = s.read_us();
        ThisThread::sleep_for(100ms);  // give time for the buffer to empty
        s.reset();
        printf("Hello World - poll\n");

        int polled_time = s.read_us();
        s.stop();
        ThisThread::sleep_for(100ms);  // give time for the buffer to empty
        pc.printf("printf buffered took %d us\n", buffered_time);
        pc.printf("printf polled took %d us\n", polled_time);
        ThisThread::sleep_for(500ms);*/
    }
}

And what I get from printing in the console is:

uIrtr
eprttuepnttuIpnr
uIrtr
eIrtt
epnttuIpnrtuIrnr
eIrtt
eprttuepnrtuIrnr
uIrtr
eprttuepnttuIpnr
uIrtr
eIrtt
epnttuepnrtuIrnr
eIrtr
eprttuepnrtuIpnr
uIrtr