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

nRF51822 - Using UART to communicate with a sensor (app_uart) - locks up if sensor is bad - how to implement a timeout or prevent lock up?

Hello, I am using a nRF51822 chip to communicate with a sensor. The sensor continuously outputs its reading via UART every couple of seconds. This is what I am currently doing which works perfectly...... unless the sensor is bad, or not installed. Either of these functions will prevent the firmware from continuing to run, and essentially locks everything up.

I am using this to initialize the UART:

void UART_init( uint8_t pin_rx, uint8_t pin_tx )
{
	uint32_t err_code;
    const app_uart_comm_params_t comm_params =
      {
          pin_tx,  //nRF51 RX
          pin_rx,  //nRF51 TX
          2,  //not used
          3,  //not used
          APP_UART_FLOW_CONTROL_DISABLED,
          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);
}

And I use this to read the UART data:

void Read_string( char *resultPointer )
{
	uint8_t charResponse;
	char cToStr[2];
	cToStr[1] = '\0';
	
	for(int i = 0; i < RX_BUF_SIZE; i++)
	{
		while(app_uart_get(&charResponse) != NRF_SUCCESS);
		cToStr[0] = charResponse;
		strcat(resultPointer,cToStr);
		
		if(charResponse == '\n'){ break; }
	}
	
	return;
}

Do you know of a good way, such as if I don't get a reading after X amount of time, then to just exit and try later? Or is there a better way of handling this? There are other firmware functions that I would like to continue running even if the sensor goes bad.

Thanks and any help or advice is greatly appreciated!

Related