J2mod Library

First, you need the dependency. Since j2mod is not officially in Maven Central (though you can find community builds), the easiest method is downloading the JAR from GitHub or using a JitPack integration.

<dependency> <groupId>org.s7</groupId> <artifactId>j2mod</artifactId> <version>2.2.0</version> </dependency> j2mod library

The library is actively maintained on GitHub by Steve O'Hara, with recent updates focusing on stability and modern Java standards. First, you need the dependency

// Create an RTU slave connection on COM port 3 SerialConnection serialConnection = new SerialConnection("/dev/ttyUSB0"); ModbusCoupler.getReference().setUnitID(1); RTUSlave slave = new RTUSlave(serialConnection); slave.addProcessImage(1, new SimpleProcessImage()); // Create an RTU slave connection on COM

public static void readHoldingRegisters(TCPMasterConnection conn) try // Prepare the request: Start at register 40001 (offset 0), read 10 registers ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(0, 10); ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) conn.sendMessage(request); Register[] registers = response.getRegisters(); for (int i = 0; i < registers.length; i++) // j2mod registers can be accessed as bytes, short, or int int value = registers[i].getValue(); System.out.println("Register[" + i + "] = " + value);