| | |
| | | mu sync.Mutex |
| | | } |
| | | |
| | | func NewPlcConnectionManager() *ConnectionManager { |
| | | func newPlcConnectionManager() *ConnectionManager { |
| | | return &ConnectionManager{ |
| | | connections: make(map[string]plc4go.PlcConnection), |
| | | } |
| | |
| | | return conn, ok |
| | | } |
| | | |
| | | var connectionManager = NewPlcConnectionManager() |
| | | var connectionManager = newPlcConnectionManager() |
| | | |
| | | func (cm *ConnectionManager) AddConnection(address string, connection plc4go.PlcConnection) { |
| | | cm.mu.Lock() |
| | |
| | | // 创建一个上下文,并设置 3 秒超时 |
| | | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| | | defer cancel() |
| | | conn, err := NewGetModbusConnection(ctx, ipAddr) |
| | | conn, err := newGetModbusConnection(ctx, ipAddr) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | |
| | | 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) { |