From 4f9aa18d056ac850f6b6514bf06cbf542de96e45 Mon Sep 17 00:00:00 2001 From: Henry Schwanbeck Date: Thu, 26 Sep 2019 22:15:48 +0200 Subject: [PATCH 1/5] Refactored 'time_out' to 'timeout' --- plugins/inputs/modbus/README.md | 2 +- plugins/inputs/modbus/modbus.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index 87cee03d1670c..1afec2b03f5ca 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -6,7 +6,7 @@ The Modbus plugin collects Discrete Inputs, Coils, Input Registers and Holding R ```toml slave_id = 1 - time_out = "1s" + timeout = "1s" #protocol = "RTU" #TCP diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index b8436a8dc2251..3a7432258da74 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -25,7 +25,7 @@ type Modbus struct { Parity string `toml:"parity"` Stop_Bits int `toml:"stop_bits"` Slave_Id int `toml:"slave_id"` - Time_out internal.Duration `toml:"time_out"` + Timeout internal.Duration `toml:"timeout"` Discrete_Inputs []tag `toml:"discrete_inputs"` Coils []tag `toml:"coils"` Holding_Registers []tag `toml:"holding_registers"` @@ -69,7 +69,7 @@ const ( var ModbusConfig = ` slave_id = 1 - time_out = "1s" + timeout = "1s" #protocol = "RTU" #TCP @@ -145,7 +145,7 @@ func connect(m *Modbus) error { return _err } m.tcp_handler = mb.NewTCPClientHandler(host + ":" + port) - m.tcp_handler.Timeout = m.Time_out.Duration + m.tcp_handler.Timeout = m.Timeout.Duration m.tcp_handler.SlaveId = byte(m.Slave_Id) m.client = mb.NewClient(m.tcp_handler) err := m.tcp_handler.Connect() @@ -157,7 +157,7 @@ func connect(m *Modbus) error { case "file": if m.Protocol == "RTU" { m.serial_handler = mb.NewRTUClientHandler(u.Path) - m.serial_handler.Timeout = m.Time_out.Duration + m.serial_handler.Timeout = m.Timeout.Duration m.serial_handler.SlaveId = byte(m.Slave_Id) m.serial_handler.BaudRate = m.Baud_Rate m.serial_handler.DataBits = m.Data_Bits @@ -172,7 +172,7 @@ func connect(m *Modbus) error { return nil } else if m.Protocol == "ASCII" { m.ascii_handler = mb.NewASCIIClientHandler(u.Path) - m.ascii_handler.Timeout = m.Time_out.Duration + m.ascii_handler.Timeout = m.Timeout.Duration m.ascii_handler.SlaveId = byte(m.Slave_Id) m.ascii_handler.BaudRate = m.Baud_Rate m.ascii_handler.DataBits = m.Data_Bits From bd3d26b153b30a9258f611358b4a2e1afb6d59b2 Mon Sep 17 00:00:00 2001 From: Henry Schwanbeck Date: Fri, 27 Sep 2019 09:00:44 +0200 Subject: [PATCH 2/5] Refactored 'protocol' to 'transmission_mode' * The protocol is always modbus, regardless of encoding (RTU or ASCII) --- plugins/inputs/modbus/README.md | 2 +- plugins/inputs/modbus/modbus.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index 1afec2b03f5ca..e18b3fcface2a 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -7,7 +7,7 @@ The Modbus plugin collects Discrete Inputs, Coils, Input Registers and Holding R ```toml slave_id = 1 timeout = "1s" - #protocol = "RTU" + #transmission_mode = "RTU" #TCP controller = "tcp://localhost:1502" diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 3a7432258da74..89ae9481fe778 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -19,7 +19,7 @@ import ( type Modbus struct { Controller string `toml:"controller"` - Protocol string `toml:"protocol"` + Transmission_Mode string `toml:"transmission_mode"` Baud_Rate int `toml:"baud_rate"` Data_Bits int `toml:"data_bits"` Parity string `toml:"parity"` @@ -70,7 +70,7 @@ const ( var ModbusConfig = ` slave_id = 1 timeout = "1s" - #protocol = "RTU" + #transmission_mode = "RTU" #TCP controller = "tcp://localhost:1502" @@ -155,7 +155,7 @@ func connect(m *Modbus) error { m.is_connected = true return nil case "file": - if m.Protocol == "RTU" { + if m.Transmission_mode == "RTU" { m.serial_handler = mb.NewRTUClientHandler(u.Path) m.serial_handler.Timeout = m.Timeout.Duration m.serial_handler.SlaveId = byte(m.Slave_Id) @@ -170,7 +170,7 @@ func connect(m *Modbus) error { } m.is_connected = true return nil - } else if m.Protocol == "ASCII" { + } else if m.Transmission_Mode == "ASCII" { m.ascii_handler = mb.NewASCIIClientHandler(u.Path) m.ascii_handler.Timeout = m.Timeout.Duration m.ascii_handler.SlaveId = byte(m.Slave_Id) @@ -186,7 +186,7 @@ func connect(m *Modbus) error { m.is_connected = true return nil } else { - return errors.New(fmt.Sprintf("Not valid protcol [%s] - [%s] ", u.Scheme, m.Protocol)) + return errors.New(fmt.Sprintf("Not valid protcol [%s] - [%s] ", u.Scheme, m.Transmission_Mode)) } default: return errors.New("Not valid Controller") From 08a5e5f4b307cacb8700e7c1b351fa56487c569d Mon Sep 17 00:00:00 2001 From: Henry Schwanbeck Date: Fri, 27 Sep 2019 09:45:03 +0200 Subject: [PATCH 3/5] Default config uses registered MODBUS/TCP port --- plugins/inputs/modbus/README.md | 2 +- plugins/inputs/modbus/modbus.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index e18b3fcface2a..cc9176c204993 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -10,7 +10,7 @@ The Modbus plugin collects Discrete Inputs, Coils, Input Registers and Holding R #transmission_mode = "RTU" #TCP - controller = "tcp://localhost:1502" + controller = "tcp://localhost:502" #RTU #controller = "file:///dev/ttyUSB0" diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 89ae9481fe778..ef9bd73a19cc4 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -73,7 +73,7 @@ var ModbusConfig = ` #transmission_mode = "RTU" #TCP - controller = "tcp://localhost:1502" + controller = "tcp://localhost:502" #RTU #controller = "file:///dev/ttyUSB0" From d230b051a55594f77a3941322c5850c9a71d78a6 Mon Sep 17 00:00:00 2001 From: Henry Schwanbeck Date: Fri, 27 Sep 2019 10:17:22 +0200 Subject: [PATCH 4/5] Config cleanup and commentary --- plugins/inputs/modbus/README.md | 54 +++++++++++++++++----------- plugins/inputs/modbus/modbus.go | 62 ++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 44 deletions(-) diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index cc9176c204993..675a7b2fb817b 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -5,19 +5,33 @@ The Modbus plugin collects Discrete Inputs, Coils, Input Registers and Holding R ### Configuration: ```toml + ## Connection Configuration + ## + ## The module supports connections to PLCs via MODBUS/TCP or + ## via serial line communication in binary (RTU) or readable (ASCII) encoding + ## + + ## 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" - #transmission_mode = "RTU" - #TCP + # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" - #RTU + # Serial (RS485; RS232) #controller = "file:///dev/ttyUSB0" #baud_rate = 9600 #data_bits = 8 #parity = "N" #stop_bits = 1 + #transmission_mode = "RTU" + + + ## Measurements + ## ## Digital Variables, Discrete Inputs and Coils ## name - the variable name @@ -25,14 +39,14 @@ The Modbus plugin collects Discrete Inputs, Coils, Input Registers and Holding R discrete_inputs = [ { name = "Start", address = [0]}, - { name = "Stop", address = [1]}, - { name = "Reset", address = [2]}, - { name = "EmergencyStop", address = [3]}, + { name = "Stop", address = [1]}, + { name = "Reset", address = [2]}, + { name = "EmergencyStop", address = [3]}, ] coils = [ - { name = "Motor1-Run", address = [0]}, - { name = "Motor1-Jog", address = [1]}, - { name = "Motor1-Stop", address = [2]}, + { name = "Motor1-Run", address = [0]}, + { name = "Motor1-Jog", address = [1]}, + { name = "Motor1-Stop", address = [2]}, ] ## Analog Variables, Input Registers and Holding Registers @@ -43,21 +57,21 @@ The Modbus plugin collects Discrete Inputs, Coils, Input Registers and Holding R ## |---BADC - Mid-Big Endian ## |---CDAB - Mid-Little Endian ## data_type - UINT16, INT16, INT32, UINT32, FLOAT32, FLOAT32-IEEE (the IEEE 754 binary representation) - ## scale - the final numeric variable representation + ## scale - the final numeric variable representation ## address - variable address holding_registers = [ - { name = "PowerFactor", byte_order = "AB", data_type = "FLOAT32", scale="0.01" , address = [8]}, - { name = "Voltage", byte_order = "AB", data_type = "FLOAT32", scale="0.1" , address = [0]}, - { name = "Energy", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001" , address = [5,6]}, - { name = "Current", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001" , address = [1, 2]}, - { name = "Frequency", byte_order = "AB", data_type = "FLOAT32", scale="0.1" , address = [7]}, - { name = "Power", byte_order = "ABCD", data_type = "FLOAT32", scale="0.1" , address = [3,4]}, - ] + { name = "PowerFactor", byte_order = "AB", data_type = "FLOAT32", scale="0.01", address = [8]}, + { name = "Voltage", byte_order = "AB", data_type = "FLOAT32", scale="0.1", address = [0]}, + { name = "Energy", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001", address = [5,6]}, + { name = "Current", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001", address = [1, 2]}, + { name = "Frequency", byte_order = "AB", data_type = "FLOAT32", scale="0.1", address = [7]}, + { name = "Power", byte_order = "ABCD", data_type = "FLOAT32", scale="0.1", address = [3,4]}, + ] input_registers = [ - { name = "TankLevel", byte_order = "AB", data_type = "INT16", scale="1" , address = [0]}, - { name = "TankPH", byte_order = "AB", data_type = "INT16", scale="1" , address = [1]}, - { name = "Pump1-Speed", byte_order = "ABCD", data_type = "INT32", scale="1" , address = [3,4]}, + { name = "TankLevel", byte_order = "AB", data_type = "INT16", scale="1", address = [0]}, + { name = "TankPH", byte_order = "AB", data_type = "INT16", scale="1", address = [1]}, + { name = "Pump1-Speed", byte_order = "ABCD", data_type = "INT32", scale="1", address = [3,4]}, ] ``` ### Example Output: diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index ef9bd73a19cc4..0b74bf5957fd0 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -68,59 +68,73 @@ const ( ) var ModbusConfig = ` + ## Connection Configuration + ## + ## The module supports connections to PLCs via MODBUS/TCP or + ## via serial line communication in binary (RTU) or readable (ASCII) encoding + ## + + ## 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" - #transmission_mode = "RTU" - #TCP + # TCP - connect via Modbus/TCP controller = "tcp://localhost:502" - #RTU + # Serial (RS485; RS232) #controller = "file:///dev/ttyUSB0" #baud_rate = 9600 #data_bits = 8 #parity = "N" #stop_bits = 1 + #transmission_mode = "RTU" + + + ## Measurements + ## ## Digital Variables, Discrete Inputs and Coils ## name - the variable name ## address - variable address discrete_inputs = [ - { name = "Start", address = [0]}, - { name = "Stop", address = [1]}, - { name = "Reset", address = [2]}, - { name = "EmergencyStop", address = [3]}, + { name = "Start", address = [0]}, + { name = "Stop", address = [1]}, + { name = "Reset", address = [2]}, + { name = "EmergencyStop", address = [3]}, ] coils = [ - { name = "Motor1-Run", address = [0]}, - { name = "Motor1-Jog", address = [1]}, - { name = "Motor1-Stop", address = [2]}, - ] + { name = "Motor1-Run", address = [0]}, + { name = "Motor1-Jog", address = [1]}, + { name = "Motor1-Stop", address = [2]}, + ] ## Analog Variables, Input Registers and Holding Registers - ## name - the variable name - ## byte_order - the ordering of bytes + ## 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 - UINT16, INT16, INT32, UINT32, FLOAT32, FLOAT32-IEEE (the IEEE 754 binary representation) - ## scale - the final numeric variable representation + ## scale - the final numeric variable representation ## address - variable address holding_registers = [ - { name = "PowerFactor", byte_order = "AB", data_type = "FLOAT32", scale="0.01" , address = [8]}, - { name = "Voltage", byte_order = "AB", data_type = "FLOAT32", scale="0.1" , address = [0]}, - { name = "Energy", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001" , address = [5,6]}, - { name = "Current", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001" , address = [1, 2]}, - { name = "Frequency", byte_order = "AB", data_type = "FLOAT32", scale="0.1" , address = [7]}, - { name = "Power", byte_order = "ABCD", data_type = "FLOAT32", scale="0.1" , address = [3,4]}, - ] + { name = "PowerFactor", byte_order = "AB", data_type = "FLOAT32", scale="0.01", address = [8]}, + { name = "Voltage", byte_order = "AB", data_type = "FLOAT32", scale="0.1", address = [0]}, + { name = "Energy", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001", address = [5,6]}, + { name = "Current", byte_order = "ABCD", data_type = "FLOAT32", scale="0.001", address = [1, 2]}, + { name = "Frequency", byte_order = "AB", data_type = "FLOAT32", scale="0.1", address = [7]}, + { name = "Power", byte_order = "ABCD", data_type = "FLOAT32", scale="0.1", address = [3,4]}, + ] input_registers = [ - { name = "TankLevel", byte_order = "AB", data_type = "INT16", scale="1" , address = [0]}, - { name = "TankPH", byte_order = "AB", data_type = "INT16", scale="1" , address = [1]}, - { name = "Pump1-Speed", byte_order = "ABCD", data_type = "INT32", scale="1" , address = [3,4]}, + { name = "TankLevel", byte_order = "AB", data_type = "INT16", scale="1", address = [0]}, + { name = "TankPH", byte_order = "AB", data_type = "INT16", scale="1", address = [1]}, + { name = "Pump1-Speed", byte_order = "ABCD", data_type = "INT32", scale="1", address = [3,4]}, ] ` From fceaa69d2ea09192bc54972545fbbe716de3c27d Mon Sep 17 00:00:00 2001 From: Henry Schwanbeck Date: Fri, 27 Sep 2019 12:48:33 +0200 Subject: [PATCH 5/5] Corrected typo --- plugins/inputs/modbus/modbus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 0b74bf5957fd0..b2ac2cc836276 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -169,7 +169,7 @@ func connect(m *Modbus) error { m.is_connected = true return nil case "file": - if m.Transmission_mode == "RTU" { + if m.Transmission_Mode == "RTU" { m.serial_handler = mb.NewRTUClientHandler(u.Path) m.serial_handler.Timeout = m.Timeout.Duration m.serial_handler.SlaveId = byte(m.Slave_Id)