X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmodbus.c;h=3824097c5a43b57e34c170eb4782ff756d8524a7;hb=2f2744072ab144b8130b2296327fc3382e5f7f80;hp=d8e270e87828efcf0d2a01935802c56f9a342bbd;hpb=8f5a6a64dda60f8397f3c45fc6b3d0606e123d3b;p=collectd.git diff --git a/src/modbus.c b/src/modbus.c index d8e270e8..3824097c 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -24,8 +24,18 @@ #include "plugin.h" #include "configfile.h" +#include + #include +#ifndef MODBUS_TCP_DEFAULT_PORT +# ifdef MODBUS_TCP_PORT +# define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT +# else +# define MODBUS_TCP_DEFAULT_PORT 502 +# endif +#endif + /* * * RegisterBase 1234 @@ -49,17 +59,17 @@ /* * Data structures */ -enum mb_register_type_e +enum mb_register_type_e /* {{{ */ { REG_TYPE_UINT16, REG_TYPE_UINT32, REG_TYPE_FLOAT -}; +}; /* }}} */ typedef enum mb_register_type_e mb_register_type_t; struct mb_data_s; typedef struct mb_data_s mb_data_t; -struct mb_data_s +struct mb_data_s /* {{{ */ { char *name; int register_base; @@ -68,23 +78,23 @@ struct mb_data_s char instance[DATA_MAX_NAME_LEN]; mb_data_t *next; -}; +}; /* }}} */ -struct mb_slave_s +struct mb_slave_s /* {{{ */ { int id; char instance[DATA_MAX_NAME_LEN]; mb_data_t *collect; -}; +}; /* }}} */ typedef struct mb_slave_s mb_slave_t; -struct mb_host_s +struct mb_host_s /* {{{ */ { char host[DATA_MAX_NAME_LEN]; char node[NI_MAXHOST]; /* char service[NI_MAXSERV]; */ int port; - int interval; + cdtime_t interval; mb_slave_t *slaves; size_t slaves_num; @@ -92,18 +102,18 @@ struct mb_host_s modbus_param_t connection; _Bool is_connected; _Bool have_reconnected; -}; +}; /* }}} */ typedef struct mb_host_s mb_host_t; struct mb_data_group_s; typedef struct mb_data_group_s mb_data_group_t; -struct mb_data_group_s +struct mb_data_group_s /* {{{ */ { mb_data_t *registers; size_t registers_num; mb_data_group_t *next; -}; +}; /* }}} */ /* * Global variables @@ -212,12 +222,16 @@ static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */ if ((host == NULL) || (slave == NULL) || (data == NULL)) return (EINVAL); + if (host->interval <= 0) + host->interval = interval_g; + if (slave->instance[0] == 0) ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i", slave->id); vl.values = &value; vl.values_len = 1; + vl.interval = host->interval; sstrncpy (vl.host, host->host, sizeof (vl.host)); sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance)); @@ -267,8 +281,10 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */ modbus_set_debug (&host->connection, 1); +#if 0 /* We'll do the error handling ourselves. */ modbus_set_error_handling (&host->connection, NOP_ON_ERROR); +#endif if ((host->port < 1) || (host->port > 65535)) host->port = MODBUS_TCP_DEFAULT_PORT; @@ -277,8 +293,10 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */ host->node, host->port); modbus_init_tcp (&host->connection, - /* host = */ host->node, + /* host = */ host->node); +#if 0 /* port = */ host->port); +#endif status = modbus_connect (&host->connection); if (status != 0) @@ -307,7 +325,7 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */ mb_data_t *data) { - uint16_t values[2]; + int values[2]; int values_num; const data_set_t *ds; int status; @@ -753,7 +771,7 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */ status = -1; } else if (strcasecmp ("Interval", child->key) == 0) - status = cf_util_get_int (child, &host->interval); + status = cf_util_get_cdtime (child, &host->interval); else if (strcasecmp ("Slave", child->key) == 0) /* Don't set status: Gracefully continue if a slave fails. */ mb_config_add_slave (host, child); @@ -779,21 +797,18 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */ { user_data_t ud; char name[1024]; - struct timespec interval; + struct timespec interval = { 0, 0 }; ud.data = host; ud.free_func = host_free; ssnprintf (name, sizeof (name), "modbus-%s", host->host); - interval.tv_nsec = 0; - if (host->interval > 0) - interval.tv_sec = host->interval; - else - interval.tv_sec = 0; + CDTIME_T_TO_TIMESPEC (host->interval, &interval); - plugin_register_complex_read (name, mb_read, - (interval.tv_sec > 0) ? &interval : NULL, + plugin_register_complex_read (/* group = */ NULL, name, + /* callback = */ mb_read, + /* interval = */ (host->interval > 0) ? &interval : NULL, &ud); } else @@ -828,47 +843,6 @@ static int mb_config (oconfig_item_t *ci) /* {{{ */ /* ========= */ -#if 0 -static int foo (void) /* {{{ */ -{ - int status; - uint16_t values[2]; - int values_num; - - if (dev == NULL) - return (EINVAL); - - printf ("mb_read (addr = %i, float = %s);\n", register_addr, - is_float ? "true" : "false"); - - memset (values, 0, sizeof (values)); - if (is_float) - values_num = 2; - else - values_num = 1; - - status = read_holding_registers (dev->connection, - /* slave = */ 1, /* start_addr = */ register_addr, - /* num_registers = */ values_num, /* buffer = */ values); - printf ("read_coil_status returned with status %i\n", status); - if (status <= 0) - return (EAGAIN); - - if (is_float) - { - float value = mb_register_to_float (values[0], values[1]); - printf ("read_coil_status returned value %g (hi %#"PRIx16", lo %#"PRIx16")\n", - value, values[0], values[1]); - } - else - { - printf ("read_coil_status returned value %"PRIu16"\n", values[0]); - } - - return (0); -} /* }}} int foo */ -#endif - static int mb_shutdown (void) /* {{{ */ { data_free_all (data_definitions);