| | |
| | | package service |
| | | |
| | | import ( |
| | | "apsClient/conf" |
| | | "apsClient/constvar" |
| | | "apsClient/model" |
| | | "apsClient/pkg/logx" |
| | | "apsClient/pkg/plc" |
| | | "apsClient/pkg/plccom" |
| | | "encoding/binary" |
| | | "errors" |
| | | "fmt" |
| | | "github.com/spf13/cast" |
| | | ) |
| | | |
| | | func PlcRead(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType) (val interface{}, err error) { |
| | |
| | | dataLength = pc.Length |
| | | } |
| | | } |
| | | |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP { |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | |
| | | conn, err := plc.GetModbusConnection(ipAddr) |
| | | if err != nil { |
| | | logx.Errorf("PlcRead 连接plc失败: %v", err.Error()) |
| | | return |
| | | return nil, err |
| | | } |
| | | |
| | | rawData, err := plc.ReadHoldingRegister(conn, startAddress, dataLength) |
| | | if err != nil { |
| | | logx.Errorf("PlcRead 获取plc数据失败: %v", err.Error()) |
| | | return |
| | | return nil, err |
| | | } |
| | | switch valueType { |
| | | case constvar.PlcStartAddressValueTypeString: |
| | |
| | | } |
| | | } |
| | | return nil, errors.New("undefined value type") |
| | | } else if plcConfig.Method == constvar.PlcMethodSerial { |
| | | ipAddr = conf.Conf.Services.Serial |
| | | if ipAddr == "" { |
| | | return nil, errors.New("conf.Conf.Services.Serial config not set yet") |
| | | } |
| | | label := fmt.Sprintf("D%d", startAddress) |
| | | return plccom.ReadPLC(plccom.DeviceTypeMitsubishi, ipAddr, label) |
| | | } |
| | | return nil, errors.New("interface type not support") |
| | | } |
| | | |
| | | func PlcWrite(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType, value interface{}) (err error) { |
| | |
| | | startAddress = pc.StartAddress |
| | | } |
| | | } |
| | | |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP { |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | conn, err := plc.GetModbusConnection(ipAddr) |
| | | if err != nil { |
| | |
| | | return PlcWrite(plcConfig, fieldType, value) |
| | | } |
| | | logx.Infof("plc write ok, address: %v, value: %v, result: %v", startAddress, value, result) |
| | | } else if plcConfig.Method == constvar.PlcMethodSerial { |
| | | ipAddr = conf.Conf.Services.Serial |
| | | if ipAddr == "" { |
| | | return errors.New("conf.Conf.Services.Serial config not set yet") |
| | | } |
| | | label := fmt.Sprintf("D%d", startAddress) |
| | | return plccom.WritePLC(plccom.DeviceTypeMitsubishi, ipAddr, label, cast.ToInt(value)) |
| | | } |
| | | return |
| | | } |
| | | |
| | | func PlcWriteDirect(plcConfig *model.DevicePlc, address int, value interface{}) (err error) { |
| | | var ( |
| | | ipAddr string |
| | | ) |
| | | |
| | | if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes { |
| | | 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) |
| | | 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) |
| | | } else if plcConfig.Method == constvar.PlcMethodSerial { |
| | | ipAddr = conf.Conf.Services.Serial |
| | | if ipAddr == "" { |
| | | return errors.New("conf.Conf.Services.Serial config not set yet") |
| | | } |
| | | label := fmt.Sprintf("D%d", address) |
| | | return plccom.WritePLC(plccom.DeviceTypeMitsubishi, ipAddr, label, cast.ToInt(value)) |
| | | } |
| | | return |
| | | } |