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

  1. From e596496f5c783f4bba85c4d559502c98e4050465 Mon Sep 17 00:00:00 2001
  2. From: cekstam <christian.ekstam@gmail.com>
  3. Date: Tue, 27 Mar 2018 14:11:52 +0200
  4. Subject: [PATCH 2/3] correcting all the wrongs
  5. , data->scale, data->shift
  6. ---
  7. src/modbus.c | 32 +++++++++++++++-----------------
  8. 1 file changed, 15 insertions(+), 17 deletions(-)
  9. --- a/src/modbus.c
  10. +++ b/src/modbus.c
  11. @@ -394,16 +394,16 @@ static int mb_init_connection(mb_host_t
  12. } /* }}} int mb_init_connection */
  13. #endif /* !LEGACY_LIBMODBUS */
  14. -#define CAST_TO_VALUE_T(ds, vt, raw) \
  15. +#define CAST_TO_VALUE_T(ds, vt, raw, scale, shift) \
  16. do { \
  17. if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
  18. - (vt).counter = (((counter_t)(raw) * ds[0].scale) + ds[0].shift); \
  19. + (vt).counter = (((counter_t)(raw) * scale) + shift); \
  20. else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
  21. - (vt).gauge = (((gauge_t)(raw) * ds[0].scale) + ds[0].shift); \
  22. + (vt).gauge = (((gauge_t)(raw) * scale) + shift); \
  23. else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
  24. - (vt).derive = (((derive_t)(raw) * ds[0].scale) + ds[0].shift); \
  25. + (vt).derive = (((derive_t)(raw) * scale) + shift); \
  26. else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
  27. - (vt).absolute = (((absolute_t)(raw) * ds[0].scale) + ds[0].shift); \
  28. + (vt).absolute = (((absolute_t)(raw) * scale) + shift); \
  29. } while (0)
  30. static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
  31. @@ -532,7 +532,7 @@ static int mb_read_data(mb_host_t *host,
  32. "Returned float value is %g",
  33. (double)float_value);
  34. - CAST_TO_VALUE_T(ds, vt, float_value);
  35. + CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
  36. mb_submit(host, slave, data, vt);
  37. } else if (data->register_type == REG_TYPE_FLOAT_CDAB) {
  38. float float_value;
  39. @@ -543,7 +543,7 @@ static int mb_read_data(mb_host_t *host,
  40. "Returned float value is %g",
  41. (double)float_value);
  42. - CAST_TO_VALUE_T(ds, vt, float_value);
  43. + CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
  44. mb_submit(host, slave, data, vt);
  45. } else if (data->register_type == REG_TYPE_INT32) {
  46. union {
  47. @@ -557,7 +557,7 @@ static int mb_read_data(mb_host_t *host,
  48. "Returned int32 value is %" PRIi32,
  49. v.i32);
  50. - CAST_TO_VALUE_T(ds, vt, v.i32);
  51. + CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
  52. mb_submit(host, slave, data, vt);
  53. } else if (data->register_type == REG_TYPE_INT32_CDAB) {
  54. union {
  55. @@ -571,7 +571,7 @@ static int mb_read_data(mb_host_t *host,
  56. "Returned int32 value is %" PRIi32,
  57. v.i32);
  58. - CAST_TO_VALUE_T(ds, vt, v.i32);
  59. + CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
  60. mb_submit(host, slave, data, vt);
  61. } else if (data->register_type == REG_TYPE_INT16) {
  62. union {
  63. @@ -586,7 +586,7 @@ static int mb_read_data(mb_host_t *host,
  64. "Returned int16 value is %" PRIi16,
  65. v.i16);
  66. - CAST_TO_VALUE_T(ds, vt, v.i16);
  67. + CAST_TO_VALUE_T(ds, vt, v.i16, data->scale, data->shift);
  68. mb_submit(host, slave, data, vt);
  69. } else if (data->register_type == REG_TYPE_UINT32) {
  70. uint32_t v32;
  71. @@ -597,7 +597,7 @@ static int mb_read_data(mb_host_t *host,
  72. "Returned uint32 value is %" PRIu32,
  73. v32);
  74. - CAST_TO_VALUE_T(ds, vt, v32);
  75. + CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
  76. mb_submit(host, slave, data, vt);
  77. } else if (data->register_type == REG_TYPE_UINT32_CDAB) {
  78. uint32_t v32;
  79. @@ -608,7 +608,7 @@ static int mb_read_data(mb_host_t *host,
  80. "Returned uint32 value is %" PRIu32,
  81. v32);
  82. - CAST_TO_VALUE_T(ds, vt, v32);
  83. + CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
  84. mb_submit(host, slave, data, vt);
  85. } else /* if (data->register_type == REG_TYPE_UINT16) */
  86. {
  87. @@ -618,7 +618,7 @@ static int mb_read_data(mb_host_t *host,
  88. "Returned uint16 value is %" PRIu16,
  89. values[0]);
  90. - CAST_TO_VALUE_T(ds, vt, values[0]);
  91. + CAST_TO_VALUE_T(ds, vt, values[0], data->scale, data->shift);
  92. mb_submit(host, slave, data, vt);
  93. }
  94. @@ -741,11 +741,9 @@ static int mb_config_add_data(oconfig_it
  95. status = cf_util_get_string_buffer(child, data.instance,
  96. sizeof(data.instance));
  97. else if (strcasecmp("Scale", child->key) == 0)
  98. - status = cf_util_get_string_buffer(child, data.scale,
  99. - sizeof(data.scale));
  100. + status = cf_util_get_double(child, &data.scale);
  101. else if (strcasecmp("Shift", child->key) == 0)
  102. - status = cf_util_get_string_buffer(child, data.shift,
  103. - sizeof(data.shift));
  104. + status = cf_util_get_double(child, &data.shift);
  105. else if (strcasecmp("RegisterBase", child->key) == 0)
  106. status = cf_util_get_int(child, &data.register_base);
  107. else if (strcasecmp("RegisterType", child->key) == 0) {