Search in sources :

Example 11 with ModbusIOException

use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.

the class ModbusTCPTransport method readRequest.

// write
@Override
public ModbusRequest readRequest() throws ModbusIOException {
    // System.out.println("readRequest()");
    try {
        ModbusRequest req = null;
        synchronized (m_ByteIn) {
            // use same buffer
            byte[] buffer = m_ByteIn.getBuffer();
            // read to byte length of message
            if (m_Input.read(buffer, 0, 6) == -1) {
                throw new EOFException("Premature end of stream (Header truncated).");
            }
            // extract length of bytes following in message
            int bf = ModbusUtil.registerToShort(buffer, 4);
            // read rest
            if (m_Input.read(buffer, 6, bf) == -1) {
                throw new ModbusIOException("Premature end of stream (Message truncated).");
            }
            m_ByteIn.reset(buffer, (6 + bf));
            m_ByteIn.skip(7);
            int functionCode = m_ByteIn.readUnsignedByte();
            m_ByteIn.reset();
            req = ModbusRequest.createModbusRequest(functionCode);
            req.readFrom(m_ByteIn);
        }
        return req;
    /*
             * int transactionID = m_Input.readUnsignedShort();
             * int protocolID = m_Input.readUnsignedShort();
             * int dataLength = m_Input.readUnsignedShort();
             * if (protocolID != Modbus.DEFAULT_PROTOCOL_ID || dataLength > 256) {
             * throw new ModbusIOException();
             * }
             * int unitID = m_Input.readUnsignedByte();
             * int functionCode = m_Input.readUnsignedByte();
             * ModbusRequest request =
             * ModbusRequest.createModbusRequest(functionCode, m_Input, false);
             * if (request instanceof IllegalFunctionRequest) {
             * //skip rest of bytes
             * for (int i = 0; i < dataLength - 2; i++) {
             * m_Input.readByte();
             * }
             * }
             * //set read parameters
             * request.setTransactionID(transactionID);
             * request.setProtocolID(protocolID);
             * request.setUnitID(unitID);
             * return request;
             *
             */
    } catch (EOFException eoex) {
        throw new ModbusIOException(true);
    } catch (SocketException sockex) {
        // connection reset by peer, also EOF
        throw new ModbusIOException(true);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ModbusIOException("I/O exception - failed to read.");
    }
}
Also used : SocketException(java.net.SocketException) EOFException(java.io.EOFException) ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) ModbusIOException(net.wimpi.modbus.ModbusIOException) IOException(java.io.IOException) EOFException(java.io.EOFException) SocketException(java.net.SocketException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Example 12 with ModbusIOException

use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.

the class ModbusTCPTransport method writeMessage.

// close
@Override
public void writeMessage(ModbusMessage msg) throws ModbusIOException {
    try {
        msg.writeTo(m_Output);
        m_Output.flush();
    // write more sophisticated exception handling
    } catch (Exception ex) {
        throw new ModbusIOException(String.format("I/O exception - failed to write: %s", ex.getMessage()));
    }
}
Also used : IOException(java.io.IOException) EOFException(java.io.EOFException) SocketException(java.net.SocketException) ModbusIOException(net.wimpi.modbus.ModbusIOException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Example 13 with ModbusIOException

use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.

the class ModbusUDPTransport method readRequest.

// write
@Override
public ModbusRequest readRequest() throws ModbusIOException {
    try {
        ModbusRequest req = null;
        synchronized (m_ByteIn) {
            m_ByteIn.reset(m_Terminal.receiveMessage());
            m_ByteIn.skip(7);
            int functionCode = m_ByteIn.readUnsignedByte();
            m_ByteIn.reset();
            req = ModbusRequest.createModbusRequest(functionCode);
            req.readFrom(m_ByteIn);
        }
        return req;
    } catch (Exception ex) {
        throw new ModbusIOException("I/O exception - failed to read.");
    }
}
Also used : ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ModbusIOException(net.wimpi.modbus.ModbusIOException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Example 14 with ModbusIOException

use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.

the class ModbusUDPTransport method readResponse.

// readRequest
@Override
public ModbusResponse readResponse() throws ModbusIOException {
    try {
        ModbusResponse res = null;
        synchronized (m_ByteIn) {
            m_ByteIn.reset(m_Terminal.receiveMessage());
            m_ByteIn.skip(7);
            int functionCode = m_ByteIn.readUnsignedByte();
            m_ByteIn.reset();
            res = ModbusResponse.createModbusResponse(functionCode);
            res.readFrom(m_ByteIn);
        }
        return res;
    } catch (InterruptedIOException ioex) {
        throw new ModbusIOException("Socket timed out.");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ModbusIOException("I/O exception - failed to read.");
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ModbusResponse(net.wimpi.modbus.msg.ModbusResponse) ModbusIOException(net.wimpi.modbus.ModbusIOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Example 15 with ModbusIOException

use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.

the class ModbusSerialListener method listen.

/**
     * Listen to incoming messages.
     */
private void listen() {
    try {
        m_Listening = true;
        m_SerialCon.open();
        logger.trace("Opened Serial connection.");
        ModbusTransport transport = m_SerialCon.getModbusTransport();
        do {
            if (m_Listening) {
                try {
                    // 1. read the request
                    ModbusRequest request = transport.readRequest();
                    ModbusResponse response = null;
                    // test if Process image exists
                    if (ModbusCoupler.getReference().getProcessImage() == null) {
                        response = request.createExceptionResponse(Modbus.ILLEGAL_FUNCTION_EXCEPTION);
                    } else {
                        response = request.createResponse();
                    }
                    logger.debug("Request:{}", request.getHexMessage());
                    logger.debug("Response:{}", response.getHexMessage());
                    transport.writeMessage(response);
                    count();
                } catch (ModbusIOException ex) {
                    ex.printStackTrace();
                    continue;
                }
            }
        // ensure nice multithreading behaviour on specific platforms
        } while (true);
    } catch (Exception e) {
        // FIXME: this is a major failure, how do we handle this
        e.printStackTrace();
    }
}
Also used : ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) ModbusResponse(net.wimpi.modbus.msg.ModbusResponse) ModbusTransport(net.wimpi.modbus.io.ModbusTransport) ModbusIOException(net.wimpi.modbus.ModbusIOException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Aggregations

ModbusIOException (net.wimpi.modbus.ModbusIOException)19 IOException (java.io.IOException)14 ModbusResponse (net.wimpi.modbus.msg.ModbusResponse)7 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)6 EOFException (java.io.EOFException)3 InterruptedIOException (java.io.InterruptedIOException)3 SocketException (java.net.SocketException)3 ModbusSlaveException (net.wimpi.modbus.ModbusSlaveException)3 ExceptionResponse (net.wimpi.modbus.msg.ExceptionResponse)3 ModbusException (net.wimpi.modbus.ModbusException)2 ModbusTransport (net.wimpi.modbus.io.ModbusTransport)1