Search in sources :

Example 6 with ModbusTCPTransaction

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

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

the class ReadHoldingRegisterExecutor method execute.

@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
    logger.trace("{}: Reading holding register={} requestWords={}", getApplianceId(), getAddress(), getRequestWords());
    ReadMultipleRegistersRequest req = new ReadMultipleRegistersRequest(getAddress(), getRequestWords());
    req.setUnitID(slaveAddress);
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    trans.execute();
    ReadMultipleRegistersResponse res = (ReadMultipleRegistersResponse) 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("{}: Holding register={} value={}", getApplianceId(), getAddress(), byteValues);
        getValueTransformer().setByteValues(byteValues);
    } else {
        logger.error("{}: No response received.", getApplianceId());
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) ReadMultipleRegistersRequest(com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest) ReadMultipleRegistersResponse(com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse)

Example 8 with ModbusTCPTransaction

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

Example 9 with ModbusTCPTransaction

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

the class WriteHoldingRegisterExecutorImpl method execute.

@Override
public void execute(TCPMasterConnection con, int slaveAddress) throws ModbusException {
    logger.debug("{}: Write holding register={} value={}", getApplianceId(), getAddress(), this.value);
    Integer factoredValue = factorToValue != null ? Double.valueOf(this.value * this.factorToValue).intValue() : this.value;
    logger.debug("{}: Write holding register={} factoredValue={}", getApplianceId(), getAddress(), factoredValue);
    SimpleRegister register = new SimpleRegister(factoredValue);
    WriteSingleRegisterRequest req = new WriteSingleRegisterRequest(getAddress(), register);
    req.setUnitID(slaveAddress);
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    trans.execute();
    WriteSingleRegisterResponse res = (WriteSingleRegisterResponse) trans.getResponse();
    if (res != null) {
        this.result = res.getRegisterValue();
        logger.debug("{}: Write holding register={} confirmedValue={}", getApplianceId(), getAddress(), this.result);
    } else {
        logger.error("{}: No response received: register={} value={} ", getApplianceId(), getAddress(), factoredValue);
    }
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) SimpleRegister(com.ghgande.j2mod.modbus.procimg.SimpleRegister)

Example 10 with ModbusTCPTransaction

use of com.ghgande.j2mod.modbus.io.ModbusTCPTransaction in project openems by OpenEMS.

the class ModbusTcp method getTransaction.

@Override
public ModbusTransaction getTransaction() throws OpenemsModbusException {
    TCPMasterConnection connection = getModbusConnection();
    ModbusTCPTransaction trans = new ModbusTCPTransaction(connection);
    return trans;
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) TCPMasterConnection(com.ghgande.j2mod.modbus.net.TCPMasterConnection)

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