Search in sources :

Example 1 with ModbusTCPTransaction

use of com.ghgande.j2mod.modbus.io.ModbusTCPTransaction in project SmartApplianceEnabler by camueller.

the class ReadInputRegisterExecutor method execute.

@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
    ReadInputRegistersRequest req = new ReadInputRegistersRequest(Integer.parseInt(registerAddress, 16), 2);
    req.setUnitID(slaveAddress);
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    trans.execute();
    ReadInputRegistersResponse res = (ReadInputRegistersResponse) trans.getResponse();
    if (res != null) {
        registerValue = Float.intBitsToFloat(res.getRegisterValue(0) << 16 | res.getRegisterValue(1));
        logger.debug("{}: Input register={} value={}", applianceId, registerAddress, registerValue);
    } else {
        logger.error("{}: No response received.", applianceId);
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) ReadInputRegistersRequest(com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest) ReadInputRegistersResponse(com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse)

Example 2 with ModbusTCPTransaction

use of com.ghgande.j2mod.modbus.io.ModbusTCPTransaction 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 3 with ModbusTCPTransaction

use of com.ghgande.j2mod.modbus.io.ModbusTCPTransaction in project SmartApplianceEnabler by camueller.

the class ReadInputRegisterExecutor method execute.

@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
    logger.trace("{}: Reading input register={} requestWords={}", getApplianceId(), getAddress(), getRequestWords());
    ReadInputRegistersRequest req = new ReadInputRegistersRequest(getAddress(), getRequestWords());
    req.setUnitID(slaveAddress);
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    trans.execute();
    ReadInputRegistersResponse res = (ReadInputRegistersResponse) trans.getResponse();
    if (res != null) {
        Integer[] byteValues = new Integer[res.getWordCount()];
        for (int i = 0; i < res.getWordCount(); i++) {
            byteValues[i] = res.getRegisterValue(i);
        }
        logger.debug("{}: Input register={} value={}", getApplianceId(), getAddress(), byteValues);
        getValueTransformer().setByteValues(byteValues);
    } else {
        logger.error("{}: No response received.", getApplianceId());
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) ReadInputRegistersRequest(com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest) ReadInputRegistersResponse(com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse)

Example 4 with ModbusTCPTransaction

use of com.ghgande.j2mod.modbus.io.ModbusTCPTransaction 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 5 with ModbusTCPTransaction

use of com.ghgande.j2mod.modbus.io.ModbusTCPTransaction in project SmartApplianceEnabler by camueller.

the class ReadDiscreteInputExecutorImpl method execute.

@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
    logger.trace("{}: Reading discrete input register={}", getApplianceId(), getAddress());
    ReadInputDiscretesRequest req = new ReadInputDiscretesRequest(getAddress(), 1);
    req.setUnitID(slaveAddress);
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    trans.execute();
    ReadInputDiscretesResponse res = (ReadInputDiscretesResponse) trans.getResponse();
    if (res != null) {
        discrete = res.getDiscreteStatus(0);
        logger.debug("{}: Discrete input register={} value={}", getApplianceId(), getAddress(), discrete);
    } else {
        logger.error("{}: No response received.", getApplianceId());
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) ReadInputDiscretesResponse(com.ghgande.j2mod.modbus.msg.ReadInputDiscretesResponse) ReadInputDiscretesRequest(com.ghgande.j2mod.modbus.msg.ReadInputDiscretesRequest)

Aggregations

ModbusTCPTransaction (com.ghgande.j2mod.modbus.io.ModbusTCPTransaction)10 ReadCoilsRequest (com.ghgande.j2mod.modbus.msg.ReadCoilsRequest)2 ReadCoilsResponse (com.ghgande.j2mod.modbus.msg.ReadCoilsResponse)2 ReadInputRegistersRequest (com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest)2 ReadInputRegistersResponse (com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse)2 WriteCoilRequest (com.ghgande.j2mod.modbus.msg.WriteCoilRequest)2 WriteCoilResponse (com.ghgande.j2mod.modbus.msg.WriteCoilResponse)2 ReadInputDiscretesRequest (com.ghgande.j2mod.modbus.msg.ReadInputDiscretesRequest)1 ReadInputDiscretesResponse (com.ghgande.j2mod.modbus.msg.ReadInputDiscretesResponse)1 ReadMultipleRegistersRequest (com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest)1 ReadMultipleRegistersResponse (com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse)1 TCPMasterConnection (com.ghgande.j2mod.modbus.net.TCPMasterConnection)1 SimpleRegister (com.ghgande.j2mod.modbus.procimg.SimpleRegister)1