| | |
| | | "errors" |
| | | "fmt" |
| | | "github.com/spf13/cast" |
| | | "sync/atomic" |
| | | "sync" |
| | | ) |
| | | |
| | | // 串口不支持并行读写,所以需要加个锁,防止timeout和资源不可用报错 |
| | | var lock sync.Mutex |
| | | |
| | | func PlcWrite(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType, channel int32, value interface{}) (err error) { |
| | | var ( |
| | |
| | | } |
| | | |
| | | func PlcWriteDirect(plcConfig *model.DevicePlc, address int, value interface{}) (err error) { |
| | | lock.Lock() |
| | | defer lock.Unlock() |
| | | var ( |
| | | ipAddr string |
| | | ) |
| | | defer dealErr(err) |
| | | defer func() { |
| | | dealErr(err) |
| | | }() |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP { |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | err = WriteByModbusTCP(ipAddr, address, value) |
| | |
| | | } |
| | | |
| | | func PlcReadDirect(plcConfig *model.DevicePlc, address int, dataLength int, valueType constvar.PlcStartAddressValueType) (val interface{}, err error) { |
| | | lock.Lock() |
| | | defer lock.Unlock() |
| | | var ( |
| | | ipAddr string |
| | | ) |
| | | defer dealErr(err) |
| | | defer func() { |
| | | dealErr(err) |
| | | }() |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP || plcConfig.Method == constvar.PlcMethodModbusRTU { |
| | | var value []byte |
| | | if plcConfig.Method == constvar.PlcMethodModbusTCP { |
| | |
| | | switch valueType { |
| | | case constvar.PlcStartAddressValueTypeString: |
| | | return string(value), nil |
| | | case constvar.PlcStartAddressValueTypeInt: |
| | | case constvar.PlcStartAddressValueTypeInt32: |
| | | if len(value) == 2 { |
| | | val = binary.BigEndian.Uint16(value) |
| | | } else if len(value) == 4 { |
| | | val = binary.BigEndian.Uint32(value) |
| | | } else { |
| | | logx.Errorf("plc read get an unknown int value: %v, address:%v", value, address) |
| | | return nil, errors.New(fmt.Sprintf("unknown int value:%v", value)) |
| | | } |
| | | default: |
| | | if len(value) == 2 { |
| | | val = binary.BigEndian.Uint16(value) |
| | | } else if len(value) == 4 { |
| | |
| | | return nil, errors.New(fmt.Sprintf("unknown int value:%v", value)) |
| | | } |
| | | } |
| | | logx.Infof("plc read ok, address: %v, result: %v, dataLength: %v, valueLength: %v", address, val, dataLength, len(value)) |
| | | logx.Infof("plc read ok, address: %v, result: %v, dataLength: %v, value: %v", address, val, dataLength, value) |
| | | return val, nil |
| | | } else if plcConfig.Method == constvar.PlcMethodSerial { |
| | | ipAddr = conf.Conf.Services.Serial |
| | |
| | | } |
| | | } |
| | | |
| | | var connectionStatus atomic.Bool |
| | | |
| | | var failureRemainingOpportunities atomic.Int64 |
| | | var connectionStatus sync.Map |
| | | |
| | | const ( |
| | | defaultFailureRemainingOpportunities = 20 |
| | | ) |
| | | |
| | | func IsConnect() bool { |
| | | return connectionStatus.Load() |
| | | val, ok := connectionStatus.Load(conf.Conf.CurrentDeviceID) |
| | | if !ok { |
| | | return false |
| | | } |
| | | failureRemainingOpportunities := val.(int) |
| | | return failureRemainingOpportunities > 0 |
| | | } |
| | | |
| | | func FailureRemainingOpportunitiesDecr() { |
| | | newValue := failureRemainingOpportunities.Add(-1) |
| | | if newValue <= 0 { |
| | | connectionStatus.Store(false) |
| | | val, ok := connectionStatus.Load(conf.Conf.CurrentDeviceID) |
| | | if !ok { |
| | | return |
| | | } |
| | | failureRemainingOpportunities := val.(int) |
| | | if failureRemainingOpportunities > 0 { |
| | | failureRemainingOpportunities-- |
| | | } |
| | | connectionStatus.Store(conf.Conf.CurrentDeviceID, failureRemainingOpportunities) |
| | | return |
| | | } |
| | | |
| | | func FailureRemainingOpportunitiesReset() { |
| | | if failureRemainingOpportunities.Load() < defaultFailureRemainingOpportunities { |
| | | failureRemainingOpportunities.Store(defaultFailureRemainingOpportunities) |
| | | } |
| | | if connectionStatus.Load() == false { |
| | | connectionStatus.Store(true) |
| | | val, ok := connectionStatus.Load(conf.Conf.CurrentDeviceID) |
| | | if !ok || val.(int) < defaultFailureRemainingOpportunities { |
| | | connectionStatus.Store(conf.Conf.CurrentDeviceID, defaultFailureRemainingOpportunities) |
| | | return |
| | | } |
| | | return |
| | | } |