Search in sources :

Example 1 with WriteMultipleCoilsRequest

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();
}
Also used : BitVector(com.ghgande.j2mod.modbus.util.BitVector) WriteMultipleCoilsRequest(com.ghgande.j2mod.modbus.msg.WriteMultipleCoilsRequest) ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Aggregations

ModbusException (com.ghgande.j2mod.modbus.ModbusException)1 ModbusTransaction (com.ghgande.j2mod.modbus.io.ModbusTransaction)1 WriteMultipleCoilsRequest (com.ghgande.j2mod.modbus.msg.WriteMultipleCoilsRequest)1 BitVector (com.ghgande.j2mod.modbus.util.BitVector)1 OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)1