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

lafar6502
Posts: 31
Joined: Fri Mar 27, 2020 8:26 pm

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

Postby lafar6502 » Thu Apr 23, 2020 9:33 pm

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

Sprite
Espressif staff
Espressif staff
Posts: 10596
Joined: Thu Nov 26, 2015 4:08 am

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

Postby Sprite » Fri Apr 24, 2020 7:15 am

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.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 12 guests