Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
- if( CHECK_BIT(received_data[1], 7)) //set or clear LED2
- {LATAbits.LATA2=1;}else{LATAbits.LATA2=0;}
- if( CHECK_BIT(received_data[5], 7)) //set or clear LED3
- {LATAbits.LATA3=1;}else{LATAbits.LATA3=0;}
- float f1 = *(float*)(received_data+1)
- float f2 = *(float*)(received_data+5)
- union {
- char chars[4];
- float f;
- } u;
- for (i = 0; i < 4; i++)
- u.chars[3-i] = received_data[i+1];
- float f1 = u.f;
- // ... and similarly for second float
- float f = *((float*)(&received_data[1]));
- char *data;
- //Assemble in the *right* order
- data[0] = received_data[4]; // index is machine dependant!
- ...
- // cast
- float *fptr = (float *)data;
- printf ("%fn",*fptr);
- struct packet_s {
- char byte0;
- float f1;
- float f2;
- ...
- }
- static char* bufferFloat (int* bLen, float value) {
- *bLen = sizeof(float);
- char* buf = malloc (*bLen);
- int storage;
- memcpy (&storage, &value, sizeof (int));
- uint32_t val = htonl (storage);
- memcpy (buf, &val, *bLen);
- return buf;
- }
- static float readFloat (char* buf, int* bLen) {
- float ret;
- int temp;
- if (*bLen < sizeof (float)) {
- *bLen = 0;
- return 0.0f;
- }
- memcpy (&temp, buf, sizeof (int));
- uint32_t val = htonl(temp);
- memcpy (&ret, &val, sizeof (float));
- *bLen = sizeof (float);
- return ret;
- }
- inline float FloatFromByteArray (const unsigned char * received_data)
- {
- float f;
- memcpy (&f, received_data, sizeof (float));
- return f;
- }
- inline float FloatFromByteArray (const unsigned char * received_data)
- {
- float f;
- memcpy (&f, received_data+1, sizeof (float));
- return f;
- }
Add Comment
Please, Sign In to add comment