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

nrf UART app receive value sent from uart app

Hi, I am using the nrf uart app to communicate with a smart phone and the NRF52. I am trying to detect whenever it is sent a value between 0 and 60 from the phone, and save it to an int. I know that I get a callback from the function nus_data_handler whenever something is received on the nrf, but I dont really know how to convert that data to type int.

(I am using the smart phone app NRF UART)

-Erblin

Parents
  • One solution to store the number sent from the phone, could be something like this:

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        uint32_t myNumber = 0;
        for (uint32_t i = 0; i < length; i++)
        {
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
            myNumber = myNumber + ((p_data[i]-'0')*pow(10,length-i-1));
        }
        while (app_uart_put('\r') != NRF_SUCCESS);
        while (app_uart_put('\n') != NRF_SUCCESS);
        printf("myNumber is: %d\n",myNumber);
    }
    

    You will need to add #include "math.h" for the pow()-function.

Reply
  • One solution to store the number sent from the phone, could be something like this:

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        uint32_t myNumber = 0;
        for (uint32_t i = 0; i < length; i++)
        {
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
            myNumber = myNumber + ((p_data[i]-'0')*pow(10,length-i-1));
        }
        while (app_uart_put('\r') != NRF_SUCCESS);
        while (app_uart_put('\n') != NRF_SUCCESS);
        printf("myNumber is: %d\n",myNumber);
    }
    

    You will need to add #include "math.h" for the pow()-function.

Children
Related