X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmodbus.c;h=cad4b2cf4dd01b7b1a4e5352608823f24dfdf88d;hb=5e83b286691cd53454a3864af3b01451a8ff1946;hp=3824097c5a43b57e34c170eb4782ff756d8524a7;hpb=a04ffbda508739433df0975328100e33e7986c87;p=collectd.git diff --git a/src/modbus.c b/src/modbus.c index 3824097c..cad4b2cf 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -3,17 +3,18 @@ * Copyright (C) 2010 noris network AG * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; only version 2.1 of the License is + * applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: * Florian Forster @@ -28,6 +29,13 @@ #include +#ifndef LIBMODBUS_VERSION_CHECK +/* Assume version 2.0.3 */ +# define LEGACY_LIBMODBUS 1 +#else +/* Assume version 2.9.2 */ +#endif + #ifndef MODBUS_TCP_DEFAULT_PORT # ifdef MODBUS_TCP_PORT # define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT @@ -99,7 +107,11 @@ struct mb_host_s /* {{{ */ mb_slave_t *slaves; size_t slaves_num; +#if LEGACY_LIBMODBUS modbus_param_t connection; +#else + modbus_t *connection; +#endif _Bool is_connected; _Bool have_reconnected; }; /* }}} */ @@ -265,6 +277,8 @@ static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */ return (conv.f); } /* }}} float mb_register_to_float */ +#if LEGACY_LIBMODBUS +/* Version 2.0.3 */ static int mb_init_connection (mb_host_t *host) /* {{{ */ { int status; @@ -281,10 +295,8 @@ 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; @@ -293,10 +305,8 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */ host->node, host->port); modbus_init_tcp (&host->connection, - /* host = */ host->node); -#if 0 + /* host = */ host->node, /* port = */ host->port); -#endif status = modbus_connect (&host->connection); if (status != 0) @@ -310,6 +320,57 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */ host->have_reconnected = 1; return (0); } /* }}} int mb_init_connection */ +/* #endif LEGACY_LIBMODBUS */ + +#else /* if !LEGACY_LIBMODBUS */ +/* Version 2.9.2 */ +static int mb_init_connection (mb_host_t *host) /* {{{ */ +{ + int status; + + if (host == NULL) + return (EINVAL); + + if (host->connection != NULL) + return (0); + + /* Only reconnect once per interval. */ + if (host->have_reconnected) + return (-1); + + if ((host->port < 1) || (host->port > 65535)) + host->port = MODBUS_TCP_DEFAULT_PORT; + + DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.", + host->node, host->port); + + host->connection = modbus_new_tcp (host->node, host->port); + if (host->connection == NULL) + { + host->have_reconnected = 1; + ERROR ("Modbus plugin: Creating new Modbus/TCP object failed."); + return (-1); + } + + modbus_set_debug (host->connection, 1); + + /* We'll do the error handling ourselves. */ + modbus_set_error_recovery (host->connection, 0); + + status = modbus_connect (host->connection); + if (status != 0) + { + ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.", + host->node, host->port, status); + modbus_free (host->connection); + host->connection = NULL; + return (status); + } + + host->have_reconnected = 1; + return (0); +} /* }}} int mb_init_connection */ +#endif /* !LEGACY_LIBMODBUS */ #define CAST_TO_VALUE_T(ds,vt,raw) do { \ if ((ds)->ds[0].type == DS_TYPE_COUNTER) \ @@ -325,7 +386,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) { - int values[2]; + uint16_t values[2]; int values_num; const data_set_t *ds; int status; @@ -364,22 +425,46 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */ else values_num = 1; +#if LEGACY_LIBMODBUS + /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave + * id to each call of "read_holding_registers". */ +# define modbus_read_registers(ctx, addr, nb, dest) \ + read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest)) +#else /* if !LEGACY_LIBMODBUS */ + /* Version 2.9.2: Set the slave id once before querying the registers. */ + status = modbus_set_slave (host->connection, slave->id); + if (status != 0) + { + ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.", + slave->id, status); + return (-1); + } +#endif + for (i = 0; i < 2; i++) { - status = read_holding_registers (&host->connection, - /* slave = */ slave->id, /* start_addr = */ data->register_base, + status = modbus_read_registers (host->connection, + /* start_addr = */ data->register_base, /* num_registers = */ values_num, /* buffer = */ values); if (status > 0) break; if (host->is_connected) + { +#if LEGACY_LIBMODBUS modbus_close (&host->connection); - host->is_connected = 0; + host->is_connected = 0; +#else + modbus_close (host->connection); + modbus_free (host->connection); + host->connection = NULL; +#endif + } /* If we already tried reconnecting this round, give up. */ if (host->have_reconnected) { - ERROR ("Modbus plugin: read_holding_registers (%s) failed. " + ERROR ("Modbus plugin: modbus_read_registers (%s) failed. " "Reconnecting has already been tried. Giving up.", host->host); return (-1); } @@ -389,7 +474,7 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */ status = mb_init_connection (host); if (status != 0) { - ERROR ("Modbus plugin: read_holding_registers (%s) failed. " + ERROR ("Modbus plugin: modbus_read_registers (%s) failed. " "While trying to reconnect, connecting to \"%s\" failed. " "Giving up.", host->host, host->node); @@ -403,7 +488,7 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */ } /* for (i = 0, 1) */ DEBUG ("Modbus plugin: mb_read_data: Success! " - "read_holding_registers returned with status %i.", status); + "modbus_read_registers returned with status %i.", status); if (data->register_type == REG_TYPE_FLOAT) {