You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2021/02/11 11:27:45 UTC

[plc4x] branch develop updated: - Fixed the way errors are reported (put the pushing of the result into the result pipe into a go-routine)

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 7b1bde5  - Fixed the way errors are reported (put the pushing of the result into the result pipe into a go-routine)
7b1bde5 is described below

commit 7b1bde58cd0b9f9cd618b57f68f1546c94d78f0c
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Feb 11 12:21:37 2021 +0100

    - Fixed the way errors are reported (put the pushing of the result into the result pipe into a go-routine)
---
 plc4go/examples/read/hello_world_plc4go_read.go | 25 ++++++++++++++++---------
 plc4go/pkg/plc4go/driverManager.go              | 16 ++++++++++++----
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/plc4go/examples/read/hello_world_plc4go_read.go b/plc4go/examples/read/hello_world_plc4go_read.go
index 81954b7..1455bcb 100644
--- a/plc4go/examples/read/hello_world_plc4go_read.go
+++ b/plc4go/examples/read/hello_world_plc4go_read.go
@@ -19,14 +19,20 @@
 package main
 
 import (
-	"encoding/json"
 	"fmt"
+	"github.com/apache/plc4x/plc4go/internal/plc4go/modbus"
+	"github.com/apache/plc4x/plc4go/internal/plc4go/spi/transports/tcp"
 	"github.com/apache/plc4x/plc4go/pkg/plc4go"
+	"github.com/apache/plc4x/plc4go/pkg/plc4go/model"
 )
 
 func main() {
+	driverManager := plc4go.NewPlcDriverManager()
+	driverManager.RegisterDriver(modbus.NewModbusDriver())
+	driverManager.RegisterTransport(tcp.NewTcpTransport())
+
 	// Get a connection to a remote PLC
-	crc := plc4go.NewPlcDriverManager().GetConnection("modbus:tcp://192.168.23.30")
+	crc := driverManager.GetConnection("modbus:tcp://192.168.23.30")
 
 	// Wait for the driver to connect (or not)
 	connectionResult := <-crc
@@ -41,10 +47,10 @@ func main() {
 
 	// Prepare a read-request
 	rrb := connection.ReadRequestBuilder()
-	rrb.AddItem("field", "holding-register:1:REAL[2]")
+	rrb.AddItem("field", "holding-register:26:REAL")
 	readRequest, err := rrb.Build()
 	if err != nil {
-        fmt.Printf("error preparing read-request: %s", connectionResult.Err.Error())
+		fmt.Printf("error preparing read-request: %s", connectionResult.Err.Error())
 		return
 	}
 
@@ -54,15 +60,16 @@ func main() {
 	// Wait for the response to finish
 	rrr := <-rrc
 	if rrr.Err != nil {
-        fmt.Printf("error executing read-request: %s", rrr.Err.Error())
+		fmt.Printf("error executing read-request: %s", rrr.Err.Error())
 		return
 	}
 
 	// Do something with the response
-	readResponseJson, err := json.Marshal(rrr.Response)
-	if err != nil {
-        fmt.Printf("error serializing read-response: %s", err.Error())
+	if rrr.Response.GetResponseCode("field") != model.PlcResponseCode_OK {
+		fmt.Printf("error an non-ok return code: %s", rrr.Response.GetResponseCode("field").GetName())
 		return
 	}
-	fmt.Printf("Result: %s\n", string(readResponseJson))
+
+	value := rrr.Response.GetValue("field")
+	fmt.Printf("Got result %f", value.GetFloat32())
 }
diff --git a/plc4go/pkg/plc4go/driverManager.go b/plc4go/pkg/plc4go/driverManager.go
index e71f006..4d28ca3 100644
--- a/plc4go/pkg/plc4go/driverManager.go
+++ b/plc4go/pkg/plc4go/driverManager.go
@@ -115,7 +115,9 @@ func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnec
 	connectionUrl, err := url.Parse(connectionString)
 	if err != nil {
 		ch := make(chan PlcConnectionConnectResult)
-		ch <- NewPlcConnectionConnectResult(nil, errors.New("error parsing connection string: "+err.Error()))
+		go func() {
+			ch <- NewPlcConnectionConnectResult(nil, errors.New("error parsing connection string: "+err.Error()))
+		}()
 		return ch
 	}
 
@@ -127,7 +129,9 @@ func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnec
 	driver, err := m.GetDriver(driverName)
 	if err != nil {
 		ch := make(chan PlcConnectionConnectResult)
-		ch <- NewPlcConnectionConnectResult(nil, errors.New("error getting driver for connection string: "+err.Error()))
+		go func() {
+			ch <- NewPlcConnectionConnectResult(nil, errors.New("error getting driver for connection string: "+err.Error()))
+		}()
 		return ch
 	}
 
@@ -139,7 +143,9 @@ func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnec
 		connectionUrl, err := url.Parse(connectionUrl.Opaque)
 		if err != nil {
 			ch := make(chan PlcConnectionConnectResult)
-			ch <- NewPlcConnectionConnectResult(nil, errors.New("error parsing connection string: "+err.Error()))
+			go func() {
+				ch <- NewPlcConnectionConnectResult(nil, errors.New("error parsing connection string: "+err.Error()))
+			}()
 			return ch
 		}
 		transportName = connectionUrl.Scheme
@@ -152,7 +158,9 @@ func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnec
 	// If no transport has been specified explicitly or per default, we have to abort.
 	if transportName == "" {
 		ch := make(chan PlcConnectionConnectResult)
-		ch <- NewPlcConnectionConnectResult(nil, errors.New("no transport specified and no default defined by driver"))
+		go func() {
+			ch <- NewPlcConnectionConnectResult(nil, errors.New("no transport specified and no default defined by driver"))
+		}()
 		return ch
 	}