When using NUS, configure communication protocol

hello!

I am developing communication using Nordic UART Service.

 

It defines the protocol of data sent by the ble_nus_data_send() function.

err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle); )

When composing a communication protocol, for reliability, it usually starts with STX (0x02) and ends with ETX (0x03) and uses length and checksum.

|STX | Length | data~~~~~~ | checksum | ETX |

By the way, if Nordic UART Service is used this time, I think there will be a data structure for reliability as above in the payload of Nordic UART Service. Is it okay to omit it and transmit only data?

|STX | Length | data~~~~~~ | checksum | ETX |    =====>   only data~~~~~~ 

Additionally, I want to see the data packets from NUS, but I can't find them. If it is public, please link.

Parents
  • I think using NUS gives you flexibility regarding data structure. It's doable for you to send your data structure with NUS. You can check example from nRF52-ADC to see how they use NUS to send ADC values from multiple channels.

     // Send data over BLE via NUS service. Create string from samples and send string with correct length.
            uint8_t nus_string[50];
            bytes_to_send = sprintf(nus_string, 
                                    "CH0: %d\r\nCH1: %d\r\nCH2: %d\r\nCH3: %d",
                                    p_event->data.done.p_buffer[0],
                                    p_event->data.done.p_buffer[1],
                                    p_event->data.done.p_buffer[2],
                                    p_event->data.done.p_buffer[3]);
    
            err_code = ble_nus_data_send(&m_nus, nus_string, &bytes_to_send, m_conn_handle);
            if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
            {
                APP_ERROR_CHECK(err_code);
            }

Reply
  • I think using NUS gives you flexibility regarding data structure. It's doable for you to send your data structure with NUS. You can check example from nRF52-ADC to see how they use NUS to send ADC values from multiple channels.

     // Send data over BLE via NUS service. Create string from samples and send string with correct length.
            uint8_t nus_string[50];
            bytes_to_send = sprintf(nus_string, 
                                    "CH0: %d\r\nCH1: %d\r\nCH2: %d\r\nCH3: %d",
                                    p_event->data.done.p_buffer[0],
                                    p_event->data.done.p_buffer[1],
                                    p_event->data.done.p_buffer[2],
                                    p_event->data.done.p_buffer[3]);
    
            err_code = ble_nus_data_send(&m_nus, nus_string, &bytes_to_send, m_conn_handle);
            if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
            {
                APP_ERROR_CHECK(err_code);
            }

Children
Related