use of com.ghgande.j2mod.modbus.msg.ReadCoilsRequest in project SmartApplianceEnabler by camueller.
the class ReadCoilExecutor method execute.
@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
ReadCoilsRequest req = new ReadCoilsRequest(Integer.parseInt(registerAddress, 16), 1);
req.setUnitID(slaveAddress);
ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
trans.execute();
ReadCoilsResponse res = (ReadCoilsResponse) trans.getResponse();
if (res != null) {
coil = res.getCoils().getBit(0);
logger.debug("{}: Read coil register={} coil={}", applianceId, registerAddress, coil);
} else {
logger.error("{}: No response received.", applianceId);
}
}
use of com.ghgande.j2mod.modbus.msg.ReadCoilsRequest in project SmartApplianceEnabler by camueller.
the class ReadCoilExecutorImpl method execute.
@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
logger.trace("{}: Reading coil register={}", getApplianceId(), getAddress());
ReadCoilsRequest req = new ReadCoilsRequest(getAddress(), 1);
req.setUnitID(slaveAddress);
ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
trans.execute();
ReadCoilsResponse res = (ReadCoilsResponse) trans.getResponse();
if (res != null) {
coil = res.getCoils().getBit(0);
logger.debug("{}: Coil register={} value={}", getApplianceId(), getAddress(), coil);
} else {
logger.error("{}: No response received.", getApplianceId());
}
}
use of com.ghgande.j2mod.modbus.msg.ReadCoilsRequest in project openems by OpenEMS.
the class ModbusBridge method queryCoils.
private boolean[] queryCoils(int modbusUnitId, int startAddress, int length) throws OpenemsModbusException {
ModbusTransaction trans = getTransaction();
ReadCoilsRequest req = new ReadCoilsRequest(startAddress, length);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
try {
trans.execute();
} catch (ModbusException e) {
// try again with new connection
closeModbusConnection();
trans = getTransaction();
req = new ReadCoilsRequest(startAddress, length);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
try {
trans.execute();
} catch (ModbusException e1) {
throw new OpenemsModbusException(//
"Error on modbus query. " + "UnitId [" + modbusUnitId + "], Address [" + startAddress + "/0x" + Integer.toHexString(startAddress) + "], Count [" + length + "]: " + e1.getMessage());
}
}
ModbusResponse res = trans.getResponse();
if (res instanceof ReadCoilsResponse) {
ReadCoilsResponse mres = (ReadCoilsResponse) res;
return toBooleanArray(mres.getCoils().getBytes());
} else {
throw new OpenemsModbusException(//
"Unable to read modbus response. " + "UnitId [" + modbusUnitId + "], Address [" + startAddress + "], Count [" + length + "]: " + res.toString());
}
}
Aggregations