0

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: enter image description here Sent data (8 bytes): 01 03 00 64 00 01 c5 d5

The device configuration:

enter image description here


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,
    })

wireshark capture enter image description here

5
  • rtuovertcp is not the same as ModbusTCP; I'd guess your URL should be tcp://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 only Timeout will be used). Commented Apr 29, 2024 at 21:23
  • tried that with the same result :/ (question updated) Commented Apr 29, 2024 at 22:48
  • 1
    In that case I'd start by checking the wiring and comms settings (baudrate, Unit ID etc); and then testing with a "known good" application like mbpoll/modpoll (your code looks fine and is sending the request, so the issue is most likely between the USR unit and whatever device you have connected via RS485). Providing more details (model of the USR unit, what is connected to it etc) would make it simpler to find manuals etc. Commented Apr 30, 2024 at 3:19
  • is Modbus I/P the same as Modbus TCP? I just got access to the manufacturer's remote monitoring portal, and its working just fine. It uses Mango SCADA and I see it configures Modbus I/P Data points. I'll try a "known good" application Commented Apr 30, 2024 at 19:00
  • "Modbus I/P" is not a term used in the specs so it's difficult to be sure what this means (but I'd guess Modbus TCP - the use of port 502 supports this). However, assuming the "manufacturer's remote monitoring" is running on their servers, it's unlikely to have direct access into your network so is probably getting data another way. Commented Apr 30, 2024 at 19:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.