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

Offset while sending data

I have a project which will send the temperature from the nRF51DK to host.

The value of the temperature is an integer. As example Value:0x00000A2B -> 2603 ->26.03°C

But on the Host side I will recieve only Value:2B

The same if I change the length of the Bytes to 4

nRF51DK Value is 0x00002710, but on the host side I will recieve Value:10-00-00-00

Here the set up for the characteristic(with 4Bytes):

attr_char_value.p_uuid       = &ble_uuid;
attr_char_value.p_attr_md    = &attr_md;
attr_char_value.init_len     = 4;
attr_char_value.init_offs    = 0;
attr_char_value.max_len      = 4;
attr_char_value.p_value      = NULL;

uint32_t ble_fridge_send_temp(ble_fridge_t * p_fridge, uint8_t temp_value)
{
uint32_t err_code = NRF_SUCCESS;									
ble_gatts_value_t gatts_value;										

if(temp_value != p_fridge->temp_value_last)				
{
	memset(&gatts_value,0,sizeof(gatts_value));			
	
	
	gatts_value.len 	= 4;					
	gatts_value.offset	= 0;												
	gatts_value.p_value	= &temp_value;							
	
	p_fridge->temp_value_last = temp_value;					

	err_code = sd_ble_gatts_value_set(p_fridge->conn_handle,p_fridge->temp_char_handles.value_handle,&gatts_value);
	if(err_code != NRF_SUCCESS)
	{
		return err_code;
	}
}

Why is there an offset?

Or isnt a offset? Maybe the Byte order is false?

Parents
  • Googling int32 to 4 uint8 may help you if I get your problem right. Check this Link: stackoverflow.com/.../converting-a-uint32-value-into-a-uint8-array4

    Your measured value is int32? You then need to split this into an uint8 array before you send it. And the other way around when you want the original value back. Sadly I am not at home and don't have access to my application where is implemented this

  • it just casts the pointer to uint32_t to a pointer to uint8_t, not the value, the pointer.

    It's misleading that function takes a uint8_t* because it makes people think that it wants only uint8_t, a single byte or single character. In some ways it would be better if it were a void* to indicate that it's just 'the first byte in a sequence of bytes'. Anyway what that function really takes is a buffer of bytes, the pointer to the first byte is p_value, len is the number of bytes. Doesn't have to be an actual array, doesn't have to be anything related to uint8_t, just needs to be a block of bytes starting at p_value and of length bytes.

    In this case your value is a uint32_t, which is 4 bytes one after another. &(temp_value) is a pointer to the first byte, which is what you want, it's the right pointer, just the wrong type of pointer. (uint8_t*) just says 'treat it like a byte pointer".

Reply
  • it just casts the pointer to uint32_t to a pointer to uint8_t, not the value, the pointer.

    It's misleading that function takes a uint8_t* because it makes people think that it wants only uint8_t, a single byte or single character. In some ways it would be better if it were a void* to indicate that it's just 'the first byte in a sequence of bytes'. Anyway what that function really takes is a buffer of bytes, the pointer to the first byte is p_value, len is the number of bytes. Doesn't have to be an actual array, doesn't have to be anything related to uint8_t, just needs to be a block of bytes starting at p_value and of length bytes.

    In this case your value is a uint32_t, which is 4 bytes one after another. &(temp_value) is a pointer to the first byte, which is what you want, it's the right pointer, just the wrong type of pointer. (uint8_t*) just says 'treat it like a byte pointer".

Children
No Data
Related