zhangqian
2023-09-18 83b652cd2f1be2375d33a67366a7c359cf6e22c0
pkg/plc/modbusx/connection_manager.go
@@ -1,6 +1,7 @@
package modbusx
import (
   "apsClient/conf"
   "github.com/goburrow/modbus"
   "sync"
   "time"
@@ -37,10 +38,11 @@
   cm.connections[address] = connection
}
func (cm *ConnectionManager) CheckConnect(conn modbus.Client, timeout time.Duration) (bool, error) {
   return true, nil
func (cm *ConnectionManager) RemoveConnection(address string) {
   cm.mu.Lock()
   defer cm.mu.Unlock()
   delete(cm.connections, address)
}
func getModbusConnection(ipAddr string) modbus.Client {
   if conn, ok := connectionManager.GetConnection(ipAddr); ok {
      return conn
@@ -50,6 +52,17 @@
   return conn
}
func unsetModbusConnection(ipAddr string) {
   _, ok := connectionManager.GetConnection(ipAddr)
   if !ok {
      return
   }
   connectionManager.RemoveConnection(ipAddr)
}
func newGetModbusConnection(ipAddr string) modbus.Client {
   return modbus.TCPClient(ipAddr)
   handler := modbus.NewTCPClientHandler(ipAddr)
   handler.Timeout = 10 * time.Second
   handler.SlaveId = byte(conf.Conf.PLC.SlaveId)
   return modbus.NewClient(handler)
}