How do I wait for data to be pushed to the RX buffer of the UART channel?

Hello,

I am trying to implement a system where the board waits for a character to be pushed to the RX buffer before reading it. The character would be sent by another microcontroller and used to set  a filter. That part works as expected when the character is already on the buffer or hardcoded into the software, but obviously this is not sufficient for the final product. What I am looking for is away to detect if there is anything on the buffer ready to be read. This would be similar to the Serial.available() function on Arduinos. 

Parents
  • Hi,

    You can use the function app_uart_get() to check if there is available data. This function will return NRF_ERROR_NOT_FOUND if there is no data available. Note that this function will pop the data from the RX buffer if it is available. If you need to know if there is data available without popping the data, you can set a flag in the event handler when the APP_UART_DATA_READY event is generated, and clear the flag when you empty the buffer (calling app_uart_get() until you get NRF_ERROR_NOT_FOUND). You can then check this flag to know if there is data available.

    If you are using the UART driver directly, you can also use the function nrf_drv_uart_rx_ready() if you are using the driver in Blocking mode.

    Best regards,
    Jørgen

Reply
  • Hi,

    You can use the function app_uart_get() to check if there is available data. This function will return NRF_ERROR_NOT_FOUND if there is no data available. Note that this function will pop the data from the RX buffer if it is available. If you need to know if there is data available without popping the data, you can set a flag in the event handler when the APP_UART_DATA_READY event is generated, and clear the flag when you empty the buffer (calling app_uart_get() until you get NRF_ERROR_NOT_FOUND). You can then check this flag to know if there is data available.

    If you are using the UART driver directly, you can also use the function nrf_drv_uart_rx_ready() if you are using the driver in Blocking mode.

    Best regards,
    Jørgen

Children
Related