| | |
| | | if !ok { |
| | | return nil, false |
| | | } |
| | | connOK, err := cm.CheckConnect(conn, time.Second*1) |
| | | if err != nil { |
| | | logx.Errorf("check plc conn err: %v", err.Error()) |
| | | return nil, false |
| | | } |
| | | if connOK { |
| | | return conn, true |
| | | } |
| | | return nil, false |
| | | //if ok, _ := cm.CheckConnect(conn, time.Second); !ok { |
| | | // conn.Close() |
| | | //} |
| | | |
| | | return conn, true |
| | | } |
| | | |
| | | var connectionManager = newPlcConnectionManager() |
| | |
| | | |
| | | func GetModbusConnection(ipAddr string) (plc4go.PlcConnection, error) { |
| | | if conn, ok := connectionManager.GetConnection(ipAddr); ok { |
| | | time.Sleep(time.Second * 1) |
| | | return conn, nil |
| | | } |
| | | // 创建一个上下文,并设置 3 秒超时 |
| | |
| | | return nil, err |
| | | } |
| | | connectionManager.AddConnection(ipAddr, conn) |
| | | time.Sleep(time.Second * 1) |
| | | return conn, nil |
| | | } |
| | | |