digitalpetri / modbus

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

Just help to first use #45

Closed bergheil closed 1 year ago

bergheil commented 1 year ago

Hi, I'm using a master simulator, I have created some registries and I'm trying to read the value using the example documented in the home page of this project.

I'm trying to read a registry to the index 2 using the unitid 1. Here the code:


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

        future.whenCompleteAsync((response, ex) -> {
            if (response != null) {
                //long myValue = response.getRegisters().order(ByteOrder.LITTLE_ENDIAN).readUnsignedInt();
                //long myValue = response.getRegisters().readUnsignedInt();
                int myValue = response.getRegisters().readInt();
                System.out.println("Risultato int = " + myValue);
                ReferenceCountUtil.release(response);
            } else {
                logger.error("Completed exceptionally, message={}", ex.getMessage(), ex);
            }
            scheduler.schedule(() -> sendAndReceive(master), 1, TimeUnit.SECONDS);
        }, Modbus.sharedExecutor());

The result is

Risultato int = 0

but the correct value of the registry is "5". Can anyone help me?

Thanks

Francesco

bergheil commented 1 year ago

I attach the screenshot of "modpoll" a simple client the read the same registry. Bye

image

kevinherron commented 1 year ago

Capture the requests from each software using Wireshark and see what’s different.

bergheil commented 1 year ago

Thanks for the answer, I will do. Can I ask if I'm using in the right way request object?

kevinherron commented 1 year ago

Yes, but you're reading 3 registers instead of 1 like you are with modpoll. You also need to read a short from the buffer (2 bytes), not an int (4 bytes), because each register is only 2 bytes.

I'm not sure if modpoll uses 1-based or 0-based addressing, either, so the Wireshark capture may help determine the difference.