Search in sources :

Example 1 with ModbusTransport

use of net.wimpi.modbus.io.ModbusTransport 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)1 ModbusTransport (net.wimpi.modbus.io.ModbusTransport)1 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)1 ModbusResponse (net.wimpi.modbus.msg.ModbusResponse)1