1 From a00ab52931a587cf29c53a945e9295b4d7fe41ba Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Thu, 28 Mar 2019 01:52:04 +0100
4 Subject: [PATCH] Add support for RS485 to modbus plugin
6 Allow setting up RS485 mode for Modbus-RTU
8 src/collectd.conf.pod | 6 +++++
9 src/modbus.c | 55 +++++++++++++++++++++++++++++++++++++++----
10 2 files changed, 57 insertions(+), 4 deletions(-)
12 --- a/src/collectd.conf.pod
13 +++ b/src/collectd.conf.pod
14 @@ -4234,6 +4234,12 @@ For Modbus/RTU, specifies the path to th
15 For Modbus/RTU, specifies the baud rate of the serial device.
16 Note, connections currently support only 8/N/1.
18 +=item B<UARTType> I<UARTType>
20 +For Modbus/RTU, specifies the type of the serial device.
21 +RS232, RS422 and RS485 are supported. Defaults to RS232.
22 +Available only on Linux systems with libmodbus>=2.9.4.
24 =item B<Interval> I<Interval>
26 Sets the interval (in seconds) in which the values will be collected from this
29 @@ -95,6 +95,12 @@ enum mb_conntype_e /* {{{ */
30 MBCONN_RTU }; /* }}} */
31 typedef enum mb_conntype_e mb_conntype_t;
33 +enum mb_uarttype_e /* {{{ */
36 + UARTTYPE_RS485 }; /* }}} */
37 +typedef enum mb_uarttype_e mb_uarttype_t;
40 typedef struct mb_data_s mb_data_t;
41 struct mb_data_s /* {{{ */
42 @@ -124,8 +130,9 @@ struct mb_host_s /* {{{ */
43 char host[DATA_MAX_NAME_LEN];
44 char node[NI_MAXHOST]; /* TCP hostname or RTU serial device */
45 /* char service[NI_MAXSERV]; */
46 - int port; /* for Modbus/TCP */
47 - int baudrate; /* for Modbus/RTU */
48 + int port; /* for Modbus/TCP */
49 + int baudrate; /* for Modbus/RTU */
50 + mb_uarttype_t uarttype; /* UART type for Modbus/RTU */
51 mb_conntype_t conntype;
54 @@ -390,6 +397,22 @@ static int mb_init_connection(mb_host_t
58 +#if defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4)
59 + switch (host->uarttype) {
60 + case UARTTYPE_RS485:
61 + if (modbus_rtu_set_serial_mode(host->connection, MODBUS_RTU_RS485))
62 + DEBUG("Modbus plugin: Setting RS485 mode failed.");
64 + case UARTTYPE_RS422:
65 + /* libmodbus doesn't say anything about full-duplex symmetric RS422 UART */
67 + case UARTTYPE_RS232:
70 + DEBUG("Modbus plugin: Invalid UART type!.");
72 +#endif /* defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4) */
75 } /* }}} int mb_init_connection */
76 #endif /* !LEGACY_LIBMODBUS */
77 @@ -951,11 +974,35 @@ static int mb_config_add_host(oconfig_it
79 } else if (strcasecmp("Device", child->key) == 0) {
80 status = cf_util_get_string_buffer(child, host->node, sizeof(host->node));
83 host->conntype = MBCONN_RTU;
84 + host->uarttype = UARTTYPE_RS232;
86 } else if (strcasecmp("Baudrate", child->key) == 0)
87 status = cf_util_get_int(child, &host->baudrate);
88 - else if (strcasecmp("Interval", child->key) == 0)
89 + else if (strcasecmp("UARTType", child->key) == 0) {
90 +#if defined(linux) && !LEGACY_LIBMODBUS && LIBMODBUS_VERSION_CHECK(2, 9, 4)
91 + char buffer[NI_MAXHOST];
92 + status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
95 + if (strncmp(buffer, "RS485", 6) == 0)
96 + host->uarttype = UARTTYPE_RS485;
97 + else if (strncmp(buffer, "RS422", 6) == 0)
98 + host->uarttype = UARTTYPE_RS422;
99 + else if (strncmp(buffer, "RS232", 6) == 0)
100 + host->uarttype = UARTTYPE_RS232;
102 + ERROR("Modbus plugin: The UARTType \"%s\" is unknown.", buffer);
107 + ERROR("Modbus plugin: Option `UARTType' not supported. Please "
108 + "upgrade libmodbus to at least 2.9.4");
111 + } else if (strcasecmp("Interval", child->key) == 0)
112 status = cf_util_get_cdtime(child, &host->interval);
113 else if (strcasecmp("Slave", child->key) == 0)
114 /* Don't set status: Gracefully continue if a slave fails. */