From 23a92b7802357da7650c7362fb4dd1b029123aa5 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 18 九月 2023 19:28:05 +0800 Subject: [PATCH] 新库使用短连接 --- pkg/plc/modbusx/connection_manager.go | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-) diff --git a/pkg/plc/modbusx/connection_manager.go b/pkg/plc/modbusx/connection_manager.go index 0aef49b..ae0b377 100644 --- a/pkg/plc/modbusx/connection_manager.go +++ b/pkg/plc/modbusx/connection_manager.go @@ -12,6 +12,8 @@ mu sync.Mutex } +var handler *modbus.TCPClientHandler + func newPlcConnectionManager() *ConnectionManager { return &ConnectionManager{ connections: make(map[string]modbus.Client), @@ -58,10 +60,13 @@ return } connectionManager.RemoveConnection(ipAddr) + if handler != nil { + handler.Close() + } } func newGetModbusConnection(ipAddr string) modbus.Client { - handler := modbus.NewTCPClientHandler(ipAddr) + handler = modbus.NewTCPClientHandler(ipAddr) handler.Timeout = 10 * time.Second handler.SlaveId = byte(conf.Conf.PLC.SlaveId) return modbus.NewClient(handler) -- Gitblit v1.8.0