Search in sources :

Example 1 with ReadCoilsRequest

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

the class ModbusSlave method update.

/**
     * Reads data from the connected device and updates items with the new data
     *
     * @param binding ModbusBindig that stores providers information
     */
public void update(ModbusBinding binding) {
    try {
        Object local = null;
        Exception localReadError = null;
        try {
            if (ModbusBindingProvider.TYPE_COIL.equals(getType())) {
                ModbusRequest request = new ReadCoilsRequest(getStart(), getLength());
                if (this instanceof ModbusSerialSlave) {
                    request.setHeadless();
                }
                ReadCoilsResponse response = (ReadCoilsResponse) getModbusData(request);
                local = response.getCoils();
            } else if (ModbusBindingProvider.TYPE_DISCRETE.equals(getType())) {
                ModbusRequest request = new ReadInputDiscretesRequest(getStart(), getLength());
                ReadInputDiscretesResponse response = (ReadInputDiscretesResponse) getModbusData(request);
                local = response.getDiscretes();
            } else if (ModbusBindingProvider.TYPE_HOLDING.equals(getType())) {
                ModbusRequest request = new ReadMultipleRegistersRequest(getStart(), getLength());
                ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) getModbusData(request);
                local = response.getRegisters();
            } else if (ModbusBindingProvider.TYPE_INPUT.equals(getType())) {
                ModbusRequest request = new ReadInputRegistersRequest(getStart(), getLength());
                ReadInputRegistersResponse response = (ReadInputRegistersResponse) getModbusData(request);
                local = response.getRegisters();
            }
        } catch (ModbusException e) {
            // Logging already done in getModbusData
            localReadError = e;
        } catch (ModbusConnectionException e) {
            // Logging already done in getModbusData
            localReadError = e;
        } catch (ModbusUnexpectedTransactionIdException e) {
            // Logging already done in getModbusData
            localReadError = e;
        }
        if (storage == null) {
            storage = local;
            readError = localReadError;
        } else {
            synchronized (storage) {
                storage = local;
                readError = localReadError;
            }
        }
        Collection<String> items = binding.getItemNames();
        for (String item : items) {
            updateItem(binding, item);
        }
    } catch (Exception e) {
        logger.error("ModbusSlave ({}) error getting response from slave", name, e);
    }
}
Also used : ReadInputDiscretesResponse(net.wimpi.modbus.msg.ReadInputDiscretesResponse) ReadInputDiscretesRequest(net.wimpi.modbus.msg.ReadInputDiscretesRequest) ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) ReadCoilsResponse(net.wimpi.modbus.msg.ReadCoilsResponse) ModbusException(net.wimpi.modbus.ModbusException) ReadInputRegistersResponse(net.wimpi.modbus.msg.ReadInputRegistersResponse) ModbusException(net.wimpi.modbus.ModbusException) ReadMultipleRegistersRequest(net.wimpi.modbus.msg.ReadMultipleRegistersRequest) ReadCoilsRequest(net.wimpi.modbus.msg.ReadCoilsRequest) ReadInputRegistersRequest(net.wimpi.modbus.msg.ReadInputRegistersRequest) ReadMultipleRegistersResponse(net.wimpi.modbus.msg.ReadMultipleRegistersResponse)

Aggregations

ModbusException (net.wimpi.modbus.ModbusException)1 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)1 ReadCoilsRequest (net.wimpi.modbus.msg.ReadCoilsRequest)1 ReadCoilsResponse (net.wimpi.modbus.msg.ReadCoilsResponse)1 ReadInputDiscretesRequest (net.wimpi.modbus.msg.ReadInputDiscretesRequest)1 ReadInputDiscretesResponse (net.wimpi.modbus.msg.ReadInputDiscretesResponse)1 ReadInputRegistersRequest (net.wimpi.modbus.msg.ReadInputRegistersRequest)1 ReadInputRegistersResponse (net.wimpi.modbus.msg.ReadInputRegistersResponse)1 ReadMultipleRegistersRequest (net.wimpi.modbus.msg.ReadMultipleRegistersRequest)1 ReadMultipleRegistersResponse (net.wimpi.modbus.msg.ReadMultipleRegistersResponse)1