Skip to content

Commit

Permalink
add modbus-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
led0nk committed Jan 22, 2025
1 parent 3115fdf commit cb92b77
Showing 1 changed file with 134 additions and 1 deletion.
135 changes: 134 additions & 1 deletion modbus_plugin/modbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package modbus_plugin_test
import (
"context"
"encoding/json"
"github.com/grid-x/modbus"
"os"
"strconv"
"time"

"github.com/grid-x/modbus"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -176,3 +178,134 @@ var _ = Describe("Test Against Docker Modbus Simulator", func() {
Expect(err).NotTo(HaveOccurred())
})
})

var _ = Describe("Test Against Wago-PLC", func() {

var wagoModbusEndpoint string

BeforeEach(func() {
wagoModbusEndpoint = os.Getenv("TEST_WAGO_MODBUS_ENDPOINT")

// Check if environment variables are set
if wagoModbusEndpoint == "" {
Skip("Skipping test: environment variables not set")

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-nodered-js

BeforeEach 01/22/25 21:18:11.966

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-nodered-js

BeforeEach 01/22/25 21:18:11.969

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-nodered-js

BeforeEach 01/22/25 21:18:11.969

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-nodered-js

BeforeEach 01/22/25 21:18:11.977

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-sensorconnect

BeforeEach 01/22/25 21:18:11.936

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-sensorconnect

BeforeEach 01/22/25 21:18:11.937

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-sensorconnect

BeforeEach 01/22/25 21:18:11.937

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-sensorconnect

BeforeEach 01/22/25 21:18:11.938

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-s7-plc

BeforeEach 01/22/25 21:18:21.116

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-s7-plc

BeforeEach 01/22/25 21:18:21.119

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-s7-plc

BeforeEach 01/22/25 21:18:21.12

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-s7-plc

BeforeEach 01/22/25 21:18:21.12

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-opcua-plc

BeforeEach 01/22/25 21:18:22.827

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-opcua-plc

BeforeEach 01/22/25 21:18:22.827

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-opcua-plc

BeforeEach 01/22/25 21:18:22.828

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-opcua-plc

BeforeEach 01/22/25 21:18:22.829

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-tag-processor

BeforeEach 01/22/25 21:18:11.981

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-tag-processor

BeforeEach 01/22/25 21:18:11.985

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-tag-processor

BeforeEach 01/22/25 21:18:11.987

Check notice on line 191 in modbus_plugin/modbus_test.go

View workflow job for this annotation

GitHub Actions / go-test-tag-processor

BeforeEach 01/22/25 21:18:11.989
return
}

})

type ModbusRegister struct {
Addresses []ModbusDataItemWithAddress
ExpectedValue json.Number
}

DescribeTable("Read discrete/coil/input/holding", func(tableEntry ModbusRegister) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
input := &ModbusInput{
SlaveIDs: []byte{1},
BusyRetries: 1,
Addresses: tableEntry.Addresses,
Handler: modbus.NewTCPClientHandler(wagoModbusEndpoint),
}
input.Client = modbus.NewClient(input.Handler)

var err error
input.RequestSet, err = input.CreateBatchesFromAddresses(input.Addresses)
Expect(err).NotTo(HaveOccurred())

err = input.Connect(ctx)
Expect(err).NotTo(HaveOccurred())

messageBatch, _, err := input.ReadBatch(ctx)
Expect(err).NotTo(HaveOccurred())

// increase the batch-length by 1 because of the heartbeat
Expect(messageBatch).To(HaveLen(len(tableEntry.Addresses) + 1))

for _, message := range messageBatch {

tagName, exists := message.MetaGet("modbus_tag_name")
Expect(exists).To(BeTrue())

// skip the heartbeat if processed
if tagName == "heartbeat" {
GinkgoWriter.Printf("Skipping heartbeat message: %+v\n", message)
continue
}

register, exists := message.MetaGet("modbus_tag_register")
Expect(exists).To(BeTrue())
Expect(register).To(Equal(tableEntry.Addresses[0].Register))

address, exists := message.MetaGet("modbus_tag_address")
Expect(exists).To(BeTrue())
Expect(address).To(Equal(strconv.FormatUint(uint64(tableEntry.Addresses[0].Address), 10)))

messageStruct, err := message.AsStructuredMut()
Expect(err).NotTo(HaveOccurred())

Expect(err).NotTo(HaveOccurred())
Expect(tagName).To(Equal(tableEntry.Addresses[0].Name))

Expect(messageStruct).To(BeAssignableToTypeOf(tableEntry.ExpectedValue))
Expect(messageStruct).To(Equal(tableEntry.ExpectedValue))

GinkgoWriter.Printf("Received message: %+v\n", messageStruct)
}

// Close connection
err = input.Close(ctx)
Expect(err).NotTo(HaveOccurred())

},
Entry("discrete (input)",
ModbusRegister{
Addresses: []ModbusDataItemWithAddress{
{
Name: "modbusBoolIn",
Register: "discrete",
Address: 0,
Type: "BOOL",
},
},
ExpectedValue: "1",
}),
Entry("coil (output)",
ModbusRegister{
Addresses: []ModbusDataItemWithAddress{
{
Name: "modbusBoolOut",
Register: "coil",
Address: 1,
Type: "BOOL",
},
},
ExpectedValue: "1",
}),
Entry("input (input)",
ModbusRegister{
Addresses: []ModbusDataItemWithAddress{
{
Name: "modbusIntIn",
Register: "input",
Address: 1,
Type: "INT16",
},
},
ExpectedValue: "1234",
}),
Entry("holding (output)",
ModbusRegister{
Addresses: []ModbusDataItemWithAddress{
{
Name: "modbusIntOut",
Register: "holding",
Address: 2,
Type: "INT16",
},
},
ExpectedValue: "1234",
}),
)
})

0 comments on commit cb92b77

Please sign in to comment.