protected ModbusResponse readResponseIn() throws ModbusIOException {
        boolean done;
        ModbusResponse response;
        int dlength;

        try {
            do {
                // 1. read to function code, create request and read function
                // specific bytes
                synchronized (byteInputStream) {
                    int uid = readByte();

                    if (uid != -1) {
                        int fc = readByte();
                        byteInputOutputStream.reset();
                        byteInputOutputStream.writeByte(uid);
                        byteInputOutputStream.writeByte(fc);

                        // create response to acquire length of message
                        response = ModbusResponse.createModbusResponse(fc);
                        response.setHeadless();

                        /*
                         * With Modbus RTU, there is no end frame. Either we
                         * assume the message is complete as is or we must do
                         * function specific processing to know the correct
                         * length. To avoid moving frame timing to the serial
                         * input functions, we set the timeout and to message
                         * specific parsing to read a response.
                         */
                        getResponse(fc, byteInputOutputStream);
                        dlength = byteInputOutputStream.size() - 2; // less the crc
                        if (logger.isDebugEnabled()) {
                            logger.debug("Response: {}", ModbusUtil.toHex(byteInputOutputStream.getBuffer(), 0, dlength + 2));
                        }
                        byteInputStream.reset(inBuffer, dlength);

                        // check CRC
                        int[] crc = ModbusUtil.calculateCRC(inBuffer, 0, dlength); // does not include CRC
                        if (ModbusUtil.unsignedByteToInt(inBuffer[dlength]) != crc[0] || ModbusUtil.unsignedByteToInt(inBuffer[dlength + 1]) != crc[1]) {
                            logger.debug("CRC should be {}, {}", crc[0], crc[1]);
                            throw new IOException("CRC Error in received frame: " + dlength + " bytes: " + ModbusUtil.toHex(byteInputStream.getBuffer(), 0, dlength));
                        }
                    }
                    else {
                        throw new IOException("Error reading response");
                    }

                    // read response
                    byteInputStream.reset(inBuffer, dlength);
                    response.readFrom(byteInputStream);
                    done = true;
                }
            } while (!done);
            return response;
        }
        catch (IOException ex) {
            // FIXME: This printout is wrong when reading response from other slave
            throw new ModbusIOException("I/O exception - failed to read response for request [%s] - %s", ModbusUtil.toHex(lastRequest), ex.getMessage());
        }
    }
protected ModbusResponse readResponseIn ( ) throws ModbusIOException { boolean done ; ModbusResponse response ; int dlength ; try { do { synchronized ( byteInputStream ) { int uid = readByte ( ) ; if ( uid != - 1 ) { int fc = readByte ( ) ; byteInputOutputStream . reset ( ) ; byteInputOutputStream . writeByte ( uid ) ; byteInputOutputStream . writeByte ( fc ) ; response = ModbusResponse . createModbusResponse ( fc ) ; response . setHeadless ( ) ; getResponse ( fc , byteInputOutputStream ) ; dlength = byteInputOutputStream . size ( ) - 2 ; if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Response: {}" , ModbusUtil . toHex ( byteInputOutputStream . getBuffer ( ) , 0 , dlength + 2 ) ) ; } byteInputStream . reset ( inBuffer , dlength ) ; int [ ] crc = ModbusUtil . calculateCRC ( inBuffer , 0 , dlength ) ; if ( ModbusUtil . unsignedByteToInt ( inBuffer [ dlength ] ) != crc [ 0 ] || ModbusUtil . unsignedByteToInt ( inBuffer [ dlength + 1 ] ) != crc [ 1 ] ) { logger . debug ( "CRC should be {}, {}" , crc [ 0 ] , crc [ 1 ] ) ; throw new IOException ( "CRC Error in received frame: " + dlength + " bytes: " + ModbusUtil . toHex ( byteInputStream . getBuffer ( ) , 0 , dlength ) ) ; } } else { throw new IOException ( "Error reading response" ) ; } byteInputStream . reset ( inBuffer , dlength ) ; response . readFrom ( byteInputStream ) ; done = true ; } } while ( ! done ) ; return response ; } catch ( IOException ex ) { throw new ModbusIOException ( "I/O exception - failed to read response for request [%s] - %s" , ModbusUtil . toHex ( lastRequest ) , ex . getMessage ( ) ) ; } }
readResponse - Read the bytes for the response from the slave.

@return a <tt>ModbusRespose</tt>

@throws com.ghgande.j2mod.modbus.ModbusIOException If the response cannot be read from the socket/port