| | |
| | | if code != ecode.OK { |
| | | return errors.New("请先配置PLC") |
| | | } |
| | | conn, err := plc.NewModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)) |
| | | conn, err := plc.GetModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)) |
| | | if err != nil { |
| | | return errors.New(fmt.Sprintf("连接plc失败: %v", err.Error())) |
| | | } |
| | | defer conn.Close() |
| | | |
| | | logx.Info("----------------开始下发工艺参数-----------------") |
| | | for k, v := range paramsMap { |
| | |
| | | if code != ecode.OK { |
| | | return errors.New("请先配置PLC") |
| | | } |
| | | conn, err := plc.NewModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)) |
| | | conn, err := plc.GetModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)) |
| | | if err != nil { |
| | | return errors.New(fmt.Sprintf("连接plc失败: %v", err.Error())) |
| | | } |
| | | defer conn.Close() |
| | | |
| | | logx.Info("----------------开始下发工艺参数-----------------") |
| | | var missNumbers int |
| | |
| | | "errors" |
| | | "fmt" |
| | | apiModel "github.com/apache/plc4x/plc4go/pkg/api/model" |
| | | "github.com/apache/plc4x/plc4go/spi/options" |
| | | "sync" |
| | | "time" |
| | | |
| | | plc4go "github.com/apache/plc4x/plc4go/pkg/api" |
| | | "github.com/apache/plc4x/plc4go/pkg/api/drivers" |
| | | "github.com/apache/plc4x/plc4go/pkg/api/transports" |
| | | ) |
| | | |
| | | func NewModbusConnection(ipAddr string) (plc4go.PlcConnection, error) { |
| | | // 创建驱动管理器 |
| | | driverManager := plc4go.NewPlcDriverManager() |
| | | type ConnectionManager struct { |
| | | connections map[string]plc4go.PlcConnection |
| | | mu sync.Mutex |
| | | } |
| | | |
| | | func NewPlcConnectionManager() *ConnectionManager { |
| | | return &ConnectionManager{ |
| | | connections: make(map[string]plc4go.PlcConnection), |
| | | } |
| | | } |
| | | |
| | | func (cm *ConnectionManager) GetConnection(address string) (plc4go.PlcConnection, bool) { |
| | | cm.mu.Lock() |
| | | defer cm.mu.Unlock() |
| | | |
| | | conn, ok := cm.connections[address] |
| | | return conn, ok |
| | | } |
| | | |
| | | var connectionManager = NewPlcConnectionManager() |
| | | |
| | | func (cm *ConnectionManager) AddConnection(address string, connection plc4go.PlcConnection) { |
| | | cm.mu.Lock() |
| | | defer cm.mu.Unlock() |
| | | |
| | | cm.connections[address] = connection |
| | | } |
| | | func GetModbusConnection(ipAddr string) (plc4go.PlcConnection, error) { |
| | | if conn, ok := connectionManager.GetConnection(ipAddr); ok { |
| | | if conn.IsConnected() { |
| | | return conn, nil |
| | | } |
| | | } |
| | | |
| | | // 创建驱动管理器 |
| | | option := options.WithReceiveTimeout(time.Second * 5) //五秒超时 |
| | | driverManager := plc4go.NewPlcDriverManager(option) |
| | | // 注册TCP传输 |
| | | transports.RegisterTcpTransport(driverManager) |
| | | |
| | |
| | | return nil, err |
| | | } |
| | | |
| | | return connectionResult.GetConnection(), nil |
| | | conn := connectionResult.GetConnection() |
| | | connectionManager.AddConnection(ipAddr, conn) |
| | | return conn, nil |
| | | } |
| | | func ReadHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) { |
| | | tagAddress := fmt.Sprintf("holding-register:%d:UINT", address) |
| | |
| | | } |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | |
| | | conn, err := plc.NewModbusConnection(ipAddr) |
| | | conn, err := plc.GetModbusConnection(ipAddr) |
| | | if err != nil { |
| | | logx.Errorf("GetProductProgress 连接plc失败: %v", err.Error()) |
| | | return |
| | | } |
| | | defer conn.Close() |
| | | |
| | | rawData, err := plc.ReadHoldingRegister(conn, startAddress, dataLength) |
| | | if err != nil { |
| | |
| | | } |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | |
| | | conn, err := plc.NewModbusConnection(ipAddr) |
| | | conn, err := plc.GetModbusConnection(ipAddr) |
| | | if err != nil { |
| | | logx.Errorf("GetProductProgress 连接plc失败: %v", err.Error()) |
| | | return |
| | | } |
| | | defer conn.Close() |
| | | |
| | | result, err := plc.WriteHoldingRegister(conn, startAddress, value) |
| | | if err != nil { |