[collectd] [PATCH] add Modbus/RTU support to modbus plugin

Eric Sandeen sandeen at sandeen.net
Fri Nov 7 06:50:20 CET 2014


This allows access to a local RS-485 serial port
via the modbus plugin by specifying i.e.

  Device "/dev/ttyUSB0"
  Baudrate 38400

in the <Host> block.

For now it assumes 8N1, I don't know if there's any
reason to support less common serial configs...

Signed-off-by: Eric Sandeen <sandeen at sandeen.net>
---

This applies after my previous patch.

I realize I forgot to add config examples to the comment
block in my prior patch, feel free to add those, or I can
re-send if you like.

diff --git a/src/modbus.c b/src/modbus.c
index b90266e..926f9eb 100644
--- a/src/modbus.c
+++ b/src/modbus.c
@@ -56,6 +56,10 @@
  * <Host "name">
  *   Address "addr"
  *   Port "1234"
+ *   # Or:
+ *   # Device "/dev/ttyUSB0"
+ *   # Baudrate 38400
+ *   # (Assumes 8N1)
  *   Interval 60
  *
  *   <Slave 1>
@@ -84,6 +88,14 @@ enum mb_register_cmd_e /* {{{ */
   REG_CMD_READ_INPUT
 }; /* }}} */
 typedef enum mb_register_cmd_e mb_register_cmd_t;
+
+enum mb_conntype_e /* {{{ */
+{
+  MB_TCP,
+  MB_RTU
+}; /* }}} */
+typedef enum mb_conntype_e mb_conntype_t;
+
 struct mb_data_s;
 typedef struct mb_data_s mb_data_t;
 struct mb_data_s /* {{{ */
@@ -109,9 +121,11 @@ typedef struct mb_slave_s mb_slave_t;
 struct mb_host_s /* {{{ */
 {
   char host[DATA_MAX_NAME_LEN];
-  char node[NI_MAXHOST];
+  char node[NI_MAXHOST];	/* host or device */
   /* char service[NI_MAXSERV]; */
-  int port;
+  int port;	/* Modbus/TCP */
+  int baudrate; /* Modbus/RTU */
+  mb_conntype_t conntype;
   cdtime_t interval;
 
   mb_slave_t *slaves;
@@ -301,21 +315,33 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
   /* We'll do the error handling ourselves. */
   modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
 
-  if ((host->port < 1) || (host->port > 65535))
-    host->port = MODBUS_TCP_DEFAULT_PORT;
+  if (host->conntype == MB_TCP)
+  {
+    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);
 
-  DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
-      host->node, host->port);
+    modbus_init_tcp (&host->connection,
+        /* host = */ host->node,
+        /* port = */ host->port);
+  }
+  else
+  {
+    DEBUG ("Modbus plugin: Trying to connect to \"%s\".", host->node);
 
-  modbus_init_tcp (&host->connection,
-      /* host = */ host->node,
-      /* port = */ host->port);
+    modbus_init_rtu (&host->connection,
+       /* device = */ host->node,
+     /* baudrate = */ host->baudrate,
+                      'N', 8, 1, 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);
+        host->node, host->port ? host->port : host->baudrate, status);
     return (status);
   }
 
@@ -336,17 +362,32 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
   if (host->connection != NULL)
     return (0);
 
-  if ((host->port < 1) || (host->port > 65535))
-    host->port = MODBUS_TCP_DEFAULT_PORT;
+  if (host->conntype == MB_TCP)
+  {
+    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);
+    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->connection = modbus_new_tcp (host->node, host->port);
+    if (host->connection == NULL)
+    {
+      ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
+      return (-1);
+    }
+  }
+  else
   {
-    ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
-    return (-1);
+    DEBUG ("Modbus plugin: Trying to connect to \"%s\", baudrate %i.",
+        host->node, host->baudrate);
+
+    host->connection = modbus_new_rtu (host->node, host->baudrate, 'N', 8, 1);
+    if (host->connection == NULL)
+    {
+      ERROR ("Modbus plugin: Creating new Modbus/RTU object failed.");
+      return (-1);
+    }
   }
 
   modbus_set_debug (host->connection, 1);
@@ -358,7 +399,7 @@ static int mb_init_connection (mb_host_t *host) /* {{{ */
   if (status != 0)
   {
     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
-        host->node, host->port, status);
+        host->node, host->port ? host->port : host->baudrate, status);
     modbus_free (host->connection);
     host->connection = NULL;
     return (status);
@@ -427,7 +468,7 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
   {
     status = EBADF;
   }
-  else
+  else if (host->conntype == MB_TCP)
   {
     struct sockaddr sockaddr;
     socklen_t saddrlen = sizeof (sockaddr);
@@ -443,7 +484,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: mb_init_connection (%s/%s) failed. ",
+      ERROR ("Modbus plugin: mb_init_connection (%s, %s) failed. ",
           host->host, host->node);
       host->is_connected = 0;
       host->connection = NULL;
@@ -489,7 +530,7 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
 
   if (status != values_num)
   {
-    ERROR ("Modbus plugin: modbus_read_%sregisters (%s/%s) failed. status = %i, values_num = %i "
+    ERROR ("Modbus plugin: modbus_read_%sregisters (%s, %s) failed. status = %i, values_num = %i "
         "Giving up.", data->register_cmd == REG_CMD_READ_INPUT ? "input_" : "",
         host->host, host->node, status, values_num);
 #if LEGACY_LIBMODBUS
@@ -917,6 +958,8 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
       if (status == 0)
         status = mb_config_set_host_address (host, buffer);
+      if (status == 0)
+        host->conntype = MB_TCP;
     }
     else if (strcasecmp ("Port", child->key) == 0)
     {
@@ -924,6 +967,14 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
       if (host->port <= 0)
         status = -1;
     }
+    else if (strcasecmp ("Device", child->key) == 0)
+    {
+      status = cf_util_get_string_buffer (child, host->node, sizeof (host->node));
+      if (status == 0)
+        host->conntype = MB_RTU;
+    }
+    else if (strcasecmp ("Baudrate", child->key) == 0)
+      status = cf_util_get_int(child, &host->baudrate);
     else if (strcasecmp ("Interval", child->key) == 0)
       status = cf_util_get_cdtime (child, &host->interval);
     else if (strcasecmp ("Slave", child->key) == 0)
@@ -940,9 +991,22 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
   } /* for (i = 0; i < ci->children_num; i++) */
 
   assert (host->host[0] != 0);
-  if (host->host[0] == 0)
+  if (host->node[0] == 0)
   {
-    ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
+    ERROR ("Modbus plugin: Data block \"%s\": No address or device has been specified.",
+        host->host);
+    status = -1;
+  }
+  if (host->conntype == MB_RTU && !host->baudrate)
+  {
+    ERROR ("Modbus plugin: Data block \"%s\": No serial baudrate has been specified.",
+        host->host);
+    status = -1;
+  }
+  if ((host->conntype == MB_TCP && host->baudrate) ||
+      (host->conntype == MB_RTU && host->port))
+  {
+    ERROR ("Modbus plugin: Data block \"%s\": You've mixed up RTU and TCP options.",
         host->host);
     status = -1;
   }




More information about the collectd mailing list