This Go package provides Modbus access for client (master) applications to communicate with server (slave) devices, over both TCP/IP and Serial Line/RTU/ASCII frame protocols.
Note that in modbus terminology, client refers to the master application or device, and the server is the slave waiting to respond to instructions, as shown in this transaction diagram:
This code was originally forked from lubia/modbus and repositioned as a pure client (master) library for use by controller applications.
Install the package in your environment with these commands (the RTU code now depends on goserial):
go get github.com/tarm/goserial
go get github.com/dpapathanasiou/go-modbus
Next, build and run the examples:
- rtu-client.go for an RTU example
- ascii-client.go for an ASCII example
- tcp-client.go for a TCP/IP example
Slave devices which have USB ports for RTU access will not work immediately upon hot-plugging into a master computer.
For master devices running linux, the USB serial port adapter must be explicitly activated using the usbserial linux kernel module, as follows:
- Immediately after plugging in the serial port USB, use dmesg to find the vendor and product ID numbers:
$ sudo dmesg | tail
There should be a line which looks like this:
[ 556.572417] usb 3-1: New USB device found, idVendor=04d8, idProduct=000c
- Use the usbserial linux kernel module to enable it, using the same vendor and product ID numbers from the dmesg output:
$ sudo modprobe usbserial vendor=0x04d8 product=0x000c
- Confirm that the serial port is attached to a specific tty device file:
$ sudo dmesg | tail
There should now be a line like this:
[ 2134.866724] usb 3-1: generic converter now attached to ttyUSB0
which means that the serial port is now programmatically accessible via /dev/ttyUSB0
- Modbus Technical Specifications
- Modbus Interface Tutorial
- Modbus TCP/IP Overview
- Modbus RTU Protocol Overview
- Modbus ASCII Protocol Overview
- Lubia Yang for the original modbus code in Go
- l.lefebvre for his excellent modbus client and server (slave device simulator) code repositories
- Tarmigan Casebolt for his goserial library, which resolved connection issues in RTU mode
- modbusdriver.com for their free Diagslave Modbus Slave Simulator tool
- Mohammad Hafiz (mypapit) for his well-written How to enable USB-Serial Port adapter (RS-232) in Ubuntu Linux blog post