Read Uart based sensor data on nrf5340 using VsCode?

Hi,

I'am using the  Ultrasonic Oxygen Senor with the nrf5340, the output is like this "photo" I want to calculate the concentration and the purity and with this values I had wrong results, so can you help to solve this problem?

 I am using this code

4857.gasboard.zip

 and here is my result.


this technical specification for my sensor.

 https://en.gassensor.com.cn/Product_files/Specifications/Gasboard%207500H%20series%20technical%20specification.pdf?fbclid=IwAR0R1P0FlwkPzhew7jBYVzL-rvtESB1ob2HGF0OE4bngaI5jP8ktZfFMoMo 

Could you please help me to solve this problem

  • Yes, I use %x instead of %i but always the result is "0".

    The file Proj. Conf:

    CONFIG_UART_INTERRUPT_DRIVEN=y

    Is there something missing?

  • Since you are getting the value 16 sometimes, which seems to be the expected beginning of a message according to the data sheet, I suspect that you are actually able to read the UART, and that the problem is related to how you process the data.

    For example, can you explain the purpose of these lines in your code:

    uart_buf[data_length] = 0;

    uart_fifo_fill(x, uart_buf, data_length);

    count=uart_fifo_read(uart,uart_buf,sizeof(uart_buf));

    I can't see anything similar in the Arduino code, and I'm not sure what these lines are supposed to do.

    Also, the Arduino code waits before reading, maybe you also have to do this for it to work:

    //When character arrive over the serial port ..
    if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while (Serial.available() > 0) {
      inByte = Serial.read();

    -Einar

  • Hello,

    Now the code read correctly, 

    just now I need to translate this instraction to finish the code.

    inByte = Serial. read();

    So could you help me to translate it with zephyr language

  • Hi

    Would you mind sharing what solved your issue, in case others have similar problems?

    And I believe Serial.read() can be exchanged with uart_fifo_read()

  • Hi ,

    #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/uart.h>
    #include <sys/printk.h>
    uint8_t uart_buf[12],i,j;
    uint16_t o2c,o2f,o2t;
    uint8_t temp;
    int count;
    	
     void uart_cb(const struct device *x, void *user_data)
     {
     	// uart_update(x);
         int data_length = 0;
    
     	if (uart_irq_rx_ready(x)) {
     		data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
     		// uart_buf[data_length] = 0;
     	}
         // uart_fifo_fill(x, uart_buf, data_length);
     }
    
    void main(void)
    {
        printk("sensor program start!\n"); //output on UART0, which is default for logging
    	  struct uart_config cfg ;
        cfg.baudrate = 9600 ;
        cfg.parity = UART_CFG_PARITY_NONE ;
        cfg.stop_bits = UART_CFG_STOP_BITS_1 ;
        cfg.data_bits = UART_CFG_DATA_BITS_8 ;
        cfg.flow_ctrl = UART_CFG_FLOW_CTRL_NONE ;
    
        const struct device *uart = device_get_binding("UART_1");	
      while(1){
          uart_configure(uart, &cfg);
        
          uart_rx_enable(uart,uart_buf,sizeof(uart_buf),500000);
           uart_callback_set(uart, uart_cb,NULL);
           printk("main function!\n");//output on UART0, which is default for logging
     	
         k_sleep(K_MSEC(1000));
    
    
            // for(int i=0;i<sizeof(uart_buf);i++){
            // printk("data%i= %x \n",i,uart_buf[i]);}
    
            printk("*******************************\n");
            k_sleep(K_MSEC(1000));
    
                o2c = uart_buf[3] * 256 + uart_buf[4];     //Oxygen concentration
                o2f = uart_buf[5] * 256 + uart_buf[6];     //Oxygen flow value
                o2t = uart_buf[7] * 256 + uart_buf[8];     //Oxygen temperature
                float o2c1;
                o2c1=(float)o2c/10;
                float o2f1;
                o2f1=(float)o2f/10;
                float o2t1;
                o2t1=(float)o2t/10;
                printf("O2 : %.2f %\n",o2c1);
                printf("flow : %.2f L/min\n",o2f1);
    
                printf("temp: %.2f °C\n",o2t1);
    
    }	}
    

    proj.conf

    CONFIG_UART_ASYNC_API=y
    CONFIG_UART_WIDE_DATA=y
    CONFIG_UART_USE_RUNTIME_CONFIGURE=y
    CONFIG_UART_LINE_CTRL=y
    CONFIG_UART_1_ASYNC=y
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    CONFIG_NRFX_UARTE1=y

Related