Fixed length uart frame detect on Zephyr

Hello. 

I've got problem with detect uart frame received from another device. This device send me frames which has  fixed lenght (20B) and it always starts with 2B header ("**").

I used UART implementation with one static buffer. 

static int uart0_init(void)
{
	int err;

	if (!device_is_ready(uart_central)) 
	{
		LOG_ERR("UART0 device not ready");
		return -ENODEV;
	}

	err = uart_callback_set(uart_central, uart0_cb, NULL);
	if (err) 
	{
		return err;
	}

	return uart_rx_enable(uart_central, uart_central_rx_buff, sizeof(uart_central_rx_buff), SYS_FOREVER_US);
}

static void uart0_cb(const struct device *dev, struct uart_event *evt, void *user_data)
{
	ARG_UNUSED(dev);

	switch (evt->type) {
	case UART_TX_DONE:
		break;

	case UART_RX_RDY:
		break;

	case UART_RX_DISABLED:
		uart_rx_enable(uart_central, uart_central_rx_buff, sizeof(uart_central_rx_buff), UART_RX_TIMEOUT);
		break;

	case UART_RX_BUF_REQUEST:
		break;

	case UART_RX_BUF_RELEASED:
		break;

	case UART_TX_ABORTED:
		break;

	default:
		break;
	}
}

I tried to use UART_RX_RDY event type to detect frame header but it doesn't work well when UART_RX_BUF_REQUEST event occur and the buffer is fill from beggining and there are extra UART_RX_RDY events. I can make bigger buffer than frame which I'll receive from second device and clear buffer offset after receive frame but this solution won't be good if I receive some trash data or frame will have incorrect length. It could be hours between correct frames and I'm afraid that there could be some incorrect data. How to recognize corrrect frame in this situation. There would be no problem if there was an event after one byte receive. 

My device is build on nRF52840.

Regards,

P_WE

Parents
  • Hi,

    It is not entierly clear to me what you receive and what you expect to receive? Can you show some examples and explain in more detail?

    Generally though, if you are looking for a specific pattern, I would consider keeping a buffer where you store incoming data and search for the full "packet" there, so that it does not matter if you first receive a part of it, and subsequently the last part.

Reply
  • Hi,

    It is not entierly clear to me what you receive and what you expect to receive? Can you show some examples and explain in more detail?

    Generally though, if you are looking for a specific pattern, I would consider keeping a buffer where you store incoming data and search for the full "packet" there, so that it does not matter if you first receive a part of it, and subsequently the last part.

Children
Related