Commit e6349372a8c94d905e405e8ca6313e827532425c

Authored by Stéphane Raimbault
1 parent 05efa1f7

Minor changes in float functions

src/modbus-data.c
@@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
19 #include <stdint.h> 19 #include <stdint.h>
20 #include <string.h> 20 #include <string.h>
21 #include <assert.h> 21 #include <assert.h>
  22 +#include <netinet/in.h>
22 23
23 /* Sets many bits from a single byte value (all 8 bits of the byte value are 24 /* Sets many bits from a single byte value (all 8 bits of the byte value are
24 set) */ 25 set) */
@@ -71,20 +72,21 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address, @@ -71,20 +72,21 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address,
71 /* Get a float from 4 bytes in Modbus format */ 72 /* Get a float from 4 bytes in Modbus format */
72 float modbus_get_float(const uint16_t *src) 73 float modbus_get_float(const uint16_t *src)
73 { 74 {
74 - float r = 0.0f; 75 + float f = 0.0f;
75 uint32_t i; 76 uint32_t i;
76 77
77 i = (((uint32_t)src[1]) << 16) + src[0]; 78 i = (((uint32_t)src[1]) << 16) + src[0];
78 - memcpy(&r, &i, sizeof (r));  
79 - return r; 79 + memcpy(&f, &i, sizeof(float));
  80 +
  81 + return f;
80 } 82 }
81 83
82 /* Set a float to 4 bytes in Modbus format */ 84 /* Set a float to 4 bytes in Modbus format */
83 -void modbus_set_float(float real, uint16_t *dest) 85 +void modbus_set_float(float f, uint16_t *dest)
84 { 86 {
85 uint32_t i = 0; 87 uint32_t i = 0;
86 88
87 - memcpy(&i, &real, sizeof (i)); 89 + memcpy(&i, &f, sizeof(uint32_t));
88 dest[0] = (uint16_t)i; 90 dest[0] = (uint16_t)i;
89 dest[1] = (uint16_t)(i >> 16); 91 dest[1] = (uint16_t)(i >> 16);
90 } 92 }
src/modbus.h
@@ -193,7 +193,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits @@ -193,7 +193,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits
193 const uint8_t *tab_byte); 193 const uint8_t *tab_byte);
194 uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address, unsigned int nb_bits); 194 uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address, unsigned int nb_bits);
195 float modbus_get_float(const uint16_t *src); 195 float modbus_get_float(const uint16_t *src);
196 -void modbus_set_float(float real, uint16_t *dest); 196 +void modbus_set_float(float f, uint16_t *dest);
197 197
198 #include "modbus-tcp.h" 198 #include "modbus-tcp.h"
199 #include "modbus-rtu.h" 199 #include "modbus-rtu.h"