You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

120 lines
5.2 KiB

From e596496f5c783f4bba85c4d559502c98e4050465 Mon Sep 17 00:00:00 2001
From: cekstam <christian.ekstam@gmail.com>
Date: Tue, 27 Mar 2018 14:11:52 +0200
Subject: [PATCH 2/3] correcting all the wrongs
, data->scale, data->shift
---
src/modbus.c | 32 +++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)
--- a/src/modbus.c
+++ b/src/modbus.c
@@ -394,16 +394,16 @@ static int mb_init_connection(mb_host_t
} /* }}} int mb_init_connection */
#endif /* !LEGACY_LIBMODBUS */
-#define CAST_TO_VALUE_T(ds, vt, raw) \
+#define CAST_TO_VALUE_T(ds, vt, raw, scale, shift) \
do { \
if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
- (vt).counter = (((counter_t)(raw) * ds[0].scale) + ds[0].shift); \
+ (vt).counter = (((counter_t)(raw) * scale) + shift); \
else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
- (vt).gauge = (((gauge_t)(raw) * ds[0].scale) + ds[0].shift); \
+ (vt).gauge = (((gauge_t)(raw) * scale) + shift); \
else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
- (vt).derive = (((derive_t)(raw) * ds[0].scale) + ds[0].shift); \
+ (vt).derive = (((derive_t)(raw) * scale) + shift); \
else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
- (vt).absolute = (((absolute_t)(raw) * ds[0].scale) + ds[0].shift); \
+ (vt).absolute = (((absolute_t)(raw) * scale) + shift); \
} while (0)
static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
@@ -532,7 +532,7 @@ static int mb_read_data(mb_host_t *host,
"Returned float value is %g",
(double)float_value);
- CAST_TO_VALUE_T(ds, vt, float_value);
+ CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
mb_submit(host, slave, data, vt);
} else if (data->register_type == REG_TYPE_FLOAT_CDAB) {
float float_value;
@@ -543,7 +543,7 @@ static int mb_read_data(mb_host_t *host,
"Returned float value is %g",
(double)float_value);
- CAST_TO_VALUE_T(ds, vt, float_value);
+ CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
mb_submit(host, slave, data, vt);
} else if (data->register_type == REG_TYPE_INT32) {
union {
@@ -557,7 +557,7 @@ static int mb_read_data(mb_host_t *host,
"Returned int32 value is %" PRIi32,
v.i32);
- CAST_TO_VALUE_T(ds, vt, v.i32);
+ CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
mb_submit(host, slave, data, vt);
} else if (data->register_type == REG_TYPE_INT32_CDAB) {
union {
@@ -571,7 +571,7 @@ static int mb_read_data(mb_host_t *host,
"Returned int32 value is %" PRIi32,
v.i32);
- CAST_TO_VALUE_T(ds, vt, v.i32);
+ CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
mb_submit(host, slave, data, vt);
} else if (data->register_type == REG_TYPE_INT16) {
union {
@@ -586,7 +586,7 @@ static int mb_read_data(mb_host_t *host,
"Returned int16 value is %" PRIi16,
v.i16);
- CAST_TO_VALUE_T(ds, vt, v.i16);
+ CAST_TO_VALUE_T(ds, vt, v.i16, data->scale, data->shift);
mb_submit(host, slave, data, vt);
} else if (data->register_type == REG_TYPE_UINT32) {
uint32_t v32;
@@ -597,7 +597,7 @@ static int mb_read_data(mb_host_t *host,
"Returned uint32 value is %" PRIu32,
v32);
- CAST_TO_VALUE_T(ds, vt, v32);
+ CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
mb_submit(host, slave, data, vt);
} else if (data->register_type == REG_TYPE_UINT32_CDAB) {
uint32_t v32;
@@ -608,7 +608,7 @@ static int mb_read_data(mb_host_t *host,
"Returned uint32 value is %" PRIu32,
v32);
- CAST_TO_VALUE_T(ds, vt, v32);
+ CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
mb_submit(host, slave, data, vt);
} else /* if (data->register_type == REG_TYPE_UINT16) */
{
@@ -618,7 +618,7 @@ static int mb_read_data(mb_host_t *host,
"Returned uint16 value is %" PRIu16,
values[0]);
- CAST_TO_VALUE_T(ds, vt, values[0]);
+ CAST_TO_VALUE_T(ds, vt, values[0], data->scale, data->shift);
mb_submit(host, slave, data, vt);
}
@@ -741,11 +741,9 @@ static int mb_config_add_data(oconfig_it
status = cf_util_get_string_buffer(child, data.instance,
sizeof(data.instance));
else if (strcasecmp("Scale", child->key) == 0)
- status = cf_util_get_string_buffer(child, data.scale,
- sizeof(data.scale));
+ status = cf_util_get_double(child, &data.scale);
else if (strcasecmp("Shift", child->key) == 0)
- status = cf_util_get_string_buffer(child, data.shift,
- sizeof(data.shift));
+ status = cf_util_get_double(child, &data.shift);
else if (strcasecmp("RegisterBase", child->key) == 0)
status = cf_util_get_int(child, &data.register_base);
else if (strcasecmp("RegisterType", child->key) == 0) {