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

get data from uart receive buffer

hello, there my application is like that...

(1) calculate size of receive buffer. than read that size of data. again new data comes, then again calculate that size of receive buffer and read that size of data. for that i have to clear buffer after reading data.

can anyone tell me how to do it??

i have tried this way. but its not working for me. i am using nordic sdk 12. i am using uart peripheral code.

from receiver data is coming in different packets at 1s interval. sometimes size is 8 byte or 1D byte. i have to calculate length.

can you tell me how to do it??

void uart_init()
{

    uint32_t err_code;
    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          APP_UART_FLOW_CONTROL_ENABLED,
          false,
          UART_BAUDRATE_BAUDRATE_Baud9600
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOW,
                         err_code);

    APP_ERROR_CHECK(err_code);

}


void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
    if (p_event->evt_type == APP_UART_DATA_READY)
   {  
		
		 count1 = count1+1;
		 count = count1;


   }
}

int uart_return()
{
   
return count;

}

int main()
{
  uart_init();


while(1)
{

 while(uart_return() > _len)
		{
			_len = uart_return();
			SEGGER_RTT_printf(0, "task_length:%x\r\n",_len);	
			nrf_delay_us(_t35);
		}
    
	if(_len == 0) return;
   uint8_t i;

   _frame = (uint8_t*) malloc(_len);

		for (i=0 ; i < _len ; i++) 
	{
	app_uart_get(&_frame[i]);


	}
}

}
  • Hey nordicbeginner,
    I've made some modification to your event handler:

        void uart_error_handle(app_uart_evt_t * p_event)
        {
            static uint8_t data_array[CHOOSE_YOUR_SIZE];
            static uint8_t index = 0;
            uint32_t       err_code;
            
            if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
            {
                APP_ERROR_HANDLER(p_event->data.error_communication);
            }
            else if (p_event->evt_type == APP_UART_FIFO_ERROR)
            {
                APP_ERROR_HANDLER(p_event->data.error_code);
            }
            if (p_event->evt_type == APP_UART_DATA_READY)
            {  
               err_code = app_uart_get(&data_array[index]);
               index++;
               /* TODO: Substitute '\n' in the statement below with the Modbus escape character, if 
                   applicable */
               if ((data_array[index - 1] == '\n') || (index >= (CHOOSE_YOUR_SIZE)))
               {
                    uart_callback(&data_array[0], index);
    
                    if (err_code != NRF_ERROR_INVALID_STATE)
                    {
                        APP_ERROR_CHECK(err_code);
                    }
    
                    index = 0;
                }
    
           }
        }
    
        void uart_callback(uint8_t * p_data, uint8_t length)
        {
            // TODO: Process 'data_array' of size 'length'.
        }
    

    Cheers,
    Håkon H

Related