app_uart_put not working

Hi,
I am using ble_app_uart and successfully receiving data but am unable to send back data. Is this the right way to send data back?

static void nus_data_handler(ble_nus_evt_t * p_evt)
{
    uint8_t da=5;


    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
        uint32_t err_code;
        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);
        //NRF_LOG_INFO("Data Received 1 : %x",p_evt->params.rx_data.p_data[0]);
        //NRF_LOG_INFO("Data Received 2 : %x",p_evt->params.rx_data.p_data[1]);
        //NRF_LOG_INFO("Data Received 3 : %x",p_evt->params.rx_data.p_data[2]);
        //NRF_LOG_INFO("Data Received 4 : %x",p_evt->params.rx_data.p_data[3]);
        //NRF_LOG_INFO("Data Received 5 : %x",p_evt->params.rx_data.p_data[4]);
        //NRF_LOG_INFO("Data Received 6 : %x",p_evt->params.rx_data.p_data[5]);

        while (app_uart_put(da)!= NRF_SUCCESS);//Sending response

        //for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
        //{
        //    do
        //    {
        //        err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
        //        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);
        //}
    
    
    }

}


Parents
  • Hello,

    Please clarify what you mean when you say that you would like to send data back - do you mean over the NUS service?
    Right now, it seems that you are attempting to output your value to the UART peripheral. 
    In order to use NUS service to send data to the other device you should instead look at the UART event handler's implementation in the BLE NUS example, which forwards the data it receives over UART to the connected device through the NUS service.

    Best regards,
    Karl

  • Hi 
    I have a central device (ble_app_uart_c). I want to send data back to the central device. How can I send data back?

  • Do I understand you correctly that you want to send data from the peripheral ble_app_uart to the central device running ble_app_uart_c?
    If so, please see the attached code section from ble_app_uart:

    .. 
            case APP_UART_DATA_READY:
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
    
                if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len)))
                {
                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                    do
                    {
                        uint16_t length = (uint16_t)index;
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) &&
                             (err_code != NRF_ERROR_NOT_FOUND) )
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_BUSY);
    
                    index = 0;
                }
                break;
    ..

    This is the section that picks up the UART data and sends it over BLE with the call to ble_nus_data_send. You can use the ble_nus_data_send function elsewhere to send other data to your central device.

    Best regards,
    Karl

Reply
  • Do I understand you correctly that you want to send data from the peripheral ble_app_uart to the central device running ble_app_uart_c?
    If so, please see the attached code section from ble_app_uart:

    .. 
            case APP_UART_DATA_READY:
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
    
                if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len)))
                {
                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                    do
                    {
                        uint16_t length = (uint16_t)index;
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) &&
                             (err_code != NRF_ERROR_NOT_FOUND) )
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_BUSY);
    
                    index = 0;
                }
                break;
    ..

    This is the section that picks up the UART data and sends it over BLE with the call to ble_nus_data_send. You can use the ble_nus_data_send function elsewhere to send other data to your central device.

    Best regards,
    Karl

Children
No Data
Related