瀏覽代碼

Minor changes in float functions

Stéphane Raimbault 14 年之前
父節點
當前提交
e6349372a8
共有 2 個文件被更改,包括 8 次插入6 次删除
  1. 7 5
      src/modbus-data.c
  2. 1 1
      src/modbus.h

+ 7 - 5
src/modbus-data.c

@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include <netinet/in.h>
 
 /* Sets many bits from a single byte value (all 8 bits of the byte value are
    set) */
@@ -71,20 +72,21 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address,
 /* Get a float from 4 bytes in Modbus format */
 float modbus_get_float(const uint16_t *src)
 {
-    float r = 0.0f;
+    float f = 0.0f;
     uint32_t i;
 
     i = (((uint32_t)src[1]) << 16) + src[0];
-    memcpy(&r, &i, sizeof (r));
-    return r;
+    memcpy(&f, &i, sizeof(float));
+
+    return f;
 }
 
 /* Set a float to 4 bytes in Modbus format */
-void modbus_set_float(float real, uint16_t *dest)
+void modbus_set_float(float f, uint16_t *dest)
 {
     uint32_t i = 0;
 
-    memcpy(&i, &real, sizeof (i));
+    memcpy(&i, &f, sizeof(uint32_t));
     dest[0] = (uint16_t)i;
     dest[1] = (uint16_t)(i >> 16);
 }

+ 1 - 1
src/modbus.h

@@ -193,7 +193,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits
                                 const uint8_t *tab_byte);
 uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address, unsigned int nb_bits);
 float modbus_get_float(const uint16_t *src);
-void modbus_set_float(float real, uint16_t *dest);
+void modbus_set_float(float f, uint16_t *dest);
 
 #include "modbus-tcp.h"
 #include "modbus-rtu.h"