From efec800478214f3a412f879d2b51c2968515995d Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期六, 25 十一月 2023 17:04:25 +0800 Subject: [PATCH] plc断开连接时获取工序最后一次报工数量作为加工数 --- service/plc.go | 21 +++++++++++++++++---- 1 files changed, 17 insertions(+), 4 deletions(-) diff --git a/service/plc.go b/service/plc.go index f4428ab..ac23286 100644 --- a/service/plc.go +++ b/service/plc.go @@ -45,7 +45,9 @@ 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) @@ -81,7 +83,9 @@ 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 { @@ -100,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 { @@ -112,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 -- Gitblit v1.8.0