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/10/30 15:01:42 UTC

[plc4x] branch feature/plc4go updated: - Implemented a modbus ping functionality using the diagnostic request (function code 8, sub-function code 0)

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 35fd89d  - Implemented a modbus ping functionality using the diagnostic request (function code 8, sub-function code 0)
35fd89d is described below

commit 35fd89d4b68683ea7058892c53c6a6e3bdc894e8
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri Oct 30 16:01:35 2020 +0100

    - Implemented a modbus ping functionality using the diagnostic request (function code 8, sub-function code 0)
---
 sandbox/plc4go/cmd/main/drivers/modbus_test.go     |  8 +++++
 .../internal/plc4go/modbus/ModbusConnection.go     | 35 +++++++++++++++++++++-
 .../plc4go/model/DefaultPlcWriteRequest.go         |  4 +--
 3 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/sandbox/plc4go/cmd/main/drivers/modbus_test.go b/sandbox/plc4go/cmd/main/drivers/modbus_test.go
index 4eae9d0..b85b7c2 100644
--- a/sandbox/plc4go/cmd/main/drivers/modbus_test.go
+++ b/sandbox/plc4go/cmd/main/drivers/modbus_test.go
@@ -146,6 +146,14 @@ func TestPlc4goDriver(t *testing.T) {
 	}
 	connection := connectionResult.Connection
 
+	// Try to ping the remote device
+	pingResultChannel := connection.Ping()
+	pingResult := <-pingResultChannel
+	if pingResult.Err != nil {
+		t.Errorf("couldn't ping device: %s", pingResult.Err.Error())
+		return
+	}
+
 	// Make sure the connection is closed at the end
 	defer connection.Close()
 
diff --git a/sandbox/plc4go/internal/plc4go/modbus/ModbusConnection.go b/sandbox/plc4go/internal/plc4go/modbus/ModbusConnection.go
index 607f4f3..9cce59a 100644
--- a/sandbox/plc4go/internal/plc4go/modbus/ModbusConnection.go
+++ b/sandbox/plc4go/internal/plc4go/modbus/ModbusConnection.go
@@ -19,6 +19,8 @@
 package modbus
 
 import (
+	"errors"
+	model2 "plc4x.apache.org/plc4go-modbus-driver/v0/internal/plc4go/modbus/readwrite/model"
 	internalModel "plc4x.apache.org/plc4go-modbus-driver/v0/internal/plc4go/model"
 	"plc4x.apache.org/plc4go-modbus-driver/v0/internal/plc4go/spi"
 	"plc4x.apache.org/plc4go-modbus-driver/v0/internal/plc4go/spi/interceptors"
@@ -87,7 +89,38 @@ func (m ModbusConnection) IsConnected() bool {
 }
 
 func (m ModbusConnection) Ping() <-chan plc4go.PlcConnectionPingResult {
-	panic("implement me")
+	result := make(chan plc4go.PlcConnectionPingResult)
+	diagnosticRequestPdu := model2.CastIModbusPDU(model2.NewModbusPDUDiagnosticRequest(0, 0x42))
+	go func() {
+		pingRequest := model2.NewModbusTcpADU(m.transactionIdentifier, 1, diagnosticRequestPdu)
+		err := m.messageCodec.Send(pingRequest)
+		if err != nil {
+			result <- plc4go.NewPlcConnectionPingResult(err)
+			return
+		}
+		// Register an expected response
+		check := func(response interface{}) bool {
+			responseAdu := model2.CastModbusTcpADU(response)
+			return responseAdu.TransactionIdentifier == m.transactionIdentifier &&
+				responseAdu.UnitIdentifier == 1
+		}
+		// Register a callback to handle the response
+		pingResponseChanel := m.messageCodec.Expect(check)
+		if pingResponseChanel == nil {
+			result <- plc4go.NewPlcConnectionPingResult(errors.New("no response channel"))
+			return
+		}
+		// Wait for the response to come in
+		pingResponse := <-pingResponseChanel
+		if pingResponse == nil {
+			result <- plc4go.NewPlcConnectionPingResult(errors.New("no response"))
+			return
+		}
+		// If we got a valid response (even if it will probably contain an error, we know the remote is available)
+		result <- plc4go.NewPlcConnectionPingResult(nil)
+	}()
+
+	return result
 }
 
 func (m ModbusConnection) GetMetadata() model.PlcConnectionMetadata {
diff --git a/sandbox/plc4go/internal/plc4go/model/DefaultPlcWriteRequest.go b/sandbox/plc4go/internal/plc4go/model/DefaultPlcWriteRequest.go
index 5c90853..91e5a63 100644
--- a/sandbox/plc4go/internal/plc4go/model/DefaultPlcWriteRequest.go
+++ b/sandbox/plc4go/internal/plc4go/model/DefaultPlcWriteRequest.go
@@ -88,7 +88,7 @@ func (m DefaultPlcWriteRequest) MarshalXML(e *xml.Encoder, start xml.StartElemen
 		return err
 	}
 
-	if err := e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "Fields"}}); err != nil {
+	if err := e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "fields"}}); err != nil {
 		return err
 	}
 	for fieldName, field := range m.fields {
@@ -102,7 +102,7 @@ func (m DefaultPlcWriteRequest) MarshalXML(e *xml.Encoder, start xml.StartElemen
 			return err
 		}
 	}
-	if err := e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "Fields"}}); err != nil {
+	if err := e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "fields"}}); err != nil {
 		return err
 	}