| | |
| | | mu sync.Mutex |
| | | } |
| | | |
| | | func NewPlcConnectionManager() *ConnectionManager { |
| | | func newPlcConnectionManager() *ConnectionManager { |
| | | return &ConnectionManager{ |
| | | connections: make(map[string]plc4go.PlcConnection), |
| | | } |
| | |
| | | defer cm.mu.Unlock() |
| | | |
| | | conn, ok := cm.connections[address] |
| | | return conn, ok |
| | | if !ok { |
| | | return nil, false |
| | | } |
| | | //if ok, _ := cm.CheckConnect(conn, time.Second); !ok { |
| | | // conn.Close() |
| | | //} |
| | | |
| | | return conn, true |
| | | } |
| | | |
| | | var connectionManager = NewPlcConnectionManager() |
| | | var connectionManager = newPlcConnectionManager() |
| | | |
| | | func (cm *ConnectionManager) AddConnection(address string, connection plc4go.PlcConnection) { |
| | | cm.mu.Lock() |
| | |
| | | |
| | | cm.connections[address] = connection |
| | | } |
| | | |
| | | func (cm *ConnectionManager) CheckConnect(conn plc4go.PlcConnection, timeout time.Duration) (bool, error) { |
| | | pingCh := conn.Ping() |
| | | timer := time.NewTimer(timeout) |
| | | |
| | | select { |
| | | case err := <-pingCh: |
| | | if err == nil { |
| | | return true, nil |
| | | } |
| | | return false, err.GetErr() |
| | | case <-timer.C: |
| | | return false, fmt.Errorf("connection timed out after %s", timeout) |
| | | } |
| | | } |
| | | |
| | | func GetModbusConnection(ipAddr string) (plc4go.PlcConnection, error) { |
| | | if conn, ok := connectionManager.GetConnection(ipAddr); ok { |
| | | if conn.IsConnected() { |
| | | return conn, nil |
| | | } |
| | | return conn, nil |
| | | } |
| | | // 创建一个上下文,并设置 3 秒超时 |
| | | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| | | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| | | defer cancel() |
| | | conn, err := NewGetModbusConnection(ctx, ipAddr) |
| | | conn, err := newGetModbusConnection(ctx, ipAddr) |
| | | if err != nil { |
| | | logx.Errorf("new modbus connection err: %v", err.Error()) |
| | | return nil, err |
| | | } |
| | | connectionManager.AddConnection(ipAddr, conn) |
| | | return conn, nil |
| | | } |
| | | |
| | | func NewGetModbusConnection(ctx context.Context, ipAddr string) (plc4go.PlcConnection, error) { |
| | | func newGetModbusConnection(ctx context.Context, ipAddr string) (plc4go.PlcConnection, error) { |
| | | // 创建驱动管理器 |
| | | driverManager := plc4go.NewPlcDriverManager() |
| | | // 注册TCP传输 |
| | |
| | | } |
| | | } |
| | | |
| | | func ReadHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) { |
| | | func readHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) { |
| | | tag := fmt.Sprintf("tag:%v", address) |
| | | tagAddress := fmt.Sprintf("holding-register:%d:UINT", address) |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | func ReadHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) { |
| | | func readHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) { |
| | | tag := fmt.Sprintf("tag:%v:%v", address, length) |
| | | tagAddress := fmt.Sprintf("holding-register:%d:UINT[%d]", address, length) |
| | | |
| | |
| | | |
| | | func ReadHoldingRegister(connection plc4go.PlcConnection, address, length int) ([]byte, error) { |
| | | if length > 1 { |
| | | return ReadHoldingRegisterList(connection, address, length) |
| | | return readHoldingRegisterList(connection, address, length) |
| | | } |
| | | |
| | | return ReadHoldingRegisterSingle(connection, address) |
| | | return readHoldingRegisterSingle(connection, address) |
| | | } |
| | | |
| | | func WriteHoldingRegister(connection plc4go.PlcConnection, address int, value any) (string, error) { |