This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to check the data available in serial

Hi...

 i want to read the data from another board using serial uart .. how to write the command to check the data is available ... in arduino i written like this how to write in this case

how to achieve the same thing using nRF52840

void loop() { //Choose Serial1 or Serial2 as required
  if(Serial2.available()) 
  {
    Serial1.print(char(Serial2.read()));
  }
     else if (Serial1.available()) 
     {
    Serial2.print(char(Serial1.read()));
  
}
}

Parents
  • Hi 

    If you use the nrf_serial library you can read data in a very similar fashion using the nrf_serial_read function:

    /**
     * @brief Function for reading from a serial port.
     *
     * @param p_serial   Serial port instance.
     * @param p_data     Receive buffer pointer.
     * @param size       Receive buffer size.
     * @param p_read     Amount of data actually read from the serial port.
     *                   NULL pointer can be passed.
     * @param timeout_ms Operation timeout, in milliseconds. Pass 0 to operate in
     *                   non blocking mode.
     *
     * @return Standard error code.
     * */
    ret_code_t nrf_serial_read(nrf_serial_t const * p_serial,
                               void * p_data,
                               size_t size,
                               size_t * p_read,
                               uint32_t timeout_ms);

    The p_read parameter tells you how many bytes were read, and the timeout_ms parameter allows you to tell the function how long to wait for new data (you can set it to 0 to only read the data currently in the buffer). 

    Best regards
    Torbjørn

  • Is there a way to check how many bytes are available to read, like Serial.available() does in Arduino?

    When I use nrf_serial_read, I have to specify how many bytes it should read. How would I proceed if I want just everything that's available, without knowing how much exactly?

  • Hi

    There is no function exactly like the one in Arduino unfortunately. 

    If you scale your buffer to the internal buffer in the library then you should be able to read everything in one go, assuming you have the RAM to spare. 

    If you use a smaller buffer and see that the size of bytes read is the same as the number of bytes you provided as the maximum you can assume that there might be some more data waiting, and that you have to run the read function again. 

    Best regards
    Torbjørn

Reply
  • Hi

    There is no function exactly like the one in Arduino unfortunately. 

    If you scale your buffer to the internal buffer in the library then you should be able to read everything in one go, assuming you have the RAM to spare. 

    If you use a smaller buffer and see that the size of bytes read is the same as the number of bytes you provided as the maximum you can assume that there might be some more data waiting, and that you have to run the read function again. 

    Best regards
    Torbjørn

Children
No Data
Related