Modbus plugin error

Has anyone come across the following error when working with the Modbus plugin?

[inputs.modbus] Error in plugin: write tcp xxx.xx.x.xx:5789->xxx.xx.x.xx:502: wsasend: An existing connection was forcibly closed by the remote host.

It’s giving this error every 2nd request. I’ve configured telegraf to run every 60 sec, so every even minute it returns this error. Any advice would be greatly appreciated, thanks.

Here is the config:


  name_override  = "Server_Room"
  name= "Temp_PLC"

  ## Slave ID - addresses a MODBUS device on the bus
  ## Range: 0 - 255 [0 = broadcast; 248 - 255 = reserved]
  slave_id = 1

  ## Timeout for each request
  timeout = "1s"
  
  busy_retries = 1
  busy_retries_wait = "100ms"

  ## Maximum number of retries and the time to wait between retries
  ## when a slave-device is busy.
  # busy_retries = 0
  # busy_retries_wait = "100ms"

  # TCP - connect via Modbus/TCP
  controller = "tcp://xxx.xx.xx.xx:502"

  ## Analog Variables, Input Registers and Holding Registers
  ## measurement - the (optional) measurement name, defaults to "modbus"
  ## name        - the variable name
  ## byte_order  - the ordering of bytes
  ##  |---AB, ABCD   - Big Endian
  ##  |---BA, DCBA   - Little Endian
  ##  |---BADC       - Mid-Big Endian
  ##  |---CDAB       - Mid-Little Endian
  ## data_type   - INT16, UINT16, INT32, UINT32, INT64, UINT64,
  ##               FLOAT32-IEEE, FLOAT64-IEEE (the IEEE 754 binary representation)
  ##               FLOAT32, FIXED, UFIXED (fixed-point representation on input)
  ## scale       - the final numeric variable representation
  ## address     - variable address

  holding_registers = [
    { name = "Temp1",      byte_order = "ABCD",   data_type = "FLOAT32-IEEE", scale=1.0,   address = [10,11]},
    { name = "Temp2",       byte_order = "ABCD", data_type = "FLOAT32-IEEE", scale=1.0, address = [12,13]},
    { name = "Temp3",      byte_order = "ABCD", data_type = "FLOAT32-IEEE", scale=1.0, address = [14,15]},
    { name = "Temp4",    byte_order = "ABCD",   data_type = "FLOAT32-IEEE", scale=1.0,  address = [16,17]},
  ]

type or paste code here

type or paste code here

Since the error says the connection was closed by the remote host, it sounds like your modbusTCP device doesn’t like something about Telegraf’s requests and is shutting them down.

It may that the device supports a limited number of connections, or automatically closes connections after a certain amount of idle time, or requires a certain pause between connections. Different modbusTCP devices have different requirements so it’s difficult to have Telegraf work with all of them out of the box.

Based on your 60 second gather interval and how it shuts down every other request, it might be that that the device shuts down the connection if has been idle for some interval that is less than your gather interval. If Telegraf is trying to reuse the connection for multiple requests, that might explain why every other fails.

Telegraf has an option not to reuse the same connection but to open a new one every gather and close it when finished. You might want to try this. See https://github.com/influxdata/telegraf/tree/master/plugins/inputs/modbus#workarounds and the close_connection_after_gather setting.

You could also try reducing your gather interval. You might find that a shorter interval doesn’t trigger the idle connection shutdown, if that’s really what’s going on.