<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>ble_nus_data_send sending the wrong data</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93902/ble_nus_data_send-sending-the-wrong-data</link><description>Hi, 
 I am writing a program that sends numbers counting up sequentially from my dev board to my phone on 500ms intervals. I used the ble_app_uart example as my base. I am trying to use the command ble_nus_data_send(&amp;amp;m_nus, up_counter, &amp;amp;length, m_conn_handle</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 15 Nov 2022 13:54:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93902/ble_nus_data_send-sending-the-wrong-data" /><item><title>RE: ble_nus_data_send sending the wrong data</title><link>https://devzone.nordicsemi.com/thread/395812?ContentTypeID=1</link><pubDate>Tue, 15 Nov 2022 13:54:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:323b9061-837b-436a-9c4f-ae6eef7aea6d</guid><dc:creator>nl671dev</dc:creator><description>&lt;p&gt;I think I have it solved now. In the line&amp;nbsp;&lt;strong&gt;ble_nus_data_send&lt;/strong&gt;(&amp;amp;m_nus, counter_char, &amp;amp;length, m_conn_handle), length was set to&amp;nbsp;BLE_NUS_MAX_DATA_LEN, which is some large number. I set length to 4 (the program should terminate before the counter reaches 1000), and the output is correct as far as I know. My code is posted below for reference.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void count_up();
volatile int up_counter = 0;

void count_up()
{
    //int length = BLE_NUS_MAX_DATA_LEN;
    int length = 4;
    ble_conn_state_conn_handle_list_t conn_handles = ble_conn_state_periph_handles();
    uint32_t err_code;
    char counter_char[20];
    //uint32_t time_err_code;
    //static uint32_t timeout = 0;
    

    for (uint8_t i=m_last_successful_conn_handle; i&amp;lt;conn_handles.len; i++)
    {
        itoa(up_counter, counter_char, 10);
        //NRF_LOG_DEBUG(&amp;quot;Integer: %d   Converted String: %c\n&amp;quot;, up_counter, counter_char);
        err_code = ble_nus_data_send(&amp;amp;m_nus, counter_char, &amp;amp;length, m_conn_handle);

        if(err_code == NRF_SUCCESS)
        {
            m_last_successful_conn_handle++;
        }
        else if (err_code == NRF_ERROR_RESOURCES)
        {
            // Queue is full. Break the loop, and countinue with the m_last_successful_conn_handle after TX_COMPLETE
            break;
        }
        //err_code = ble_nus_data_send(&amp;amp;m_nus, up_counter, &amp;amp;length, m_conn_handle);
        
    }
    
    if (m_last_successful_conn_handle == conn_handles.len)
    {
        m_last_successful_conn_handle = 0;
    }

    if(up_counter == 255)
    {
        up_counter == 0;
    } else
    {
        up_counter++;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Thank you for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ble_nus_data_send sending the wrong data</title><link>https://devzone.nordicsemi.com/thread/395801?ContentTypeID=1</link><pubDate>Tue, 15 Nov 2022 13:27:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b350060a-48dd-4b69-acba-22318eb2222f</guid><dc:creator>nl671dev</dc:creator><description>&lt;p&gt;Thank you for answering. I have tried to implement the&amp;nbsp;&lt;strong&gt;itoa&lt;/strong&gt; function, but it doesnt seem to be working. The output to my phone still looks the same. I even tried to look at the output in the debugger, and it&amp;#39;s also incorrect. I posted my code and the debugger output below. Do you see whether I did something wrong?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void count_up();
int up_counter = 0;

void count_up()
{
    int length = BLE_NUS_MAX_DATA_LEN;
    ble_conn_state_conn_handle_list_t conn_handles = ble_conn_state_periph_handles();
    uint32_t err_code;
    char counter_char[sizeof(int)*8+1];
    //uint32_t time_err_code;
    //static uint32_t timeout = 0;
    

    for (uint8_t i=m_last_successful_conn_handle; i&amp;lt;conn_handles.len; i++)
    {
        itoa(up_counter, counter_char, 10);
        NRF_LOG_DEBUG(&amp;quot;Converted String: %c&amp;quot;, counter_char);
        err_code = ble_nus_data_send(&amp;amp;m_nus, counter_char, &amp;amp;length, m_conn_handle);

        if(err_code == NRF_SUCCESS)
        {
            m_last_successful_conn_handle++;
        }
        else if (err_code == NRF_ERROR_RESOURCES)
        {
            // Queue is full. Break the loop, and countinue with the m_last_successful_conn_handle after TX_COMPLETE
            break;
        }
        
    }
    
    if (m_last_successful_conn_handle == conn_handles.len)
    {
        m_last_successful_conn_handle = 0;
    }

    if(up_counter == 255)
    {
        up_counter == 0;
    } else
    {
        up_counter++;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1668518802095v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;
&lt;p&gt;Edit: removed a line from the code that was obviously wrong. The output is still the same, unfortunately.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ble_nus_data_send sending the wrong data</title><link>https://devzone.nordicsemi.com/thread/395777?ContentTypeID=1</link><pubDate>Tue, 15 Nov 2022 12:38:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dcfc098a-cbc8-4d09-acbc-450e328bc30f</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Your&amp;nbsp;up_counter is an uint8_t, starting from 0. So you are sending raw numbers, not ascii characters. When the receiver prints these as if they were ascii characters, that will result in a lot of strange charcters and is expected. I assume what you want to do is send an ascii representatnion of th enumber? If so, use snprintf() or itoa() or similar to convert it to a string before sending it with&amp;nbsp;ble_nus_data_send().&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>