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

UART skipping characters?

Dear Community;

I am trying to read serial data sent by a PMS 5003 in hexadecimal. If I connect the PMS 5003 to a conventional serial device (Raspberry) this is what I read:

42 4d 00 1c 00 07 00 0a 00 0a 00 07 00 0a 00 0a 04 38 01 59 00 30 00 00 00 00 00 00 97 00 02 3e

However, using nRF 9160 DK I read this:

42 4d 00 1c 00 07 00 0a

This is my code. I would like to find out what's wrong.

/*

* Copyright (c) 2012-2014 Wind River Systems, Inc.

*

* SPDX-License-Identifier: Apache-2.0

*/

#include <zephyr.h>
#include <misc/printk.h>
#include <uart.h>
#include <nrf9160.h>

unsigned char uart_buf[1024];

void uart_cb(struct device *x)
{
    static int counter = 0;
	uart_irq_update(x);  //Start processing interrupts in ISR
	int data_length = 0;

	if (uart_irq_rx_ready(x)) {  //Check if UART RX buffer has a received char
		data_length = uart_fifo_read(x, uart_buf, 1024);  //Read data from FIFO
		//printk("bytes: %i", data_length);
		//printk("rx Ready!\n");
		uart_buf[data_length] = 0;
	}
    
    printf("PMS5003 hexadecimal data: %02X\n", (unsigned char) uart_buf[0]);
    counter++;

}

void main(void)
{
	printk("Starting...\n");
	struct device *uart = device_get_binding("UART_0"); //Conecting
	uart_irq_callback_set(uart, uart_cb);  //Set the IRQ callback function pointer
	uart_irq_rx_enable(uart);  //Enable RX interrupt

	printk("UART loopback start!\n");
	while (1) {
		k_cpu_idle(); //Make the CPU idle (useless)
	}
}

Thanks in advance,

Ander.

Parents Reply Children
Related