Mahantesh - 2016-05-18

Hi all,

i am not getting any reponse in ModbusTCPTransport.java. when i call readResponse() function it will try to read
at m_Input.read(buffer, 0, 6). Here after no response. You can see this function below in that i am getting only fisrt logger(m_Input.isAvailbal 0) but not the second logger. Same driver is working fine in serial connection. An one has clue why it is not getting any value? Please reply to this. Any help is appreciated....

public ModbusResponse readResponse() throws ModbusIOException {

    try {

        ModbusResponse response = null;

        synchronized (m_ByteIn) {
            // use same buffer
            byte[] buffer = m_ByteIn.getBuffer();
            if (Modbus.debug)
                System.err.println("Read: " +
                        ModbusUtil.toHex(buffer, 0, m_ByteIn.count));
            if (!headless) {
                /*
                 * All Modbus TCP transactions start with 6 bytes. Get them.
                 */
                System.out.println("m_Input.isAvailbale "+m_Input.available());
                if (m_Input.read(buffer, 0, 6) == -1)
                {
                    throw new ModbusIOException(
                            "Premature end of stream (Header truncated).");
                }
                System.out.println("m_Input.isAvailbale_2 "+m_Input.available());
                /*
                 * The transaction ID is the first word (offset 0) in the
                 * data that was just read. It will be echoed back to the
                 * requester.
                 * 
                 * The protocol ID is the second word (offset 2) in the
                 * data. It should always be 0, but I don't check.
                 * 
                 * The length of the payload is the third word (offset 4) in
                 * the data that was just read. That's what I need in order
                 * to read the rest of the response.
                 */
                int transaction = ModbusUtil.registerToShort(buffer, 0);
                int protocol = ModbusUtil.registerToShort(buffer, 2);
                int count = ModbusUtil.registerToShort(buffer, 4);

                if (m_Input.read(buffer, 6, count) == -1)
                    throw new ModbusIOException(
                            "Premature end of stream (Message truncated).");

                m_ByteIn.reset(buffer, (6 + count));
                m_ByteIn.reset();
                m_ByteIn.skip(7);
                int function = m_ByteIn.readUnsignedByte();
                response = ModbusResponse.createModbusResponse(function);

                /*
                 * Rewind the input buffer, then read the data into the
                 * response.
                 */
                m_ByteIn.reset();
                response.readFrom(m_ByteIn);
                response.setTransactionID(transaction);
                response.setProtocolID(protocol);
            } else {
                /*
                 * This is a headless response. It has the same format as a
                 * RTU over Serial response.
                 */
                int unit = m_Input.readByte();
                int function = m_Input.readByte();

                response = ModbusResponse.createModbusResponse(function);
                response.setUnitID(unit);
                response.setHeadless();
                response.readData(m_Input);

                /*
                 * Now discard the CRC. Which hopefully wasn't needed
                 * because this is a TCP transport.
                 */
                m_Input.readShort();
            }
        }
        return response;
    } catch (SocketTimeoutException ex) {
        throw new ModbusIOException("Timeout reading response");
    } catch (Exception ex) 
    {
        if(ex instanceof SocketException)
        {
            ModbusDevManager.bConnected = false;
        }
        throw new ModbusIOException("I/O exception - failed to read.");
    }
}

output :
m_Input.isAvailbale 0
m_Input.isAvailbale 0
m_Input.isAvailbale 0