Search in sources :

Example 1 with ModbusSlaveException

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

the class ModbusTCPTransaction method execute.

// setRetries
@Override
public void execute() throws ModbusIOException, ModbusSlaveException, ModbusException {
    // 1. check that the transaction can be executed
    assertExecutable();
    try {
        // 2. Lock transaction
        /**
             * Note: The way this explicit synchronization is implemented at the moment,
             * there is no ordering of pending threads. The Mutex will simply call notify()
             * and the JVM will handle the rest.
             */
        m_TransactionLock.acquire();
        // 3. open the connection if not connected
        if (!m_Connection.isConnected()) {
            try {
                m_Connection.connect();
                m_IO = m_Connection.getModbusTransport();
            } catch (Exception ex) {
                throw new ModbusIOException("Connecting failed.");
            }
        }
        // 4. Retry transaction m_Retries times, in case of
        // I/O Exception problems.
        int tries = 0;
        do {
            try {
                // toggle and set the id
                m_Request.setTransactionID(c_TransactionID.increment());
                // 3. write request, and read response
                m_IO.writeMessage(m_Request);
                // read response message
                m_Response = m_IO.readResponse();
                break;
            } catch (ModbusIOException ex) {
                tries++;
                logger.error("execute try {}/{} error: {}. Request: {} (unit id {} & transaction {}). Address: {}:{}", tries, m_Retries, ex.getMessage(), m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_Connection.getAddress(), m_Connection.getPort());
                if (tries >= m_Retries) {
                    logger.error("execute reached max tries {}, throwing last error: {}. Request: {}. Address: {}:{}", m_Retries, ex.getMessage(), m_Request, m_Connection.getAddress(), m_Connection.getPort());
                    throw new ModbusIOException("Executing transaction failed (tried " + m_Retries + " times)");
                }
                Thread.sleep(m_RetryDelayMillis);
            }
        } while (true);
        if (tries > 0) {
            logger.info("execute eventually succeeded with {} re-tries. Request: {}. Address: {}:{}", tries, m_Request, m_Connection.getAddress(), m_Connection.getPort());
        }
        // 5. deal with "application level" exceptions
        if (m_Response instanceof ExceptionResponse) {
            throw new ModbusSlaveException(((ExceptionResponse) m_Response).getExceptionCode());
        }
        // 6. Check transaction validity
        if (isCheckingValidity()) {
            checkValidity();
        }
    } catch (InterruptedException ex) {
        throw new ModbusIOException("Thread acquiring lock was interrupted.");
    } finally {
        // Finally: close connection if reconnecting
        if (isReconnecting() && m_Connection != null) {
            m_Connection.close();
        }
        m_TransactionLock.release();
    }
}
Also used : ExceptionResponse(net.wimpi.modbus.msg.ExceptionResponse) ModbusSlaveException(net.wimpi.modbus.ModbusSlaveException) ModbusSlaveException(net.wimpi.modbus.ModbusSlaveException) ModbusException(net.wimpi.modbus.ModbusException) ModbusIOException(net.wimpi.modbus.ModbusIOException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Example 2 with ModbusSlaveException

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

the class ModbusUDPTransaction method execute.

// setRetries
@Override
public void execute() throws ModbusIOException, ModbusSlaveException, ModbusException {
    // 1. assert executeability
    assertExecutable();
    try {
        // 2. Lock transaction
        /**
             * Note: The way this explicit synchronization is implemented at the moment,
             * there is no ordering of pending threads. The Mutex will simply call notify()
             * and the JVM will handle the rest.
             */
        m_TransactionLock.acquire();
        // 3. open the connection if not connected
        if (!m_Terminal.isActive()) {
            try {
                m_Terminal.activate();
                m_IO = m_Terminal.getModbusTransport();
            } catch (Exception ex) {
                throw new ModbusIOException("Activation failed.");
            }
        }
        // 3. Retry transaction m_Retries times, in case of
        // I/O Exception problems.
        m_RetryCounter = 0;
        while (m_RetryCounter <= m_Retries) {
            if (m_RetryCounter != 0) {
                Thread.sleep(m_RetryDelayMillis);
            }
            try {
                // toggle the id
                m_Request.setTransactionID(c_TransactionID.increment());
                // while holding the lock on the IO object
                synchronized (m_IO) {
                    // write request message
                    m_IO.writeMessage(m_Request);
                    // read response message
                    m_Response = m_IO.readResponse();
                    break;
                }
            } catch (ModbusIOException ex) {
                m_RetryCounter++;
                continue;
            }
        }
        // 4. deal with "application level" exceptions
        if (m_Response instanceof ExceptionResponse) {
            throw new ModbusSlaveException(((ExceptionResponse) m_Response).getExceptionCode());
        }
        if (isCheckingValidity()) {
            checkValidity();
        }
    } catch (InterruptedException ex) {
        throw new ModbusIOException("Thread acquiring lock was interrupted.");
    } finally {
        m_TransactionLock.release();
    }
}
Also used : ExceptionResponse(net.wimpi.modbus.msg.ExceptionResponse) ModbusSlaveException(net.wimpi.modbus.ModbusSlaveException) ModbusSlaveException(net.wimpi.modbus.ModbusSlaveException) ModbusException(net.wimpi.modbus.ModbusException) ModbusIOException(net.wimpi.modbus.ModbusIOException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Example 3 with ModbusSlaveException

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

the class ModbusSerialTransaction method execute.

@Override
public void execute() throws ModbusIOException, ModbusSlaveException, ModbusException {
    // 1. assert executeability
    assertExecutable();
    try {
        // 2. Lock transaction
        /**
             * Note: The way this explicit synchronization is implemented at the moment,
             * there is no ordering of pending threads. The Mutex will simply call notify()
             * and the JVM will handle the rest.
             */
        m_TransactionLock.acquire();
        // while holding the lock on the IO object
        synchronized (m_IO) {
            int tries = 0;
            // toggle the id
            m_Request.setTransactionID(c_TransactionID.increment());
            do {
                try {
                    if (m_TransDelayMS > 0) {
                        try {
                            Thread.sleep(m_TransDelayMS);
                        } catch (InterruptedException ex) {
                            logger.error("InterruptedException: {}", ex.getMessage());
                        }
                    }
                    // write request message
                    m_IO.writeMessage(m_Request);
                    // read response message
                    m_Response = m_IO.readResponse();
                    break;
                } catch (ModbusIOException e) {
                    tries++;
                    logger.error("execute try {}/{} error: {}. Request: {} (unit id {} & transaction {}). Serial parameters: {}", tries, m_Retries, e.getMessage(), m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_SerialCon.getParameters());
                    if (tries >= m_Retries) {
                        logger.error("execute reached max tries {}, throwing last error: {}. Request: {}. Serial parameters: {}", m_Retries, e.getMessage(), m_Request, m_SerialCon.getParameters());
                        throw e;
                    }
                    Thread.sleep(m_RetryDelayMillis);
                }
            } while (true);
            if (tries > 0) {
                logger.info("execute eventually succeeded with {} re-tries. Request: {}. Serial parameters: {}", tries, m_Request, m_SerialCon.getParameters());
            }
        }
        // 4. deal with exceptions
        if (m_Response instanceof ExceptionResponse) {
            throw new ModbusSlaveException(((ExceptionResponse) m_Response).getExceptionCode());
        }
        if (isCheckingValidity()) {
            checkValidity();
        }
    } catch (InterruptedException ex) {
        throw new ModbusIOException("Thread acquiring lock was interrupted.");
    } finally {
        m_TransactionLock.release();
    }
}
Also used : ExceptionResponse(net.wimpi.modbus.msg.ExceptionResponse) ModbusSlaveException(net.wimpi.modbus.ModbusSlaveException) ModbusIOException(net.wimpi.modbus.ModbusIOException)

Aggregations

ModbusIOException (net.wimpi.modbus.ModbusIOException)3 ModbusSlaveException (net.wimpi.modbus.ModbusSlaveException)3 ExceptionResponse (net.wimpi.modbus.msg.ExceptionResponse)3 ModbusException (net.wimpi.modbus.ModbusException)2