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

Turn off print to terminal with NUS data handler

Hi!

I'm using the ble_app_uart example with SDK 15.0.3 and nRF52840 DK. I'm using Termite as the terminal.

When receiving data from an iPad to the nRF, with the nRF as the peripheral, the nus_data_handler is invoked. I want to print the data received to the terminal, so that I can verify it. It seems to print, but it prints in ASCII, so it's not really readable. Is there a way to make it HEX or just turn off this "automatic" print so I can print it my self? In the code below is the nus_data_handler and I have figured out which line somehow does the printing, but I can not figure out how to change it.

static void nus_data_handler(ble_nus_evt_t * p_evt)
{

    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
        uint32_t err_code;
        
        printf("\n\nReceived data from iPad!\n");
        NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
        //NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);

        for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
        {
            do
            {
                printf("[ %i ] ", p_evt->params.rx_data.p_data[i]);                            // print to serial monitor
                err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);                     // send data on byte on UART
                //printf("*");
                if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                {
                    NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
                    APP_ERROR_CHECK(err_code);
                }
            } while (err_code == NRF_ERROR_BUSY);
        }
        if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
        {
            while (app_uart_put('\n') == NRF_ERROR_BUSY);
        }
    }

}

The code line that does printing to the terminal is 

err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);  // send data on byte on UART

Any help would be appreciated!

  • Hi.

    The UART prints whatever it gets, so you just have to convert the ASCII characters to HEX before app_uart_put().

    For example by making a function that converts and calling it before you call app_uart_put:

    #include <stdio.h>
    #include <string.h>
    
    //function to convert ascii char[] to hex-string (char[])
    void string2hexString(char* input, char* output)
    {
        int loop;
        int i; 
        
        i=0;
        loop=0;
        
        while(input[loop] != '\0')
        {
            sprintf((char*)(output+i),"%02X", input[loop]);
            loop+=1;
            i+=2;
        }
        //insert NULL at the end of the output string
        output[i++] = '\0';
    }
    
    int main(){
        char ascii_str[] = "Hello world!";
        //declare output string with double size of input string
        //because each character of input string will be converted
        //in 2 bytes
        int len = strlen(ascii_str);
        char hex_str[(len*2)+1];
        
        //converting ascii string to hex string
        string2hexString(ascii_str, hex_str);
        
        printf("ascii_str: %s\n", ascii_str);
        printf("hex_str: %s\n", hex_str);
        
        return 0;
    }

    Best regards,

    Andreas

Related