grid-x / modbus

BSD 3-Clause "New" or "Revised" License
82 stars 28 forks source link

Using the TCP/Serial Converter with multiple devices #55

Closed PVKonovalov closed 2 years ago

PVKonovalov commented 2 years ago

How can I use this library if I want to receive data from a set of Modbus devices connected to the same serial interface of a TCP/Serial converter. In this case, I have one IP address and port and different SlaveIDs. But in the library, the SlaveID setting is at the handler level, but in my case it's better to set the SlaveID when I send ReadInputRegisters (at the client level). Is it possible to change the SlaveID for each request?

andig commented 2 years ago

Why don‘t you share the client between your devices?

PVKonovalov commented 2 years ago

I didn't quite understand what is meant by share the client between my devices? I just want to send requests to multiple devices over a single TCP connection.

andig commented 2 years ago

Use one gridx client

PVKonovalov commented 2 years ago

Something like that? ` handler := modbus.NewTCPClientHandler("127.0.0.1:5002") ... err := handler.Connect() ... client := modbus.NewClient(handler) ... handler.SlaveID = 6 results, err := client.ReadInputRegisters(0, 10) ... handler.SlaveID = 7 results, err := client.ReadInputRegisters(0, 10)

`

PVKonovalov commented 2 years ago

Yes it works, thanks. It just looks a little out of line with the library's object hierarchy structure.

frzifus commented 2 years ago

@PVKonovalov to keep the usage similar, you can wrap the modbus client and extend your method signatures with by the salve id. Maybe that helps.