Skip to content
/ modbus Public
forked from andig/modbus

Fault-tolerant implementation of modbus protocol in Go (golang)

License

Notifications You must be signed in to change notification settings

GoChik/modbus

 
 

Repository files navigation

go modbus Build Status GoDoc

Fault-tolerant, fail-fast implementation of Modbus protocol in Go.

Supported functions

Bit access:

  • Read Discrete Inputs
  • Read Coils
  • Write Single Coil
  • Write Multiple Coils

16-bit access:

  • Read Input Registers
  • Read Holding Registers
  • Write Single Register
  • Write Multiple Registers
  • Read/Write Multiple Registers
  • Mask Write Register
  • Read FIFO Queue

Supported formats

  • TCP
  • Serial (RTU, ASCII)

Usage

Basic usage:

// Modbus TCP
client := modbus.TCPClient("localhost:502")
// Read input register 9
results, err := client.ReadInputRegisters(8, 1)
// Modbus RTU/ASCII
// use your preferred serial library
import "github.com/tarm/serial"

// open the serial connection
port, _ := serial.OpenPort(&serial.Config{
	Name:   "/dev/ttyS0"
	Baud:   115200,
	Parity: serial.ParityNone,
})

// then create the client
client = modbus.RTUClient(port)
results, err = client.ReadCoils(2, 1)

Advanced usage:

// Modbus TCP
handler := modbus.NewTCPClientHandler("localhost:502")
handler.Timeout = 10 * time.Second
handler.SlaveId = 0xFF
handler.Logger = log.New(os.Stdout, "test: ", log.LstdFlags)
// Connect manually so that multiple requests are handled in one connection session
err := handler.Connect()
defer handler.Close()

client := modbus.NewClient(handler)
results, err := client.ReadDiscreteInputs(15, 2)
results, err = client.WriteMultipleRegisters(1, 2, []byte{0, 3, 0, 4})
results, err = client.WriteMultipleCoils(5, 10, []byte{4, 3})

References

About

Fault-tolerant implementation of modbus protocol in Go (golang)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 96.2%
  • C 3.8%