From 8dfb8feb32bb5e4e460e23dcde42612a26fa2bcb Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期日, 27 八月 2023 01:11:28 +0800
Subject: [PATCH] fix

---
 pkg/plc/plc4x.go |   46 +++++++++++++++++++++++++++++-----------------
 1 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/pkg/plc/plc4x.go b/pkg/plc/plc4x.go
index e269e58..d38ac67 100644
--- a/pkg/plc/plc4x.go
+++ b/pkg/plc/plc4x.go
@@ -1,10 +1,10 @@
 package plc
 
 import (
+	"context"
 	"errors"
 	"fmt"
 	apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
-	"github.com/apache/plc4x/plc4go/spi/options"
 	"sync"
 	"time"
 
@@ -46,13 +46,22 @@
 			return conn, nil
 		}
 	}
+	// 鍒涘缓涓�涓笂涓嬫枃锛屽苟璁剧疆 5 绉掕秴鏃�
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+	defer cancel()
+	conn, err := NewGetModbusConnection(ctx, ipAddr)
+	if err != nil {
+		return nil, err
+	}
+	connectionManager.AddConnection(ipAddr, conn)
+	return conn, nil
+}
 
+func NewGetModbusConnection(ctx context.Context, ipAddr string) (plc4go.PlcConnection, error) {
 	// 鍒涘缓椹卞姩绠$悊鍣�
-	option := options.WithReceiveTimeout(time.Second * 5) //浜旂瓒呮椂
-	driverManager := plc4go.NewPlcDriverManager(option)
+	driverManager := plc4go.NewPlcDriverManager()
 	// 娉ㄥ唽TCP浼犺緭
 	transports.RegisterTcpTransport(driverManager)
-
 	// 娉ㄥ唽椹卞姩
 	//drivers.RegisterKnxDriver(driverManager)
 	drivers.RegisterModbusTcpDriver(driverManager)
@@ -61,23 +70,24 @@
 	connectionString := fmt.Sprintf("modbus-tcp://%s", ipAddr)
 	connectionRequestChanel := driverManager.GetConnection(connectionString)
 
-	// 绛夊緟杩炴帴鍝嶅簲
-	connectionResult := <-connectionRequestChanel
-
-	// 鍒ゆ柇鏄惁杩炴帴鎴愬姛
-	if err := connectionResult.GetErr(); err != nil {
-		return nil, err
+	// 绛夊緟杩炴帴鍝嶅簲锛屽悓鏃惰�冭檻涓婁笅鏂囩殑瓒呮椂
+	select {
+	case connectionResult := <-connectionRequestChanel:
+		if err := connectionResult.GetErr(); err != nil {
+			return nil, err
+		}
+		return connectionResult.GetConnection(), nil
+	case <-ctx.Done():
+		return nil, ctx.Err()
 	}
-
-	conn := connectionResult.GetConnection()
-	connectionManager.AddConnection(ipAddr, conn)
-	return conn, nil
 }
+
 func ReadHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) {
+	tag := fmt.Sprintf("tag:%v", address)
 	tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
 
 	// 璇绘ā寮�
-	readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()
+	readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
 	if err != nil {
 		fmt.Printf("preparing read-request:%s\n", err.Error())
 		return nil, err
@@ -103,10 +113,11 @@
 }
 
 func ReadHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) {
+	tag := fmt.Sprintf("tag:%v:%v", address, length)
 	tagAddress := fmt.Sprintf("holding-register:%d:UINT[%d]", address, length)
 
 	// 璇绘ā寮�
-	readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()
+	readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
 	if err != nil {
 		fmt.Printf("preparing read-request:%s\n", err.Error())
 		return nil, err
@@ -145,10 +156,11 @@
 }
 
 func WriteHoldingRegister(connection plc4go.PlcConnection, address int, value any) (string, error) {
+	tag := fmt.Sprintf("tag:%v:w", address)
 	tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
 
 	// 鍐欐ā寮�
-	writeRequest, err := connection.WriteRequestBuilder().AddTagAddress("tag", tagAddress, value).Build()
+	writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build()
 	if err != nil {
 		fmt.Printf("preparing read-request:%s\n", err.Error())
 		return "", err

--
Gitblit v1.8.0