From 173667a4dec3109c849f4ffaf8e7d6e715d6a8e2 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 18 Jul 2020 10:36:28 +0200 Subject: [PATCH] Close modbus connection if operation fails to reestablish connection (#221) --- meter/modbus.go | 4 +++- provider/modbus.go | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/meter/modbus.go b/meter/modbus.go index 0e7a437678..e3f0773952 100644 --- a/meter/modbus.go +++ b/meter/modbus.go @@ -105,7 +105,9 @@ func (m *Modbus) floatGetter(op modbus.Operation) (float64, error) { } } - if err == nil { + if err != nil { + m.conn.Close() // close connection in case of modbus error + } else { m.log.TRACE.Printf("%+v", res) } diff --git a/provider/modbus.go b/provider/modbus.go index 1ad0ac07f6..7760bda253 100644 --- a/provider/modbus.go +++ b/provider/modbus.go @@ -124,6 +124,7 @@ func (m *Modbus) FloatGetter() (float64, error) { } if err != nil { + m.conn.Close() // close connection in case of modbus error return 0, errors.Wrap(err, "read failed") } @@ -144,7 +145,9 @@ func (m *Modbus) FloatGetter() (float64, error) { } } - if err == nil { + if err != nil { + m.conn.Close() // close connection in case of modbus error + } else { m.log.TRACE.Printf("%+v", res) }