WARNING: The APIs are not yet stable, changing versions may result in broken build
A Modbus library that supports RTU, ASCII, and TCP servers and clients. Examples can be found in the examples directory.
Note: RTU implementation does not follow strict timing requirements due to limitations in non-RTOS environments.
Creating a Modbus client is as simple as calling <type>.NewModbusClient. Replace <type> with the transport you want to use, ex: tcp, rtu, or ascii. Clients expose the standard Modbus functions. Due to how the Modbus TCP/UDP protocols work, the address parameter on these methods has no effect.
Creating a Modbus server is as simple as calling NewModbusServer on the appropriate type. Servers are intended to have a long lifetime, as such they have Start() and Stop() methods.
All servers use the DefaultHandler with 65535 registers of each type by default. If you wish to have fewer registers, simply use the NewModbusServerWithHandler constructor.
handler := server.NewDefaultHandler(logger, 65535, 65535, 65535, 65535)
server, err := tcp.NewModbusServerWithHandler(logger, ":502", handler)
All implementations of the server use the DefaultHandler, however you can create your own handler if you the default one does not suit your needs. Simply implement the RequestHandler interface and use the NewModbusServerWithHandler constructor to pass in the new handler. While I provide the ability to write your own handler, it is not for the feint of heart.
There are a handful of examples in the examples directory that cover most functionality. Each example has a readme with more information.
These are straightforward examples of using the clients to connect to a Modbus Server and read/write values.
These are straightforward examples of using the servers that a Modbus Client can connect to and read/write values.
This example shows how you can share a single RequestHandler between multiple servers to function as a bridge between 2 Modbus clients.