0

I am developing Modbus/TCP master application in Java using jamod library. During consecutive readings from my slave device, connection is being abruptly closed.

My code:

import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadMultipleRegistersRequest;
import net.wimpi.modbus.msg.ReadMultipleRegistersResponse;
import net.wimpi.modbus.net.TCPMasterConnection;

public static void main(String[] args) throws Exception {
  TCPMasterConnection connection = new TCPMasterConnection(InetAddress.getByName("192.168.0.111"));
  connection.setPort(502);
  connection.connect();
  if (!connection.isConnected())
    return;

  System.out.println(readIntFromIntAddr(connection, 256));
  System.out.println(readIntFromIntAddr(connection, 256));
  System.out.println(readIntFromIntAddr(connection, 256));
  System.out.println(readIntFromIntAddr(connection, 256));
  System.out.println(readIntFromIntAddr(connection, 256));
  System.out.println(readIntFromIntAddr(connection, 256));
}

private static synchronized int readIntFromIntAddr(TCPMasterConnection connection, int address) throws Exception {
  ReadMultipleRegistersRequest Rreq = new ReadMultipleRegistersRequest(address, 1); // reading one register
  Rreq.setUnitID(1); // slaveAddress

  ModbusTCPTransaction trans = null;
  trans = new ModbusTCPTransaction(connection);
  trans.setRetries(3);
  trans.setReconnecting(true);
  trans.setRequest(Rreq);
  trans.execute();

  ReadMultipleRegistersResponse Rres = (ReadMultipleRegistersResponse) trans.getResponse();
  return Rres.getRegisterValue(0);
}

The error, which is thrown during 6th reading

net.wimpi.modbus.ModbusIOException: Premature end of stream (Header truncated).
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:190)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:192)
at cz.jlochman.marklar.drivers.connection.modbustcp.ModBusTcpConnection.readIntFromIntAddr(ModBusTcpConnection.java:280)
at cz.jlochman.marklar.drivers.connection.modbustcp.ModBusTcpConnection.main(ModBusTcpConnection.java:267)

After this error, I am not able to restore connection. I have to terminate my application thread and start application again. I have found some workarounds: Adding Thread.sleep(500) after 5th reading removes this exception. Thread.sleep(200) after each reading works as well. Unfortunately, these workarounds are not enough to satisfy my demand of continous reading from slave device.

Does anyone have any idea, what could be going on?

Jan Lochman
  • 439
  • 3
  • 12
  • do you have an idea what happened? i tried it and it works. – noidea May 16 '16 at 16:10
  • I didn't solved that. Slave device manufacturer didn't respond to my question so I was forced to implement a workaround. I've kept `Thread.sleep(200)` after each reading and modifed reading from slave, so now, I am reading from multiple addresses at once... This was enough to reduce reading interval bellow desired value. – Jan Lochman May 27 '16 at 13:52

0 Answers0