| | |
| | | "apsClient/model" |
| | | "apsClient/pkg/logx" |
| | | "apsClient/pkg/plc" |
| | | "apsClient/pkg/plc/modbusx" |
| | | "apsClient/pkg/plccom" |
| | | "encoding/binary" |
| | | "errors" |
| | |
| | | |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP { |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | conn, err := plc.GetModbusConnection(ipAddr) |
| | | if err != nil { |
| | | logx.Errorf("plc write failed, 连接plc失败: %v", err.Error()) |
| | | plcConfig.CurrentErr = err |
| | | return PlcWrite(plcConfig, fieldType, position, value) |
| | | } |
| | | |
| | | result, err := plc.WriteHoldingRegister(conn, startAddress, value) |
| | | //conn, err := plc.GetModbusConnection(ipAddr) |
| | | //if err != nil { |
| | | // logx.Errorf("plc write failed, 连接plc失败: %v", err.Error()) |
| | | // plcConfig.CurrentErr = err |
| | | // return PlcWrite(plcConfig, fieldType, position, value) |
| | | //} |
| | | // |
| | | //result, err := plc.WriteHoldingRegister(conn, startAddress, value) |
| | | err = modbusx.Write(ipAddr, uint16(startAddress), value) |
| | | if err != nil { |
| | | logx.Errorf("plc write failed, address: %v, value: %v, err: %v", startAddress, value, err.Error()) |
| | | plcConfig.CurrentErr = err |
| | | return PlcWrite(plcConfig, fieldType, position, value) |
| | | } |
| | | logx.Infof("plc write ok, address: %v, value: %v, result: %v", startAddress, value, result) |
| | | logx.Infof("plc write ok, address: %v, value: %v", startAddress, value) |
| | | } else if plcConfig.Method == constvar.PlcMethodSerial { |
| | | ipAddr = conf.Conf.Services.Serial |
| | | if ipAddr == "" { |
| | |
| | | ) |
| | | |
| | | if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes { |
| | | logx.Errorf("plc write try time beyond max try times, err: %v", plcConfig.CurrentErr) |
| | | return plcConfig.CurrentErr |
| | | } |
| | | plcConfig.CurrentTryTimes++ |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP { |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | conn, err := plc.GetModbusConnection(ipAddr) |
| | | if err != nil { |
| | | logx.Errorf("plc write failed, 连接plc失败: %v", err.Error()) |
| | | plcConfig.CurrentErr = err |
| | | return PlcWriteDirect(plcConfig, address, value) |
| | | } |
| | | result, err := plc.WriteHoldingRegister(conn, address, value) |
| | | //conn, err := plc.GetModbusConnection(ipAddr) |
| | | //if err != nil { |
| | | // logx.Errorf("plc write failed, 连接plc失败: %v", err.Error()) |
| | | // plcConfig.CurrentErr = err |
| | | // return PlcWriteDirect(plcConfig, address, value) |
| | | //} |
| | | //result, err := plc.WriteHoldingRegister(conn, address, value) |
| | | |
| | | err = modbusx.Write(ipAddr, uint16(address), value) |
| | | if err != nil { |
| | | logx.Errorf("plc write failed, address: %v, value: %v, err: %v", address, value, err.Error()) |
| | | plcConfig.CurrentErr = err |
| | | return PlcWriteDirect(plcConfig, address, value) |
| | | } |
| | | logx.Infof("plc write ok, address: %v, value: %v, result: %v", address, value, result) |
| | | logx.Infof("plc write ok, address: %v, value: %v", address, value) |
| | | } else if plcConfig.Method == constvar.PlcMethodSerial { |
| | | ipAddr = conf.Conf.Services.Serial |
| | | if ipAddr == "" { |
| | |
| | | ) |
| | | |
| | | if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes { |
| | | logx.Errorf("plc read try time beyond max try times, err:%v", plcConfig.CurrentErr) |
| | | logx.Errorf("plc read try time beyond max try times, err: %v", plcConfig.CurrentErr) |
| | | return nil, plcConfig.CurrentErr |
| | | } |
| | | plcConfig.CurrentTryTimes++ |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP { |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | conn, err := plc.GetModbusConnection(ipAddr) |
| | | if err != nil { |
| | | plcConfig.CurrentErr = err |
| | | return PlcReadDirect(plcConfig, address, dataLength, valueType) |
| | | } |
| | | value, err := plc.ReadHoldingRegister(conn, address, dataLength) |
| | | //conn, err := plc.GetModbusConnection(ipAddr) |
| | | //if err != nil { |
| | | // plcConfig.CurrentErr = err |
| | | // return PlcReadDirect(plcConfig, address, dataLength, valueType) |
| | | //} |
| | | //value, err := plc.ReadHoldingRegister(conn, address, dataLength) |
| | | value, err := modbusx.Read(ipAddr, uint16(address), uint16(dataLength)) |
| | | if err != nil { |
| | | plcConfig.CurrentErr = err |
| | | return PlcReadDirect(plcConfig, address, dataLength, valueType) |