digitalpetri / modbus

Modbus TCP, Modbus RTU/TCP, and Modbus RTU/Serial for Java 17+.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to use

paulorb opened this issue · comments

I tried the example code (master) against a Modicon PLC, on latest master "whenCompleteAsync" call of the "sendAndReceive" method is never completed. On wireshark I can see the TCP 3-way handshake completed successfully but no request at all.

I also tried version v1.1.1, on this one on wireshark TCP handshake ok, on this version I also can see request and response. But the "whenCompleteAsync" is never called.

Can you post the full code you are using? It sounds like you might just be letting the program exit.

Same code you provided MasterExample.java, the only difference is that I ported it to Kotlin (Intelij /Windows) and changed the number of masters to 1.

MasterExample(1, 100).start()

Also I added a delay between requests

   for (j in 0 until nRequests) {
                Thread.sleep(1000);
                sendAndReceive(master)
       }

On wireshark, each second I see the modbus request and response. But I dont see the message from println

 future.whenCompleteAsync({ response: ReadHoldingRegistersResponse?, ex: Throwable ->
            if (response != null) {
                println("response")
                println("Response: " + ByteBufUtil.hexDump(response.registers));
                ReferenceCountUtil.release(response)
            } else {
                println("Completed exceptionally, message=${ex.message}")
            }
            scheduler.schedule({ sendAndReceive(master) }, 1, TimeUnit.SECONDS)
        }, Modbus.sharedExecutor())

This is the output I get. I also added two more metrics requestCounter and responseCounter to try to understand what's going on.

Running with v1.1.0. Wireshark: connection ok, also can see req and response (as expected)

Mean rate=0.05851407124404467, 1m rate=2.356221287991323E-12
request counter 100
response counter 100
timeout counter 0
Mean rate=0.05834295290433627, 1m rate=2.1678282356471266E-12
request counter 100
response counter 100
timeout counter 0
Mean rate=0.05817300823468216, 1m rate=1.994498260082879E-12
request counter 100
response counter 100
timeout counter 0
Mean rate=0.05800398511124546, 1m rate=1.8350269841771565E-12
request counter 100
response counter 100
timeout counter 0
Mean rate=0.057836042249378314, 1m rate=1.6883063274862844E-12
request counter 100
response counter 100
timeout counter 0
Mean rate=0.057668622049167804, 1m rate=1.553316806787101E-12

Running with v1.2.0. - Wireshark: connection ok, no request, no response

request counter 0
response counter 0
timeout counter 0
Mean rate=0.0, 1m rate=0.0
request counter 0
response counter 0
timeout counter 0
Mean rate=0.0, 1m rate=0.0
request counter 0
response counter 0
timeout counter 0
Mean rate=0.0, 1m rate=0.0
request counter 0
response counter 0
timeout counter 0
Mean rate=0.0, 1m rate=0.0
request counter 0
response counter 0
timeout counter 0
Mean rate=0.0, 1m rate=0.0

Have you tried something simple like in the README?

The examples work fine for me, both with the simulated slave and a real PLC, though I had to reduce the concurrent requests down to not overload the PLC.

Try this:

ModbusTcpMasterConfig config = new ModbusTcpMasterConfig.Builder("localhost").build();
ModbusTcpMaster master = new ModbusTcpMaster(config);

master.connect();

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

future.thenAccept(response -> {
    System.out.println("Response: " + ByteBufUtil.hexDump(response.getRegisters()));

    ReferenceCountUtil.release(response);
});

Thread.sleep(Integer.MAX_VALUE);

Otherwise you'll have to provide me with a fully self-contained and runnable example, not a partial snippet.