Just wanted to share my Hello Word TCP slave which works with qModMaster app. It basically has four registers and the fifth register increments every half second. Setup for port 4406. Hope someone finds this helpful. :)
public class EvalModbus4J {
static BasicProcessImage pImage;
static int counter = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int slaveId = 15;
int port = 4406;
pImage = new BasicProcessImage(slaveId);
pImage.setHoldingRegister(0, (short)00);
pImage.setHoldingRegister(1, (short)11);
pImage.setHoldingRegister(2, (short)22);
pImage.setHoldingRegister(3, (short)33);
boolean encapsulated = false;
TcpSlave slave = new TcpSlave(port, encapsulated);
slave.addProcessImage(pImage);
Runnable r = () -> {
while (true) {
counter++;
pImage.setHoldingRegister(4, (short)counter);
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(EvalModbus4J.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
new Thread(r).start();
try {
slave.start();
} catch (ModbusInitException ex) {
Logger.getLogger(EvalModbus4J.class.getName()).log(Level.SEVERE, null, ex);
}
}
}