I'm using https://github.com/simonvetter/modbus. I'm able to open a connection with the device, but requests to read registers always timeout.
My first time using modbus. Can anyone tell me what the issue might be?
Code:
package main
import (
"fmt"
"os"
"time"
"github.com/simonvetter/modbus"
)
func main() {
client, err := modbus.NewClient(&modbus.ClientConfiguration{
URL: "rtuovertcp://192.168.50.60:502",
Speed: 19200,
Parity: modbus.PARITY_NONE,
DataBits: 8,
StopBits: 1,
Timeout: 1 * time.Second,
})
if err != nil {
fmt.Println("Error creating Modbus client:", err)
return
}
// tried with and without setting this
client.SetUnitId(100)
err = client.Open()
for err != nil {
fmt.Println("faild to open", err)
os.Exit(1)
}
defer client.Close()
var addr uint16
var reg16 uint16
// tried many different addresses and also tried modbus.INPUT_REGISTER
addr = 100
reg16, err = client.ReadRegister(addr, modbus.HOLDING_REGISTER)
if err != nil {
fmt.Println("ReadRegister:", err) // err == "request timed out"
os.Exit(1)
}
fmt.Printf("address: %v", addr)
fmt.Printf(" value: %v", reg16) // as unsigned integer
fmt.Printf(" value: %v", int16(reg16)) // as signed integer
fmt.Printf(" value: %v", float32(reg16)) // as float
}
The device has an interface that shows I've connected and its received data, but sent nothing back. (RX > 0, TX == 0)
Wireshark capture:
Sent data (8 bytes): 01 03 00 64 00 01 c5 d5
The device configuration:
EDIT: tried Modbus TCP with the same result:
client, err := modbus.NewClient(&modbus.ClientConfiguration{
URL: "tcp://192.168.50.60:502",
Timeout: 1 * time.Second,
})



rtuovertcpis not the same as ModbusTCP; I'd guess your URL should betcp://192.168.50.60:502(which means ModbusTCP will be used). Note that the other settings (Speed, Parity etc) are not relevant in your Go code (as it will communicate with the USR unit via TCP so onlyTimeoutwill be used).