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

nrf9160 uart strategy

Hi there,

what is recommended strategy to read/write data on uart? 

Taking from sample, read byte-by-byte, on 115k, sometimes data are truncated.

Is there a possibility to read buffer at once? 

Or, what is recommended from Nordic?

Thank you!

------------------- data -----------------

{"evt":,"dev_i{"evt":"dev_id{"evt":,"dev_i{"evt":C9EF2" <<--- this is what is received on uart, json is expected.

------------------------  the irq function ------ 

static void uart_callback(struct device *x)
{
	static u16_t rx_len = 0;
	static u8_t rx_buf[256];
	u8_t rx_byte;
	struct net_buf *nbuf;

	while (uart_irq_update(uart) && uart_irq_is_pending(uart)) {
		if (!uart_irq_rx_ready(uart))
			return;

		while (uart_fifo_read(uart, &rx_byte, 1)) {
			if (rx_byte != '\n') {
				rx_buf[rx_len] = rx_byte;
				// stop growing the message once max length is reached
				rx_len += (rx_len < MESH_MSG_MAX_LEN ? 1 : 0);
				continue;
			}

			if (0 == rx_len)
				continue;

			rx_buf[rx_len++]=0;

			printk("Received from UART: %s\n", rx_buf);

			nbuf = net_buf_alloc(&m_buf_pool_uart_to_mqtt, K_NO_WAIT);
			if (nbuf) {
				net_buf_add_mem(nbuf, rx_buf, rx_len);
				net_buf_put(&m_queue_to_mqtt, nbuf);
			} else {
				printk("No more buffers to MQTT are available!\n");
			}

			nbuf = NULL;
			rx_len=0;
		}
	}
}

Related