From 02a84fb6fb2a39bfe7fc5cf6c0137bbf231b17fe Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 09 十一月 2023 14:14:17 +0800 Subject: [PATCH] 设备列表返回是否设置工艺参数字段 --- service/plc.go | 49 +++++++++++++++++++++++++++++-------------------- 1 files changed, 29 insertions(+), 20 deletions(-) diff --git a/service/plc.go b/service/plc.go index 68258b8..ac23286 100644 --- a/service/plc.go +++ b/service/plc.go @@ -14,8 +14,12 @@ "errors" "fmt" "github.com/spf13/cast" + "sync" "sync/atomic" ) + +// 涓插彛涓嶆敮鎸佸苟琛岃鍐欙紝鎵�浠ラ渶瑕佸姞涓攣锛岄槻姝imeout鍜岃祫婧愪笉鍙敤鎶ラ敊 +var lock sync.Mutex func PlcWrite(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType, channel int32, value interface{}) (err error) { var ( @@ -36,10 +40,14 @@ } 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) @@ -70,10 +78,14 @@ } 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 { @@ -92,7 +104,16 @@ 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 { @@ -104,7 +125,7 @@ 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 @@ -112,28 +133,20 @@ return nil, errors.New("conf.Conf.Services.Serial config not set yet") } label := fmt.Sprintf("D%d", address) - return plccom.ReadPLC(plccom.DeviceTypeMitsubishi, ipAddr, label) + return plccom.ReadPLC(plccom.DeviceTypeMitsubishi, ipAddr, label, dataLength) } return } func ReadByModbusTCP(ipAddr string, address, length int) ([]byte, error) { if conf.Conf.PLC.Package == constvar.PlcPackageApache { - newLength := length / 2 - if newLength == 0 { - newLength = 1 - } - return apacheplc4x.ReadHoldingRegister(ipAddr, address, newLength) + return apacheplc4x.ReadHoldingRegister(ipAddr, address, length) } else if conf.Conf.PLC.Package == constvar.PlcPackageApacheLongConnection { conn, err := plc.GetModbusConnection(ipAddr) if err != nil { return nil, err } - newLength := length / 2 - if newLength == 0 { - newLength = 1 - } - return plc.ReadHoldingRegister(conn, address, newLength) + return plc.ReadHoldingRegister(conn, address, length) } else { return modbusx.Read(ipAddr, uint16(address), uint16(length)) } @@ -164,11 +177,7 @@ Parity: plcConfig.Parity, } if conf.Conf.PLC.Package == constvar.PlcPackageApache { - newLength := length / 2 - if newLength == 0 { - newLength = 1 - } - return apacheplc4x.ReadHoldingRegisterByRTU(rtuConfig, address, newLength) + return apacheplc4x.ReadHoldingRegisterByRTU(rtuConfig, address, length) } else { return modbusx.ReadByRTU(rtuConfig, uint16(address), uint16(length)) } -- Gitblit v1.8.0