Modbusone
A modbus library for Go, with unified client and server APIs. One implementation to rule them all.
Install / Use
/learn @xiegeo/ModbusoneREADME
ModbusOne 
A Modbus library for Go, with unified client and server APIs. One implementation to rule them all.
<details> <summary>Example</summary>// handlerGenerator returns ProtocolHandlers that interact with our application.
// In this example, we are only using Holding Registers.
func handlerGenerator(name string) modbusone.ProtocolHandler {
return &modbusone.SimpleHandler{
ReadHoldingRegisters: func(address, quantity uint16) ([]uint16, error) {
fmt.Printf("%v ReadHoldingRegisters from %v, quantity %v\n",
name, address, quantity)
r := make([]uint16, quantity)
// Application code that fills in r here.
return r, nil
},
WriteHoldingRegisters: func(address uint16, values []uint16) error {
fmt.Printf("%v WriteHoldingRegisters from %v, quantity %v\n",
name, address, len(values))
// Application code here.
return nil
},
OnErrorImp: func(req modbusone.PDU, errRep modbusone.PDU) {
fmt.Printf("%v received error:%x in request:%x", name, errRep, req)
},
}
}
// serial is a fake serial port.
type serial struct {
io.ReadCloser
io.WriteCloser
}
func newInternalSerial() (io.ReadWriteCloser, io.ReadWriteCloser) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
return &serial{ReadCloser: r1, WriteCloser: w2}, &serial{ReadCloser: r2, WriteCloser: w1}
}
func (s *serial) Close() error {
s.ReadCloser.Close()
return s.WriteCloser.Close()
}
func Example_serialPort() {
// Server id and baudRate, for Modbus over serial port.
id := byte(1)
baudRate := int64(19200)
// Open serial connections:
clientSerial, serverSerial := newInternalSerial()
// Normally we want to open a serial connection from serial.OpenPort
// such as github.com/tarm/serial. modbusone can take any io.ReadWriteCloser,
// so we created two that talks to each other for demonstration here.
// SerialContext adds baudRate information to calculate
// the duration that data transfers should takes.
// It also records Stats of read and dropped packets.
clientSerialContext := modbusone.NewSerialContext(clientSerial, baudRate)
serverSerialContext := modbusone.NewSerialContext(serverSerial, baudRate)
// You can create either a client or a server from a SerialContext and an id.
client := modbusone.NewRTUClient(clientSerialContext, id)
server := modbusone.NewRTUServer(serverSerialContext, id)
useClientAndServer(client, server, id) // follow the next function
// Output:
// reqs count: 2
// reqs count: 3
// server ReadHoldingRegisters from 0, quantity 125
// client WriteHoldingRegisters from 0, quantity 125
// server ReadHoldingRegisters from 125, quantity 75
// client WriteHoldingRegisters from 125, quantity 75
// client ReadHoldingRegisters from 1000, quantity 100
// server WriteHoldingRegisters from 1000, quantity 100
// server ReadHoldingRegisters from 0, quantity 125
// client WriteHoldingRegisters from 0, quantity 125
// server ReadHoldingRegisters from 125, quantity 75
// client WriteHoldingRegisters from 125, quantity 75
// client ReadHoldingRegisters from 1000, quantity 100
// server WriteHoldingRegisters from 1000, quantity 100
// serve terminated: io: read/write on closed pipe
}
func useClientAndServer(client modbusone.Client, server modbusone.ServerCloser, id byte) {
termChan := make(chan error)
// Serve is blocking until the serial connection has io errors or is closed.
// So we use a goroutine to start it and continue setting up our demo.
go client.Serve(handlerGenerator("client"))
go func() {
// A server is Started to same way as a client
err := server.Serve(handlerGenerator("server"))
// Do something with the err here.
// For a command line app, you probably want to terminate.
// For a service, you probably want to wait until you can open the serial port again.
termChan <- err
}()
defer client.Close()
defer server.Close()
// If you only need to support server side, then you are done.
// If you need to support client side, then you need to make requests.
clientDoTransactions(client, id) // see following function
// Clean up
server.Close()
fmt.Println("serve terminated:", <-termChan)
}
func clientDoTransactions(client modbusone.Client, id byte) {
// start by building some requests
startAddress := uint16(0)
quantity := uint16(200)
reqs, err := modbusone.MakePDURequestHeaders(modbusone.FcReadHoldingRegisters,
startAddress, quantity, nil)
if err != nil {
fmt.Println(err) // if what you asked for is not possible.
}
// Larger than allowed requests are split to many packets.
fmt.Println("reqs count:", len(reqs))
// We can add more requests, even of different types.
// The last nil is replaced by the reqs to append to.
startAddress = uint16(1000)
quantity = uint16(100)
reqs, err = modbusone.MakePDURequestHeaders(modbusone.FcWriteMultipleRegisters,
startAddress, quantity, reqs)
if err != nil {
fmt.Println(err)
}
fmt.Println("reqs count:", len(reqs))
// Range over the requests to handle each individually,
for _, r := range reqs {
err = client.DoTransaction(r)
if err != nil {
fmt.Println(err, "on", r) // The server timed out, or the connection was closed.
}
}
// or just do them all at once. Notice that reqs can be reused.
n, err := modbusone.DoTransactions(client, id, reqs)
if err != nil {
fmt.Println(err, "on", reqs[n])
}
}
func Example_tcp() {
// TCP address of the host
host := "127.2.9.1:12345"
// Default server id
id := byte(1)
// Open server tcp listener:
listener, err := net.Listen("tcp", host)
if err != nil {
fmt.Println(err)
return
}
// Connect to server:
conn, err := net.Dial("tcp", host)
if err != nil {
fmt.Println(err)
return
}
// You can create either a client or a server
client := modbusone.NewTCPClient(conn, 0)
server := modbusone.NewTCPServer(listener)
// shared example code with serial port
useClientAndServer(client, server, id)
// Output:
// reqs count: 2
// reqs count: 3
// server ReadHoldingRegisters from 0, quantity 125
// client WriteHoldingRegisters from 0, quantity 125
// server ReadHoldingRegisters from 125, quantity 75
// client WriteHoldingRegisters from 125, quantity 75
// client ReadHoldingRegisters from 1000, quantity 100
// server WriteHoldingRegisters from 1000, quantity 100
// server ReadHoldingRegisters from 0, quantity 125
// client WriteHoldingRegisters from 0, quantity 125
// server ReadHoldingRegisters from 125, quantity 75
// client WriteHoldingRegisters from 125, quantity 75
// client ReadHoldingRegisters from 1000, quantity 100
// server WriteHoldingRegisters from 1000, quantity 100
// serve terminated: accept tcp 127.2.9.1:12345: use of closed network connection
}
// end readme example
</details>
For more usage examples, see examples/memory, which is a command line application that can be used as either a server or a client.
Architecture
Why
There exist Modbus libraries for Go, such as goburrow/modbus and flosse/go-modbus. However they do not include any server APIs. Even if server function is implemented, user code will have to be written separately to support running both as client and server.
In my use case, client/server should be interchangeable. User code should worry about how to handle the translation of MODBUS data model to application logic. The only difference is the client also initiate requests.
This means that a remote function call like API, which is effective as a client-side API, is insufficient.
Instead, a callback based API (like http server handler) is used for both server and client.
Implemented
- Serial RTU
- Modbus over TCP
- Function Codes 1-6,15,16
- Server and Client API
- Server and Client Tester (examples/memory)
Development
This project and API is stable, and I am using it in production.
My primary usage is RTU (over RS-485). TCP is also supported. Others may or may not be implemented in the future.
Contribution to new or existing functionally, or just changing a private identifier public are welcome, as well as documentation, test, example code or any other improvements.
Development tools:
go generaterunsembedmdto copy parts of the examples_test.go to this readme filegolangci-lint runfor improvement hints. Ideally there are no warnings.- Use
go test -race -count=5 ./...pre release.
Breaking Changes
2022-09-09 v1.0.0
V1 release has the following depreciated identifiers removed:
- The
Serverinterface is removed. UseServerCloserinstead. - Public global variable
DebugOutis changed to private. UseSetDebugOut(w io.Writer)instead for thread safety. - Type alias
type ProtocalHandler = ProtocolHandlerremoved. - Function redirect from
GetRTUBidirectionSizeFromHeadertoGetRTUBidirectionalSizeFromHeaderremoved. - Function redirect from
NewRTUCLienttoNewRTUClientremoved.
2018-09-27 v0.2.0
- NewRTUPacketReader returns PacketReader interface instead of io.Reader. When a new RTU server or client receives a SerialContext, it will test if it is also a PacketReader, and only create a new NewRTUPacketReader if not.
- (cli
