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 |   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