use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.
the class ModbusBridge method writeSingleRegister.
private ModbusResponse writeSingleRegister(int modbusUnitId, int address, Register register) throws ModbusException, OpenemsModbusException {
ModbusTransaction trans = getTransaction();
WriteSingleRegisterRequest req = new WriteSingleRegisterRequest(address, register);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
try {
trans.execute();
} catch (ModbusException e) {
// try again with new connection
closeModbusConnection();
trans = getTransaction();
req = new WriteSingleRegisterRequest(address, register);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
trans.execute();
}
return trans.getResponse();
}
use of com.ghgande.j2mod.modbus.ModbusException 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();
}
use of com.ghgande.j2mod.modbus.ModbusException 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());
}
}
use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.
the class ModbusBridge method writeMultipleRegisters.
/**
* Write Multiple Registers (function code 16)
*
* @param modbusUnitId
* @param address
* @param register
* @throws OpenemsModbusException
* @throws ModbusException
*/
private ModbusResponse writeMultipleRegisters(int modbusUnitId, int address, Register... register) throws OpenemsModbusException, ModbusException {
ModbusTransaction trans = getTransaction();
WriteMultipleRegistersRequest req = new WriteMultipleRegistersRequest(address, register);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
try {
trans.execute();
} catch (ModbusException e) {
// try again with new connection
closeModbusConnection();
trans = getTransaction();
req = new WriteMultipleRegistersRequest(address, register);
req.setUnitID(modbusUnitId);
trans.setRequest(req);
trans.execute();
}
return trans.getResponse();
}
use of com.ghgande.j2mod.modbus.ModbusException in project openems by OpenEMS.
the class ModbusTcpApiController method restartSlave.
protected void restartSlave(Optional<Integer> portOpt) {
// remove and close old slave if existing
if (this.slaveOpt.isPresent()) {
ModbusSlave oldSlave = this.slaveOpt.get();
oldSlave.close();
this.slaveOpt = Optional.empty();
}
// create new slave, initialize and start
try {
if (!portOpt.isPresent()) {
throw new OpenemsException("Port was not set");
}
int port = portOpt.get();
ModbusSlave newSlave = ModbusSlaveFactory.createTCPSlave(port, MAX_CONCURRENT_CONNECTIONS);
newSlave.addProcessImage(UNIT_ID, this.processImage);
newSlave.open();
// TODO slave should be closed on dispose of Controller
log.info("Modbus/TCP Api started on port [" + port + "] with UnitId [" + UNIT_ID + "].");
this.slaveOpt = Optional.of(newSlave);
} catch (OpenemsException | ModbusException e) {
log.error("Unable to start Modbus/TCP slave: " + e.getMessage());
}
}
Aggregations