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 2020/11/19 14:43:59 UTC

[plc4x] branch feature/plc4go updated: - Added support for the unit-identifier config option in the Modus driver

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

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


The following commit(s) were added to refs/heads/feature/plc4go by this push:
     new 5d80f0a  - Added support for the unit-identifier config option in the Modus driver
5d80f0a is described below

commit 5d80f0a33bd19b099d98713a2c4ebdea71593786
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Nov 19 15:43:51 2020 +0100

    - Added support for the unit-identifier config option in the Modus driver
---
 plc4go/cmd/main/drivers/modbus_test.go        |  4 ++--
 plc4go/internal/plc4go/modbus/ModbusDriver.go | 13 ++++++++++++-
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/plc4go/cmd/main/drivers/modbus_test.go b/plc4go/cmd/main/drivers/modbus_test.go
index 328451b..3f6a6f8 100644
--- a/plc4go/cmd/main/drivers/modbus_test.go
+++ b/plc4go/cmd/main/drivers/modbus_test.go
@@ -85,7 +85,7 @@ func Connection(t *testing.T) {
 	wb := utils.NewWriteBuffer()
 	adu.Serialize(*wb)
 
-	servAddr := "192.168.23.30:502"
+	servAddr := "192.168.23.30:502?unit-identifier=1"
 	tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
 	if err != nil {
 		println("ResolveTCPAddr failed:", err.Error())
@@ -130,7 +130,7 @@ func TestPlc4goDriver(t *testing.T) {
 	driverManager.RegisterTransport(tcp.NewTcpTransport())
 
 	// Get a connection to a remote PLC
-	crc := driverManager.GetConnection("modbus://192.168.23.30")
+	crc := driverManager.GetConnection("modbus://192.168.23.30?unit-identifier=1")
 
 	// Wait for the driver to connect (or not)
 	connectionResult := <-crc
diff --git a/plc4go/internal/plc4go/modbus/ModbusDriver.go b/plc4go/internal/plc4go/modbus/ModbusDriver.go
index 0196d9f..191fad2 100644
--- a/plc4go/internal/plc4go/modbus/ModbusDriver.go
+++ b/plc4go/internal/plc4go/modbus/ModbusDriver.go
@@ -27,6 +27,7 @@ import (
 	"github.com/apache/plc4x/plc4go/internal/plc4go/spi"
 	"github.com/apache/plc4x/plc4go/internal/plc4go/transports"
 	"github.com/apache/plc4x/plc4go/pkg/plc4go"
+    "strconv"
 )
 
 type ModbusDriver struct {
@@ -91,7 +92,17 @@ func (m ModbusDriver) GetConnection(transportUrl url.URL, transports map[string]
 	}()
 	codec := NewModbusMessageCodec(transportInstance, nil)
 
+	// If a unit-identifier was provided in the connection string use this, otherwise use the default of 1
+	unitIdentifier := uint8(1)
+	if value, ok := options["unit-identifier"]; ok {
+	    var intValue int
+        intValue, err = strconv.Atoi(value[0])
+        if err == nil {
+            unitIdentifier = uint8(intValue)
+        }
+    }
+
 	// Create the new connection
-	connection := NewModbusConnection(uint8(1), codec, options, m.fieldHandler)
+	connection := NewModbusConnection(unitIdentifier, codec, options, m.fieldHandler)
 	return connection.Connect()
 }