Home > OS >  How to read a packet that used memcpy
How to read a packet that used memcpy

Time:09-21

    uint8_t sendPacket[200];
    
    float a = 22.0f;  

char buffer[4] ={0x01,0x02,0x03,0x04};


    memcpy(&buffer, &a, sizeof(a));
    memcpy(sendPacket 92,buffer,sizeof(buffer));
            
    HAL_UART_Transmit(&huart7, (uint8_t*)sendPacket, length 5, 0xFFFF);

I use the code above, but I know memcpy copies the memory, but when I looked at the value in Packet, the value came out so weird

There are other chords on top, but it's almost repeated, so I took it out

It's supposed to be transmitted through Bluetooth, but it comes out properly in the place where it's transmitted through Bluetooth, but it comes out weird

First of all, I want to send the packet in stm32 to another place to get the value, The result of the live watch is the same as in the picture

a contains float value

enter image description here

CodePudding user response:

To read the float again you could for instance memcpy into a float:

float a_received = 0.0f;
memcpy(&a_received, &sendPacket[92], sizeof(a_received));

As user694733 pointed out, using memcpy.

CodePudding user response:

" when I looked at the value in Packet, the value came out so weird"

When you look in sendPacketwhat you see is raw bytes. To check whether you have copied the right float at the right place you need to interpret the 4 bytes from your array to a float value. It is basically what a debugger or a printf would do.

For exemple the number 22.0f is represented in IEE754 floating number representation by 0x41b00000 so you should see in your array 00 00 b0 41 for example on a little endian system.

  • Related