Search in sources :

Example 1 with ReadCoilsResponse

use of net.wimpi.modbus.msg.ReadCoilsResponse 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)

Example 2 with ReadCoilsResponse

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

the class ModbusUDPMaster method readCoils.

// disconnect
/**
     * Reads a given number of coil states from the slave.
     * <p/>
     * Note that the number of bits in the bit vector will be
     * forced to the number originally requested.
     *
     * @param ref the offset of the coil to start reading from.
     * @param count the number of coil states to be read.
     * @return a <tt>BitVector</tt> instance holding the
     *         received coil states.
     * @throws ModbusException if an I/O error, a slave exception or
     *             a transaction error occurs.
     */
public synchronized BitVector readCoils(int ref, int count) throws ModbusException {
    m_ReadCoilsRequest.setReference(ref);
    m_ReadCoilsRequest.setBitCount(count);
    m_Transaction.setRequest(m_ReadCoilsRequest);
    m_Transaction.execute();
    BitVector bv = ((ReadCoilsResponse) m_Transaction.getResponse()).getCoils();
    bv.forceSize(count);
    return bv;
}
Also used : BitVector(net.wimpi.modbus.util.BitVector) ReadCoilsResponse(net.wimpi.modbus.msg.ReadCoilsResponse)

Example 3 with ReadCoilsResponse

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

the class ModbusSerialMaster method readCoils.

// disconnect
/**
     * Reads a given number of coil states from the slave.
     * <p/>
     * Note that the number of bits in the bit vector will be
     * forced to the number originally requested.
     *
     * @param unitid (IN) the slave unit id.
     * @param ref the offset of the coil to start reading from.
     * @param count the number of coil states to be read.
     * @return a <tt>BitVector</tt> instance holding the
     *         received coil states.
     * @throws ModbusException if an I/O error, a slave exception or
     *             a transaction error occurs.
     */
public synchronized BitVector readCoils(int unitid, int ref, int count) throws ModbusException {
    m_ReadCoilsRequest.setUnitID(unitid);
    m_ReadCoilsRequest.setReference(ref);
    m_ReadCoilsRequest.setBitCount(count);
    m_Transaction.setRequest(m_ReadCoilsRequest);
    m_Transaction.execute();
    BitVector bv = ((ReadCoilsResponse) m_Transaction.getResponse()).getCoils();
    bv.forceSize(count);
    return bv;
}
Also used : BitVector(net.wimpi.modbus.util.BitVector) ReadCoilsResponse(net.wimpi.modbus.msg.ReadCoilsResponse)

Example 4 with ReadCoilsResponse

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

the class ModbusTCPMaster method readCoils.

// isReconnecting
/**
     * Reads a given number of coil states from the slave.
     * <p/>
     * Note that the number of bits in the bit vector will be
     * forced to the number originally requested.
     *
     * @param ref the offset of the coil to start reading from.
     * @param count the number of coil states to be read.
     * @return a <tt>BitVector</tt> instance holding the
     *         received coil states.
     * @throws ModbusException if an I/O error, a slave exception or
     *             a transaction error occurs.
     */
public synchronized BitVector readCoils(int ref, int count) throws ModbusException {
    m_ReadCoilsRequest.setReference(ref);
    m_ReadCoilsRequest.setBitCount(count);
    m_Transaction.setRequest(m_ReadCoilsRequest);
    m_Transaction.execute();
    BitVector bv = ((ReadCoilsResponse) m_Transaction.getResponse()).getCoils();
    bv.forceSize(count);
    return bv;
}
Also used : BitVector(net.wimpi.modbus.util.BitVector) ReadCoilsResponse(net.wimpi.modbus.msg.ReadCoilsResponse)

Aggregations

ReadCoilsResponse (net.wimpi.modbus.msg.ReadCoilsResponse)4 BitVector (net.wimpi.modbus.util.BitVector)3 ModbusException (net.wimpi.modbus.ModbusException)1 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)1 ReadCoilsRequest (net.wimpi.modbus.msg.ReadCoilsRequest)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