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

Sending 2-bytes value from Central to Peripheral

Hey,

I would like to send data from Central to Peripheral. I follow by ble_app_uart example, however I need to send a float type variable, so I split it to the total and fractional part.

For example, my variable is 3.14. I call ble_nus_c_string_send() method two times - sending value: 3 and next time sending value: 14. I would like to store this in characteristic as a total part in one byte and fractional in another. Unfortunately, in this way I am able to send only 1 byte which is rewrite in characteristic when I send the second one.

so I need something like this: 0E-03,

now this looks like: 0E-00 after first sending and 03-00 after next one.

Is it possible to send 2-bytes variable using this function? Or can I write received value as a second byte of my attribute value?

I would be very grateful for any help.

Parents
  • You don't have to split the float up if you don't want to. You could send all four bytes of the float in one go. After all ble_nus_string_send() can send 20 bytes in one go. The way I would do it is bit masking so you cover up and isolate three bytes while putting one byte of the float into the array to send. So do this:

    Set up your array to send uint8_t data_to_send_array[4] = { 0 }

    Next get your float_value and do this:

    data_to_send_array[0] = (float_value & 0xFF000000) >> 24;
    data_to_send_array[1] = (float_value & 0x00FF0000) >> 16;
    data_to_send_array[2] = (float_value & 0x0000FF00) >> 8;
    data_to_send_array[3] = (float_value & 0x000000FF);
    

    Then call ble_nus_string_send(&m_nus, data_to_send_array , 4) and you will be good.

    Else if you want to split the bifurcate your float into separate values apply the same principle above to each byte size of the new values.

Reply
  • You don't have to split the float up if you don't want to. You could send all four bytes of the float in one go. After all ble_nus_string_send() can send 20 bytes in one go. The way I would do it is bit masking so you cover up and isolate three bytes while putting one byte of the float into the array to send. So do this:

    Set up your array to send uint8_t data_to_send_array[4] = { 0 }

    Next get your float_value and do this:

    data_to_send_array[0] = (float_value & 0xFF000000) >> 24;
    data_to_send_array[1] = (float_value & 0x00FF0000) >> 16;
    data_to_send_array[2] = (float_value & 0x0000FF00) >> 8;
    data_to_send_array[3] = (float_value & 0x000000FF);
    

    Then call ble_nus_string_send(&m_nus, data_to_send_array , 4) and you will be good.

    Else if you want to split the bifurcate your float into separate values apply the same principle above to each byte size of the new values.

Children
Related