digitalpetri / modbus

Modbus TCP, Modbus RTU/TCP, and Modbus RTU/Serial for Java 17+.
Eclipse Public License 2.0
653 stars 222 forks source link

Usage sample to query a SUN2000 inverter? #67

Closed StefanOltmann closed 4 months ago

StefanOltmann commented 4 months ago

Hi,

I'm totally new to modbus and I look for a way how I can query my SUN2000 using Java.

Can you provide a sample how I can use this project?

The interface definitions are here: https://forum.iobroker.net/assets/uploads/files/1683548281975-sun2000ma-v100r001c20-modbus-interface-definitions.pdf

For example I want to get these values:

How would I do that?

And can I get them all at once or do I need to make separate queries?

Can I get multiple values in one block if I say "give me 37000 and the next 20 values"?

Kind regards,

Stefan

StefanOltmann commented 4 months ago

Nevermind. I'm going to use j2mod.

kevinherron commented 4 months ago

It's not clear from that document, but you either need to read:

as sometimes Modbus addressing is prefixed with 0, 1, 3, or 4 for Coils, Discrete Inputs, Input Registers, or Holding Registers.

Reading HR 32064 is as simple as the examples show:

CompletableFuture<ReadHoldingRegistersResponse> future =
    master.sendRequest(new ReadHoldingRegistersRequest(32064, 1), 0);

IR 2064 would be something like:

CompletableFuture<ReadInputRegistersResponse> future =
    master.sendRequest(new ReadInputRegistersRequest(2064, 1), 0);

Depending on the device, the unit ID may need to be 1 instead of 0.

Can I get multiple values in one block if I say "give me 37000 and the next 20 values"?

Yes, and this is how it would be possible to read multiple values at once, as long as they reside in the same contiguous block and the device supports it. It's common for Modbus slave/server implementations to return an exception response when a master/client reads an address that has no data defined at it.