Search in sources :

Example 1 with ReadCoilsRequest

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);
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) ReadCoilsRequest(com.ghgande.j2mod.modbus.msg.ReadCoilsRequest) ReadCoilsResponse(com.ghgande.j2mod.modbus.msg.ReadCoilsResponse)

Example 2 with ReadCoilsRequest

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());
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) ReadCoilsRequest(com.ghgande.j2mod.modbus.msg.ReadCoilsRequest) ReadCoilsResponse(com.ghgande.j2mod.modbus.msg.ReadCoilsResponse)

Example 3 with ReadCoilsRequest

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

Aggregations

ReadCoilsRequest (com.ghgande.j2mod.modbus.msg.ReadCoilsRequest)3 ReadCoilsResponse (com.ghgande.j2mod.modbus.msg.ReadCoilsResponse)3 ModbusTCPTransaction (com.ghgande.j2mod.modbus.io.ModbusTCPTransaction)2 ModbusException (com.ghgande.j2mod.modbus.ModbusException)1 ModbusTransaction (com.ghgande.j2mod.modbus.io.ModbusTransaction)1 ModbusResponse (com.ghgande.j2mod.modbus.msg.ModbusResponse)1 OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)1