use of me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave in project solarthing by wildmountainfarms.
the class CheckMain method doModbus.
private static boolean doModbus(@NotNull String port, int startingAddress, boolean scan, SerialConfig serialConfig, Function<ModbusSlave, BatteryVoltage> slaveToReadTable) throws SerialPortException {
System.out.println("Going to open serial port using default serial configuration...");
try (JSerialIOBundle ioBundle = JSerialIOBundle.createPort(port, serialConfig)) {
System.out.println("Successfully opened serial port...");
ModbusSlaveBus bus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder());
MutableAddressModbusSlave modbusSlave = new MutableAddressModbusSlave(startingAddress, bus);
BatteryVoltage readTable = slaveToReadTable.apply(modbusSlave);
int maxAddress = scan ? 247 : startingAddress;
for (int currentAddress = startingAddress; currentAddress <= maxAddress; currentAddress++) {
modbusSlave.setAddress(currentAddress);
System.out.println("Checking on address: " + currentAddress);
try {
float batteryVoltage = readTable.getBatteryVoltage();
System.out.println("Success! Battery Voltage: " + batteryVoltage);
return true;
} catch (ModbusTimeoutException e) {
System.err.println("Got timeout. This means that the modbus address is incorrect or that the cable is not functioning properly.");
} catch (ModbusRuntimeException e) {
e.printStackTrace();
System.err.println("Got some sort of modbus error. Info logged above");
}
}
System.err.println("Did not find a device");
return false;
}
}
use of me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave in project solarthing by wildmountainfarms.
the class RoverMain method doRoverProgram.
private static int doRoverProgram(RoverOption options, RoverProgramRunner runner, @Nullable RegisterCacheHandler registerCacheHandler) {
IOConfig ioConfig = ConfigUtil.parseIOConfig(options.getIOBundleFile(), RoverReadTable.SERIAL_CONFIG);
try (ReloadableIOBundle ioBundle = new ReloadableIOBundle(ioConfig::createIOBundle)) {
ModbusSlaveBus modbus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder(2000, 20, 4));
MutableAddressModbusSlave slave = new MutableAddressModbusSlave(options.getModbusAddress(), modbus);
final RoverReadTable read;
final Runnable reloadCache;
if (registerCacheHandler != null) {
ModbusCacheSlave modbusCacheSlave = new ModbusCacheSlave(slave);
read = new RoverModbusSlaveRead(modbusCacheSlave);
reloadCache = () -> registerCacheHandler.cacheRegisters(modbusCacheSlave);
} else {
read = new RoverModbusSlaveRead(slave);
reloadCache = () -> {
};
}
RoverWriteTable write = new RoverModbusSlaveWrite(slave);
return runner.doProgram(slave, read, write, reloadCache, ioBundle::reload);
} catch (Exception e) {
LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Got exception!", e);
return SolarThingConstants.EXIT_CODE_CRASH;
}
}
Aggregations