X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fmodbus.c;h=efcf6be8950d3b302227bac029288be746348780;hp=715724d1d44c8eef2dce93aa533aa91b6fff62ac;hb=d486225f89ea52d8ed2b4242eba2ad94c409f837;hpb=0eff2a882a9265fd59d4f7a265c6b6810acb0032 diff --git a/src/modbus.c b/src/modbus.c index 715724d1..efcf6be8 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -76,9 +76,13 @@ enum mb_register_type_e /* {{{ */ { REG_TYPE_INT16, REG_TYPE_INT32, + REG_TYPE_INT32_CDAB, REG_TYPE_UINT16, REG_TYPE_UINT32, - REG_TYPE_FLOAT }; /* }}} */ + REG_TYPE_UINT32_CDAB, + REG_TYPE_FLOAT, + REG_TYPE_FLOAT_CDAB }; /* }}} */ + enum mb_mreg_type_e /* {{{ */ { MREG_HOLDING, MREG_INPUT }; /* }}} */ @@ -131,7 +135,7 @@ struct mb_host_s /* {{{ */ #else modbus_t *connection; #endif - _Bool is_connected; + bool is_connected; }; /* }}} */ typedef struct mb_host_s mb_host_t; @@ -148,7 +152,7 @@ struct mb_data_group_s /* {{{ */ /* * Global variables */ -static mb_data_t *data_definitions = NULL; +static mb_data_t *data_definitions; /* * Functions @@ -328,7 +332,7 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */ return status; } - host->is_connected = 1; + host->is_connected = true; return 0; } /* }}} int mb_init_connection */ /* #endif LEGACY_LIBMODBUS */ @@ -417,7 +421,7 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ } if (ds->ds_num != 1) { - ERROR("Modbus plugin: The type \"%s\" has %zu data sources. " + ERROR("Modbus plugin: The type \"%s\" has %" PRIsz " data sources. " "I can only handle data sets with only one data source.", data->type, ds->ds_num); return -1; @@ -425,7 +429,9 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ if ((ds->ds[0].type != DS_TYPE_GAUGE) && (data->register_type != REG_TYPE_INT32) && - (data->register_type != REG_TYPE_UINT32)) { + (data->register_type != REG_TYPE_INT32_CDAB) && + (data->register_type != REG_TYPE_UINT32) && + (data->register_type != REG_TYPE_UINT32_CDAB)) { NOTICE( "Modbus plugin: The data source of type \"%s\" is %s, not gauge. " "This will most likely result in problems, because the register type " @@ -434,8 +440,11 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ } if ((data->register_type == REG_TYPE_INT32) || + (data->register_type == REG_TYPE_INT32_CDAB) || (data->register_type == REG_TYPE_UINT32) || - (data->register_type == REG_TYPE_FLOAT)) + (data->register_type == REG_TYPE_UINT32_CDAB) || + (data->register_type == REG_TYPE_FLOAT) || + (data->register_type == REG_TYPE_FLOAT_CDAB)) values_num = 2; else values_num = 1; @@ -456,7 +465,7 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ if (status != 0) { ERROR("Modbus plugin: mb_init_connection (%s/%s) failed. ", host->host, host->node); - host->is_connected = 0; + host->is_connected = false; host->connection = NULL; return -1; } @@ -496,8 +505,8 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ } if (status != values_num) { ERROR("Modbus plugin: modbus read function (%s/%s) failed. " - " status = %i, values_num = %i. Giving up.", - host->host, host->node, status, values_num); + " status = %i, start_addr = %i, values_num = %i. Giving up.", + host->host, host->node, status, data->register_base, values_num); #if LEGACY_LIBMODBUS modbus_close(&host->connection); #else @@ -523,6 +532,17 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ CAST_TO_VALUE_T(ds, vt, float_value); mb_submit(host, slave, data, vt); + } else if (data->register_type == REG_TYPE_FLOAT_CDAB) { + float float_value; + value_t vt; + + float_value = mb_register_to_float(values[1], values[0]); + DEBUG("Modbus plugin: mb_read_data: " + "Returned float value is %g", + (double)float_value); + + CAST_TO_VALUE_T(ds, vt, float_value); + mb_submit(host, slave, data, vt); } else if (data->register_type == REG_TYPE_INT32) { union { uint32_t u32; @@ -537,6 +557,20 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ CAST_TO_VALUE_T(ds, vt, v.i32); mb_submit(host, slave, data, vt); + } else if (data->register_type == REG_TYPE_INT32_CDAB) { + union { + uint32_t u32; + int32_t i32; + } v; + value_t vt; + + v.u32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]); + DEBUG("Modbus plugin: mb_read_data: " + "Returned int32 value is %" PRIi32, + v.i32); + + CAST_TO_VALUE_T(ds, vt, v.i32); + mb_submit(host, slave, data, vt); } else if (data->register_type == REG_TYPE_INT16) { union { uint16_t u16; @@ -563,6 +597,17 @@ static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */ CAST_TO_VALUE_T(ds, vt, v32); mb_submit(host, slave, data, vt); + } else if (data->register_type == REG_TYPE_UINT32_CDAB) { + uint32_t v32; + value_t vt; + + v32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]); + DEBUG("Modbus plugin: mb_read_data: " + "Returned uint32 value is %" PRIu32, + v32); + + CAST_TO_VALUE_T(ds, vt, v32); + mb_submit(host, slave, data, vt); } else /* if (data->register_type == REG_TYPE_UINT16) */ { value_t vt; @@ -702,12 +747,18 @@ static int mb_config_add_data(oconfig_item_t *ci) /* {{{ */ data.register_type = REG_TYPE_INT16; else if (strcasecmp("Int32", tmp) == 0) data.register_type = REG_TYPE_INT32; + else if (strcasecmp("Int32LE", tmp) == 0) + data.register_type = REG_TYPE_INT32_CDAB; else if (strcasecmp("Uint16", tmp) == 0) data.register_type = REG_TYPE_UINT16; else if (strcasecmp("Uint32", tmp) == 0) data.register_type = REG_TYPE_UINT32; + else if (strcasecmp("Uint32LE", tmp) == 0) + data.register_type = REG_TYPE_UINT32_CDAB; else if (strcasecmp("Float", tmp) == 0) data.register_type = REG_TYPE_FLOAT; + else if (strcasecmp("FloatLE", tmp) == 0) + data.register_type = REG_TYPE_FLOAT_CDAB; else { ERROR("Modbus plugin: The register type \"%s\" is unknown.", tmp); status = -1; @@ -770,10 +821,8 @@ static int mb_config_set_host_address(mb_host_t *host, /* {{{ */ status = getaddrinfo(address, /* service = */ NULL, &ai_hints, &ai_list); if (status != 0) { - char errbuf[1024]; ERROR("Modbus plugin: getaddrinfo failed: %s", - (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf)) - : gai_strerror(status)); + (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status)); return status; }