use of io.openems.api.exception.OpenemsModbusException 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 io.openems.api.exception.OpenemsModbusException 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 io.openems.api.exception.OpenemsModbusException in project openems by OpenEMS.
the class ModbusRtu method getModbusConnection.
private SerialConnection getModbusConnection() throws OpenemsModbusException {
if (!connection.isPresent()) {
if (!baudrate.valueOptional().isPresent() || !databits.valueOptional().isPresent() || !parity.valueOptional().isPresent() || !serialinterface.valueOptional().isPresent() || !stopbits.valueOptional().isPresent()) {
this.configurationFault.setValue(true);
throw new OpenemsModbusException(this.id() + ": Modbus-RTU is not configured completely");
}
SerialParameters params = new SerialParameters();
params.setPortName(serialinterface.valueOptional().get());
params.setBaudRate(baudrate.valueOptional().get());
params.setDatabits(databits.valueOptional().get());
params.setParity(parity.valueOptional().get());
params.setStopbits(stopbits.valueOptional().get());
params.setEncoding(Modbus.SERIAL_ENCODING_RTU);
params.setEcho(false);
connection = Optional.of(new SerialConnection(params));
this.configurationFault.setValue(false);
}
if (!connection.get().isOpen()) {
try {
SerialConnection serialCon = connection.get();
serialCon.open();
serialCon.getModbusTransport().setTimeout(1000);
this.connectionFault.setValue(false);
} catch (Exception e) {
this.connectionFault.setValue(true);
throw new OpenemsModbusException(this.id() + ": Unable to open Modbus-RTU connection to [" + serialinterface.valueOptional().orElse("UNDEFINED") + "]: " + e.getMessage());
}
}
return connection.get();
}
use of io.openems.api.exception.OpenemsModbusException 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 io.openems.api.exception.OpenemsModbusException 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();
}
Aggregations