Thanks for you guys' easy-of-use Modbus implementation at the beginning!
Recently I have been developing a web system used to test Modbus (TCP) devices. What I want to implement now is to offer system users an API to create some Modbus slaves running on the server.
However, when I want to return whether the new slave is started successfully (IMO, main loop start can means success) to the frontend, it turns out that tcpSlave.start()
won't end until an exception happened. If I run this method in a new thread, still I cannot detect whether the slave is started successfully or the port has been occupied. Ways like setting a timeout are not elegant IMO. I think if the main loop of this method is run in a new thread instead of the main thread calling the method, it will be much more convenient for me to use this method.
Regarding code:
/** {@inheritDoc} */
@Override
public void start() throws ModbusInitException {
try {
serverSocket = new ServerSocket(port);
Socket socket;
while (true) {
socket = serverSocket.accept();
TcpConnectionHandler handler = new TcpConnectionHandler(socket);
executorService.execute(handler);
synchronized (listConnections) {
listConnections.add(handler);
}
}
}
catch (IOException e) {
throw new ModbusInitException(e);
}
}