I'm working on a modbus UDP implementation [ J2Mod(2.3.4) ] in Java. I have found almost no useful documentation. I wrote a slave as shown below.
It basically, create a UDP Slave and add processImage for unitId=0 and unitId=1. Then it write out digital in and digital out.
public class Slave {
private SimpleProcessImage image;
private ModbusSlave slave;
public Slave(){
image = new SimpleProcessImage();
for (int i = 0; i < 62000; i++) {
image.addDigitalOut(i, new SimpleDigitalOut(false));
image.addDigitalIn(i, new SimpleDigitalIn(false));
}
(new Timer()).scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
read();
}
}, 10000, 10000);
startServer();
}
private void read() {
System.out.print("Read In : ");
for (int i = 0; i < interSize; i++) {
System.out.print((image.getDigitalIn(i).isSet() ? 1 : 0) + " ");
}
System.out.print("Read Out: ");
for (int i = 0; i < interSize; i++) {
System.out.print((image.getDigitalOut(i).isSet() ? 1 : 0) + " ");
}
}
public void startServer() {
try {
slave = ModbusSlaveFactory.createUDPSlave(502);
slave.addProcessImage(0, image);
slave.addProcessImage(1, image);
slave.open();
} catch (ModbusException e) {
e.printStackTrace();
}
}
}
I also wrote a client example. It can successfuly read/write from/to slave. I use master.readCoils master.writeCoil functions.
But I have a java program that works like modbus client. I cannot see the source codes but there is a log screen. So I can see that this program can write (with writeCoil) but it cannot read from slave (by readCoils) and throws "Cannot send UDP message" , "Socket was interrupt" and "Received Timeout " errors in order.
10:42:30.611 [Timer-1] ERROR c.g.j.m.io.ModbusUDPTransaction - Cannot send UDP message
com.ghgande.j2mod.modbus.ModbusIOException: Socket was interrupted
at com.ghgande.j2mod.modbus.io.ModbusUDPTransport.readResponse(ModbusUDPTransport.java:132) ~[j2mod-2.3.4.jar:2.3.4]
at com.ghgande.j2mod.modbus.io.ModbusUDPTransaction.execute(ModbusUDPTransaction.java:129) ~[j2mod-2.3.4.jar:2.3.4]
at com.ghgande.j2mod.modbus.facade.AbstractModbusMaster.readCoils(AbstractModbusMaster.java:97) [j2mod-2.3.4.jar:2.3.4]
at gov.tubitak.ys03.railcore.request.RailModbusUDPMaster.readCoils(RailModbusUDPMaster.java:42) [railcore-0.0.26.jar:na]
at gov.tubitak.ys03.railcore.service.strategy.ILReaderSchedule.run(ILReaderSchedule.java:67) [railcore-0.0.26.jar:na]
at java.util.TimerThread.mainLoop(Unknown Source) [na:1.8.0_121]
at java.util.TimerThread.run(Unknown Source) [na:1.8.0_121]
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.TwoStacksPlainDatagramSocketImpl.receive0(Native Method) ~[na:1.8.0_121]
at java.net.TwoStacksPlainDatagramSocketImpl.receive(Unknown Source) ~[na:1.8.0_121]
at java.net.DatagramSocket.receive(Unknown Source) ~[na:1.8.0_121]
at com.ghgande.j2mod.modbus.net.UDPMasterTerminal.receiveMessage(UDPMasterTerminal.java:100) ~[j2mod-2.3.4.jar:2.3.4]
at com.ghgande.j2mod.modbus.io.ModbusUDPTransport.readResponse(ModbusUDPTransport.java:122) ~[j2mod-2.3.4.jar:2.3.4]
... 6 common frames omitted