Page 1 of 1

UART Api - reading single byte, reading single line, peeking into the driver buffer

Posted: Thu Apr 23, 2020 9:33 pm
by lafar6502
Hi, i'd like to user the uart driver APIs to read text from serial port, line by line. So i need an api function to look inside the receive buffer and check if a line end character is in the buffer. But it seems we only have

Code: Select all

uart_read_bytes
function that reads a fixed number of bytes regardless of the buffer content. So to use it for reading line by line i would have to use uart_read_bytes to read single characters in a loop, until line break is found.
Of course I could do that but it appears quite inefficient as uart_read_bytes is quite complex function that's not optimized for single byte reading. So, question to you - is there anything i could use for efficient line by line uart processing?

And btw i dont want to implement another level of buffering in my code, i'd prefer to use standard UART APIs and its internal buffer.
Thanks
R

Re: UART Api - reading single byte, reading single line, peeking into the driver buffer

Posted: Fri Apr 24, 2020 7:15 am
by Sprite
According to here, you can call esp_vfs_dev_uart_use_driver() and after that all the standard C IO functions should work as expected. That should allow you to just do fgets(buffer, buffer_length, stdin) to read a line.