Two server url #13
Replies: 3 comments 2 replies
-
When you create Builder instance with For example this would create 2 requests object to 2 different servers for same register requests, _ := b.Add(b.Uint16(18).UnitID(0).Name("test_do").ServerAddress("1.1.1.1:502")).
Add(b.Uint16(18).UnitID(0).Name("test_do2").ServerAddress("1.1.1.2:502")).
ReadHoldingRegistersTCP() same goes for in that example that you are talking we only connect to single server. This is for simplicity sake: if err := client.Connect(context.Background(), "localhost:5020"); err != nil {
return err
} |
Beta Was this translation helpful? Give feedback.
-
OK. if i need only one server. package main
import (
"context"
"fmt"
"github.com/aldas/go-modbus-client"
)
func main() {
serverAddress := "172.25.28.21:8899" // Виносимо адресу в змінну
b := modbus.NewRequestBuilder(serverAddress, 1)
requests, _ := b.Add(b.Uint16(10240).Name("main_volt_a")).
Add(b.Uint16(10242).Name("main_volt_b")).
Add(b.Uint16(10244).Name("main_volt_c")).
ReadHoldingRegistersTCP()
client := modbus.NewTCPClient()
if err := client.Connect(context.Background(),serverAddress); err != nil {
fmt.Printf("error backgroud\n")
return
}
for _, req := range requests {
resp, err := client.Do(context.Background(), req)
if err != nil {
fmt.Printf("error request\n")
return
}
// extract values to FieldValue struct
fields, _ := req.ExtractFields(resp.(modbus.RegistersResponse), true)
for _, t := range fields {
fmt.Print(t.Field.Name, "=", t.Value,"\n")
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Yes. my example is working. In global - i will get all devices with all them registers.
If my device will gone offline(and then online after some time) - will i need to make new client.Connect(context.Background(),serverAddress)? |
Beta Was this translation helpful? Give feedback.
-
Hi.
Why in your example we need to write two the same server url? Also first sever can be any like 1.1.1.1 -and this is working. )
But without first url - dont work. Can you explain logic?
Also why we duplicate unit ID in add request?
b := modbus.NewRequestBuilder("localhost:5020", 1) - can be ("1.1.1.1:5020", 1)
requests, _ := b.Add(b.Uint16(18).UnitID(0).Name("test_do")).
Add(b.Int64(18).Name("alarm_do_1").UnitID(0)). (why we dupilcate UnitID? )
ReadHoldingRegistersTCP() // split added fields into multiple requests with suitable quantity size
client := modbus.NewTCPClient()
if err := client.Connect(context.Background(), "localhost:5020"); err != nil {
return err
}
Beta Was this translation helpful? Give feedback.
All reactions