# device-modbus-go
**Repository Path**: EdgexFoundry/device-modbus-go
## Basic Information
- **Project Name**: device-modbus-go
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 3
- **Created**: 2020-09-28
- **Last Updated**: 2026-11-23
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Device Modbus Go
[](https://jenkins.edgexfoundry.org/view/EdgeX%20Foundry%20Project/job/edgexfoundry/job/device-modbus-go/job/main/) [](https://codecov.io/gh/edgexfoundry/device-modbus-go) [](https://goreportcard.com/report/github.com/edgexfoundry/device-modbus-go) [](https://github.com/edgexfoundry/device-modbus-go/tags)  [](https://choosealicense.com/licenses/apache-2.0/)  [](https://github.com/edgexfoundry/device-modbus-go/pulls) [](https://github.com/edgexfoundry/device-modbus-go/contributors) [](https://github.com/orgs/edgexfoundry/teams/device-modbus-go-committers/members) [](https://github.com/edgexfoundry/device-modbus-go/commits)
> **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](https://github.com/edgexfoundry/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](https://github.com/edgexfoundry/device-modbus-go/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:
::`. 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.
```yaml
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.
```makefile
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](https://github.com/edgexfoundry/edgex-compose/tree/main/compose-builder#gen) `nat-bus` option to generate compose file for NATS and local dev images.
## Usage
Users can refer to [the document](https://docs.edgexfoundry.org/2.0/examples/Ch-ExamplesAddingModbusDevice) 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](Dockerfile) and [Docker Compose Builder] scripts.
## Modbus RTU Usage
Users can refer to [the document](https://docs.edgexfoundry.org/2.0/examples/Ch-ExamplesAddingModbusDevice/#set-up-the-modbus-rtu-device) to set up the Modbus RTU device.
## Community
- Discussion: https://github.com/orgs/edgexfoundry/discussions
- Mailing lists: https://lists.edgexfoundry.org/mailman/listinfo
## License
[Apache-2.0](LICENSE)
[Docker Compose Builder]: https://github.com/edgexfoundry/edgex-compose/tree/main/compose-builder