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);
}
}
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());
}
}
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());
}
}
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);
}
}
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;
}
Aggregations