BufferedSerial Example : question about the num = serial_port.read

About the Example of BufferedSerial
if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
// Toggle the LED.
led = !led;

        // Echo the input back to the terminal.
        serial_port.write(buf, num);
    }

Does the num always = 1?
I am confused about the return of the read function.
I try to change the MAXIMUM_BUFFER_SIZE to 1
and send multiple chars from the terminal, I still can get multiple chars.

Hello Tzu-Hsuan,

According to the related Mbed documentation the BufferedSerial::read(void* buffer, size_t length) function returns the number of bytes actually read. If end of file is read then it returns 0 and on error a negative number. The length parameter (num in your example) tells the function the maximum number of bytes your program is capable to read in one shot. So if more bytes are send to your program then several read's are needed to get all of them. You can have a look also at this example.

Best regards, Zoltan

Thank you Zoltan , your reply always useful.

This is my test code, when I use a terminal program send “abcd”
why I get the below result?

image

#include "mbed.h"

// Maximum number of element the application buffer can contain
#define MAXIMUM_BUFFER_SIZE 32

// Create a DigitalOutput object to toggle an LED whenever data is received.
static DigitalOut led(LED1);

// Create a BufferedSerial object with a default baud rate.
static BufferedSerial serial_port(USBTX, USBRX);

int main(void)
{
// Set desired properties (9600-8-N-1).
serial_port.set_baud(9600);
serial_port.set_format(
/* bits / 8,
/
parity / BufferedSerial::None,
/
stop bit */ 1
);

// Application buffer to receive the data
char buf[MAXIMUM_BUFFER_SIZE] = {0};
char bufnum[1] = {0};

int count=0;
while (1) {
if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
// Toggle the LED.
led = !led;
bufnum[0]=num;
// Echo the input back to the terminal.
serial_port.write(buf, num);
//serial_port.write(bufnum, sizeof(bufnum));
printf(“num:%d\r\n”,num);
}

}

}

Hello Tzu-Hsuan,

This is my test code, when I use a terminal program send “abcd”
why I get the below result?

I think it’s because when serial_port.read(buf, sizeof(buf)) was called the first time only the character “a” was available in the BufferedSerial’s internal buffer. Hence, “a” was put into the buf and num was set to 1. Consequently, the serial_port.write(buf, num); sent an “a” character back to the serial terminal and the printf(“num:%d\r\n”,num) sent “num:1” followed by a carriage return “\r” and a new line “\n” characters. So the serial monitor on the PC displayed anum:1 and moved to the first position on the new line (as the result of receiving a \r\n sequence).
When serial_port.read(buf, sizeof(buf)) was called again, “bcd” was available in the BufferedSerial’s internal buffer. Consequently, the serial_port.write(buf, num) sent “bcd” back to the serial terminal and the printf(“num:%d\r\n”,num) sent “num:3” followed by a carriage return “\r” and a new line “\n” characters. So the serial monitor on the PC should display bcdnum:3. I’m sorry, but I don’t know why was the “n” character missing and only bcdum:3 was diplayed instead. It could happen due to an unreliable transmission.

Best regards, Zoltan