Skip to content

About

Owner: Device WG

Resources

Contributing

Stars

120 stars

Watchers

20 watching

Forks

Repository files navigation

Device Modbus Go

Build Status Code Coverage Go Report Card GitHub Latest Dev Tag) GitHub Latest Stable Tag) GitHub License GitHub go.mod Go version GitHub Pull Requests GitHub Contributors GitHub Committers GitHub Commit Activity

Warning
The main branch of this repository contains work-in-progress development code for the upcoming release, and is not guaranteed to be stable or working. It is only compatible with the main branch of edgex-compose which uses the Docker images built from the main branch of this repo and other repos.

The source for the latest release can be found at Releases.

Overview

Modbus Micro Service - device service for connecting Modbus devices to EdgeX.

SharedConnection

modbus-tcp accepts an optional boolean SharedConnection. Missing or false keeps one TCP connection per Unit ID. true shares one TCP socket across every Unit ID on the same address and port.

The client cache key stays modbus-tcp:<address>:<port>:<unitID>. Each Unit ID has its own packager and a fixed slave ID. Only the TCP transporter is shared, via NewClient2(packager, transport) from github.com/grid-x/modbus. The handler is stored as a pointer. RTU and ASCII are unchanged.

Commands to one address and port are already serialized by lockableAddress. Timeout, IdleTimeout, link and protocol recovery, and ConnectDelay are taken from the first device that opens the session. A later device with different timings logs a warning and keeps the first handler.

IdleTimeout follows grid-x: 0 dials for every request, a positive value closes after idle, a negative value keeps the socket open. A persistent shared session needs IdleTimeout: -1 on every device of that server. SharedConnection: true alone does not change idle behaviour.

Closing one Unit ID does not close the socket while another Unit ID still uses it. A Modbus exception leaves the socket up. A connection error closes it; the next send dials once, and the other Unit ID clients stay cached. Malformed protocol responses also reset the socket. Local validation and transformation errors leave a shared connection intact.

Changing a device's endpoint or SharedConnection setting closes its old client before using the new configuration. Commands using stale connection settings, or targeting a removed device, are rejected.

Graceful service stop rejects new commands and device changes, waits for admitted operations to finish, and closes every transport. Forced stop cancels queued commands and connection attempts. An in-flight network operation may still run until its timeout. Closed shared transports cannot reconnect.

Each packager has its own MBAP transaction-id counter, so identifiers repeat on the shared socket. With one request in flight, Verify checks the frame just sent. The transporter's numeric recovery heuristic can mis-classify a late frame.

protocols:
  modbus-tcp:
    Address: "192.0.2.10"
    Port: 502
    UnitID: 1
    Timeout: 5
    IdleTimeout: -1
    SharedConnection: true

Build with NATS Messaging

Currently, the NATS Messaging capability (NATS MessageBus) is opt-in at build time. This means that the published Docker images do not include the NATS messaging capability.

The following make commands will build the local binary or local Docker image with NATS messaging capability included.

make build-nats
make docker-nats

The locally built Docker image can then be used in place of the published Docker image in your compose file. See Compose Builder nat-bus option to generate compose file for NATS and local dev images.

Usage

Users can refer to the document to learn how to use this device service.

Example Profile and Device

The ProfilesDir and DevicesDir in the configuration.yaml are empty string by default. To use the example Profile and Device in this repository, please fill './res/profiles' and './res/devices' to ProfilesDir and DevicesDir respectively. modbus.test.device.profile.yml and modbus.test.devices.yaml will be loaded and created when the Device Service starts up. Users can modify those files or add additional Profile YAML to meet their needs.

Modbus Simulator

Build and run the Modbus simulator

$ cd simulator
$ go build
$ ./simulator 
Modbus TCP address: 0.0.0.0:1502 
Start up a Modbus TCP simulator.

Packaging

For docker, please refer to the Dockerfile and Docker Compose Builder scripts.

Modbus RTU Usage

Users can refer to the document to set up the Modbus RTU device.

Community

License

Apache-2.0

About

Owner: Device WG

Resources

Contributing

Stars

120 stars

Watchers

20 watching

Forks

Releases

Packages

Used by

Contributors

Languages