From 8324f872ef3a4d0c978a9b1d062800c6a1701c12 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 01 十二月 2023 09:58:17 +0800
Subject: [PATCH] fix

---
 pkg/plccom/plccom.go |   34 +++++++++++++++++++++++++++++-----
 1 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/pkg/plccom/plccom.go b/pkg/plccom/plccom.go
index f066eef..43e2b9b 100644
--- a/pkg/plccom/plccom.go
+++ b/pkg/plccom/plccom.go
@@ -19,11 +19,18 @@
 	// MakeReadProtocol 鍒涘缓璇诲彇鏁版嵁鐨勫崗璁�
 	MakeReadProtocol(addr string) []byte
 
+	// MakeReadProtocolBits 鍒涘缓璇诲彇鏁版嵁鐨勫崗璁�
+	MakeReadProtocolBits(bits int, addr string) []byte
+
 	// MakeWriteProtocol 鍒涘缓鍐欏叆鏁版嵁鐨勫崗璁�
 	MakeWriteProtocol(addr string, value int) []byte
 
+	MakeWriteProtocolBits(bits int, addr string, ival int) []byte
+
 	// ParseReadValue 瑙f瀽浠庤澶囪鍙栫殑鍊�
 	ParseReadValue(data []byte) int
+
+	ParseReadValueBits(data []byte) int
 
 	// ParseWriteValue 瑙f瀽鍐欏叆璁惧鐨勭粨鏋滐紝骞惰繑鍥炵粨鏋滃拰鏄惁鎴愬姛
 	ParseWriteValue(data []byte) (int, bool)
@@ -49,13 +56,19 @@
 	return protocol, nil
 }
 
-func ReadPLC(deviceType, url, label string) (val int, err error) {
+func ReadPLC(deviceType, url, label string, length int) (val int, err error) {
 	protocol, err := LoadProtocol(deviceType)
 	if err != nil {
 		return 0, err
 	}
 	addr := protocol.ConvertLabelToAddress(label)
-	proto := protocol.MakeReadProtocol(addr)
+	var proto []byte
+	if length == 1 {
+		proto = protocol.MakeReadProtocol(addr)
+	} else {
+		proto = protocol.MakeReadProtocolBits(length*16, addr)
+	}
+
 	bp := base64.StdEncoding.EncodeToString(proto)
 	fullUrl := fmt.Sprintf("%v?proto=%v", url, bp)
 	resp, err := http.Get(fullUrl)
@@ -66,10 +79,15 @@
 	body := readBody(resp.Body)
 	data, err := base64.StdEncoding.DecodeString(string(body))
 	if err != nil {
-		logx.Errorf("ReadPLC base64.StdEncoding.DecodeString failed:%v", err)
+		logx.Errorf("ReadPLC base64.StdEncoding.DecodeString failed: %v, data: %v", err, string(body))
 		return 0, err
 	}
-	val = protocol.ParseReadValue(data)
+	if length == 1 {
+		val = protocol.ParseReadValue(data)
+	} else {
+		val = protocol.ParseReadValueBits(data)
+	}
+
 	fmt.Println("read PLC:", val)
 	return val, nil
 
@@ -91,7 +109,13 @@
 		return err
 	}
 	addr := protocol.ConvertLabelToAddress(label)
-	proto := protocol.MakeWriteProtocol(addr, val)
+	var proto []byte
+	if val < 65536 {
+		proto = protocol.MakeWriteProtocol(addr, val)
+	} else {
+		proto = protocol.MakeWriteProtocolBits(32, addr, val)
+	}
+
 	bp := base64.StdEncoding.EncodeToString(proto)
 	resp, err := http.Post(url, "text/plain", strings.NewReader(bp))
 	if err != nil {

--
Gitblit v1.8.0