Search in sources :

Example 1 with WriteCoilRequest

use of com.ghgande.j2mod.modbus.msg.WriteCoilRequest in project openems by OpenEMS.

the class ModbusBridge method writeSingleCoil.

private ModbusResponse writeSingleCoil(int modbusUnitId, int address, boolean b) throws OpenemsModbusException, ModbusException {
    ModbusTransaction trans = getTransaction();
    WriteCoilRequest req = new WriteCoilRequest(address, b);
    req.setUnitID(modbusUnitId);
    trans.setRequest(req);
    try {
        trans.execute();
    } catch (ModbusException e) {
        // try again with new connection
        closeModbusConnection();
        trans = getTransaction();
        req = new WriteCoilRequest(address, b);
        req.setUnitID(modbusUnitId);
        trans.setRequest(req);
        trans.execute();
    }
    return trans.getResponse();
}
Also used : ModbusTransaction(com.ghgande.j2mod.modbus.io.ModbusTransaction) WriteCoilRequest(com.ghgande.j2mod.modbus.msg.WriteCoilRequest) ModbusException(com.ghgande.j2mod.modbus.ModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException)

Example 2 with WriteCoilRequest

use of com.ghgande.j2mod.modbus.msg.WriteCoilRequest in project SmartApplianceEnabler by camueller.

the class WriteCoilExecutor method execute.

@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
    WriteCoilRequest req = new WriteCoilRequest(Integer.parseInt(registerAddress, 16), coil);
    req.setUnitID(slaveAddress);
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    trans.execute();
    WriteCoilResponse res = (WriteCoilResponse) trans.getResponse();
    if (res != null) {
        result = res.getCoil();
        logger.debug("{}: Write coil register={} coil={} result={}", applianceId, registerAddress, coil, result);
    } else {
        logger.error("{}: No response received.", applianceId);
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) WriteCoilResponse(com.ghgande.j2mod.modbus.msg.WriteCoilResponse) WriteCoilRequest(com.ghgande.j2mod.modbus.msg.WriteCoilRequest)

Example 3 with WriteCoilRequest

use of com.ghgande.j2mod.modbus.msg.WriteCoilRequest in project SmartApplianceEnabler by camueller.

the class WriteCoilExecutorImpl method execute.

@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
    WriteCoilRequest req = new WriteCoilRequest(getAddress(), value);
    req.setUnitID(slaveAddress);
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    trans.execute();
    WriteCoilResponse res = (WriteCoilResponse) trans.getResponse();
    if (res != null) {
        result = res.getCoil();
        logger.debug("{}: Write coil register={} coil={} confirmedValue={}", getApplianceId(), getAddress(), value, result);
    } else {
        logger.error("{}: No response received.", getApplianceId());
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) WriteCoilResponse(com.ghgande.j2mod.modbus.msg.WriteCoilResponse) WriteCoilRequest(com.ghgande.j2mod.modbus.msg.WriteCoilRequest)

Aggregations

WriteCoilRequest (com.ghgande.j2mod.modbus.msg.WriteCoilRequest)3 ModbusTCPTransaction (com.ghgande.j2mod.modbus.io.ModbusTCPTransaction)2 WriteCoilResponse (com.ghgande.j2mod.modbus.msg.WriteCoilResponse)2 ModbusException (com.ghgande.j2mod.modbus.ModbusException)1 ModbusTransaction (com.ghgande.j2mod.modbus.io.ModbusTransaction)1 OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)1