use of com.ghgande.j2mod.modbus.net.SerialConnection 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 com.ghgande.j2mod.modbus.net.SerialConnection in project openems by OpenEMS.
the class ModbusRtu method getTransaction.
@Override
public ModbusTransaction getTransaction() throws OpenemsModbusException {
SerialConnection connection = getModbusConnection();
ModbusSerialTransaction trans = new ModbusSerialTransaction(connection);
trans.setRetries(0);
return trans;
}
Aggregations