use of com.ghgande.j2mod.modbus.msg.WriteMultipleCoilsRequest in project openems by OpenEMS.
the class ModbusBridge method writeMultipleCoils.
private ModbusResponse writeMultipleCoils(int modbusUnitId, int address, boolean[] coils) throws OpenemsModbusException, ModbusException {
ModbusTransaction trans = getTransaction();
BitVector vec = new BitVector(coils.length);
for (int i = 0; i < coils.length; i++) {
vec.setBit(i, coils[i]);
}
WriteMultipleCoilsRequest req = new WriteMultipleCoilsRequest(address, vec);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
try {
trans.execute();
} catch (ModbusException e) {
// try again with new connection
closeModbusConnection();
trans = getTransaction();
req = new WriteMultipleCoilsRequest(address, vec);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
trans.execute();
}
return trans.getResponse();
}
Aggregations