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);
}
}
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;
}
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;
}
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;
}
Aggregations