Closed browncrane closed 6 years ago
ServerSocket serverSocket = new ServerSocket(3333);
Socket client = serverSocket.accept();
ModbusTranslator modbusTranslator = new ModbusTranslator(client);
modbusTranslator.connect();
Why do you invoke connect
, if you use a connected socket? Please show the code you use to connect to serverSocket.
In the ModbusTranslator
class
public final void connect() throws ModbusIOException {
if (!isConnected()) {
connectImpl();
}
}
private void connectImpl() throws ModbusIOException {
getConnection().open();
}
The getConnection()
returns a EmbeddedConnection
object.
The EmbeddedConnection
class extends ModbusConnection
and
@Override
protected void openImpl() {
if (!isOpened()) {
if (socket.isConnected()) {
try {
transport = ModbusTransportFactory.createTCP(socket);
} catch (IOException e) {
e.printStackTrace();
}
setReadTimeout(getReadTimeout());
}
}
}
@Override
protected void closeImpl() throws ModbusIOException {
try {
if (transport != null) {
transport.close();
}
} catch (IOException e) {
throw new ModbusIOException(e);
} finally {
transport = null;
}
}
Thinking about the hecules TCP client, it doesn't act like a Modbus Slave, would that be the reason of the SocketTimeoutException
?
Exactly. You should implement a TCP-client with Modbus Slave functionality. In any case, this approach looks a little weird.
Thank you for confirming my guess. What's the better practice in your experience? The approach I'm trying is base on the device which packages serial Modbus slave data as Modbus TCP data. And it acts as a TCP client to connect to the server first. After authorized by the server the device transfer Modbus data.
In my experience, the procedure of a remote device registration and the procedure of data transmission are different and should be performed in the different sessions. A remote device as a TCP-client sends it's connection parameters. The device performs this procedure at power on or after changing its settings. The server stores devices' connection parameters in a database and uses it to connect to devices. The server can ping the devices to determine if a device is available or not. If not the server can mark it as temporarily unavailable or remove it from the database.
The idea is to make the server listening and the clients would connect to them.
But not as a Modbus TCP client, as a normal TCP client, so the server could read the clients (act like Modbus Master) rather than read by clients. Maybe the idea totally wrong.
The test code:
Which will issue a
SocketTimeoutException
when running the test. Using hercules-setup-utility from https://www.hw-group.com/software/hercules-setup-utility to simulate a client socket.client.getOutputStream()
andclient.getInputStream()
;modbusTranslator.readHoldingRegisters(1,1,1);
ModbusTranslator
implementsFrameEventListenerList
with a customEmbeddedConnection
which extendsModbusConnection
The mainly different from your
ModbusMasterConnection
is it get the socket instead from init from tcp parameters.Any help would be appreciated.