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

Reading UART input in CLI command

I have the CLI module over UART enabled, with one command that continuously prints out the readings from a sensor. What I'm trying to do is stopping this continuous printout if the user types CTRL+C. The sample code is below:

static void sensor_printout_cmd(nrf_cli_t const* p_cli, size_t argc, char** argv)

{

while(rx != ASCII_FOR_CTRL_C) /* rx is from UART, check here if byte read is CTRL+C */

{

uint8_t buf[3];

read_from_sensor(buf);

nrf_cli_fprintf(...); /* print sensor readings */

nrf_drv_uart_rx(&cli_uart_transport_uart, &rx, 1U); /* read character from UART */

}

}

The problem is that while I'm able to read properly from the UART with the nrf_drv_uart_rx() function, CLI over UART is unable to retrieve any more characters and locks the CLI. After stepping through the code, it seems like the CLI queue for UART gets messed up somehow because of the call to the nrf_drv_uart_rx() function.

So the question is: When using CLI with UART, is there a way to reliably read UART inputs while inside CLI commands?

Parents
  • You the uart handle to get the RX data is the best way in your case.

    Like this

    void uart_event_handle(app_uart_evt_t * p_event)
    {
    static uint8_t data_array[256];
    static uint8_t index = 0;

    switch (p_event->evt_type)
    {
    /**@snippet [Handling data from UART] */
    case APP_UART_DATA_READY:
    //UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    app_uart_get(&data_array[index]);
    index++;

    if ((data_array[index - 1] ==ASCII_FOR_CTRL_C)|| (index >=255))
    {
    data_array[index]=0;
    index = 0;
    memcpy(uart_rx_buff,data_array,sizeof(data_array));

    uart_state.rx_finsh=set;
    }
    break;
    /**@snippet [Handling data from UART] */
    case APP_UART_COMMUNICATION_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_communication);
    break;

    case APP_UART_FIFO_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_code);
    break;

    default:
    break;
    }
    }

    Your sensor handle code:

    static void sensor_printout_cmd(nrf_cli_t const* p_cli, size_t argc, char** argv)

    {

    while(!uart_state.rx_finsh) /* rx is from UART, check here if byte read is CTRL+C */

    {

    uint8_t buf[3];

    read_from_sensor(buf);

    nrf_cli_fprintf(...); /* print sensor readings */

    /*do something if you want to handle data_array[index] */

    }

    Do the rx_finish job with uart_rx_buff here

Reply
  • You the uart handle to get the RX data is the best way in your case.

    Like this

    void uart_event_handle(app_uart_evt_t * p_event)
    {
    static uint8_t data_array[256];
    static uint8_t index = 0;

    switch (p_event->evt_type)
    {
    /**@snippet [Handling data from UART] */
    case APP_UART_DATA_READY:
    //UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    app_uart_get(&data_array[index]);
    index++;

    if ((data_array[index - 1] ==ASCII_FOR_CTRL_C)|| (index >=255))
    {
    data_array[index]=0;
    index = 0;
    memcpy(uart_rx_buff,data_array,sizeof(data_array));

    uart_state.rx_finsh=set;
    }
    break;
    /**@snippet [Handling data from UART] */
    case APP_UART_COMMUNICATION_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_communication);
    break;

    case APP_UART_FIFO_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_code);
    break;

    default:
    break;
    }
    }

    Your sensor handle code:

    static void sensor_printout_cmd(nrf_cli_t const* p_cli, size_t argc, char** argv)

    {

    while(!uart_state.rx_finsh) /* rx is from UART, check here if byte read is CTRL+C */

    {

    uint8_t buf[3];

    read_from_sensor(buf);

    nrf_cli_fprintf(...); /* print sensor readings */

    /*do something if you want to handle data_array[index] */

    }

    Do the rx_finish job with uart_rx_buff here

Children
No Data
Related