use of com.ghgande.j2mod.modbus.io.ModbusTransaction 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());
}
}
use of com.ghgande.j2mod.modbus.io.ModbusTransaction in project openems by OpenEMS.
the class ModbusBridge method writeMultipleRegisters.
/**
* Write Multiple Registers (function code 16)
*
* @param modbusUnitId
* @param address
* @param register
* @throws OpenemsModbusException
* @throws ModbusException
*/
private ModbusResponse writeMultipleRegisters(int modbusUnitId, int address, Register... register) throws OpenemsModbusException, ModbusException {
ModbusTransaction trans = getTransaction();
WriteMultipleRegistersRequest req = new WriteMultipleRegistersRequest(address, register);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
try {
trans.execute();
} catch (ModbusException e) {
// try again with new connection
closeModbusConnection();
trans = getTransaction();
req = new WriteMultipleRegistersRequest(address, register);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
trans.execute();
}
return trans.getResponse();
}
Aggregations