Search in sources :

Example 1 with MutableAddressModbusSlave

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;
    }
}
Also used : MutableAddressModbusSlave(me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) RtuDataEncoder(me.retrodaredevil.io.modbus.RtuDataEncoder) ModbusTimeoutException(me.retrodaredevil.io.modbus.ModbusTimeoutException) ModbusRuntimeException(me.retrodaredevil.io.modbus.ModbusRuntimeException) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) ModbusSlaveBus(me.retrodaredevil.io.modbus.ModbusSlaveBus) JSerialIOBundle(me.retrodaredevil.io.serial.JSerialIOBundle) BatteryVoltage(me.retrodaredevil.solarthing.solar.common.BatteryVoltage)

Example 2 with MutableAddressModbusSlave

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;
    }
}
Also used : RoverModbusSlaveRead(me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveRead) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) ModbusCacheSlave(me.retrodaredevil.solarthing.program.modbus.ModbusCacheSlave) IOConfig(me.retrodaredevil.solarthing.config.io.IOConfig) RoverReadTable(me.retrodaredevil.solarthing.solar.renogy.rover.RoverReadTable) MutableAddressModbusSlave(me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave) RtuDataEncoder(me.retrodaredevil.io.modbus.RtuDataEncoder) RoverModbusSlaveWrite(me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveWrite) ReloadableIOBundle(me.retrodaredevil.solarthing.io.ReloadableIOBundle) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) ModbusSlaveBus(me.retrodaredevil.io.modbus.ModbusSlaveBus) RoverWriteTable(me.retrodaredevil.solarthing.solar.renogy.rover.RoverWriteTable)

Aggregations

IOModbusSlaveBus (me.retrodaredevil.io.modbus.IOModbusSlaveBus)2 ModbusSlaveBus (me.retrodaredevil.io.modbus.ModbusSlaveBus)2 RtuDataEncoder (me.retrodaredevil.io.modbus.RtuDataEncoder)2 MutableAddressModbusSlave (me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave)2 ModbusRuntimeException (me.retrodaredevil.io.modbus.ModbusRuntimeException)1 ModbusTimeoutException (me.retrodaredevil.io.modbus.ModbusTimeoutException)1 JSerialIOBundle (me.retrodaredevil.io.serial.JSerialIOBundle)1 IOConfig (me.retrodaredevil.solarthing.config.io.IOConfig)1 ReloadableIOBundle (me.retrodaredevil.solarthing.io.ReloadableIOBundle)1 ModbusCacheSlave (me.retrodaredevil.solarthing.program.modbus.ModbusCacheSlave)1 BatteryVoltage (me.retrodaredevil.solarthing.solar.common.BatteryVoltage)1 RoverReadTable (me.retrodaredevil.solarthing.solar.renogy.rover.RoverReadTable)1 RoverWriteTable (me.retrodaredevil.solarthing.solar.renogy.rover.RoverWriteTable)1 RoverModbusSlaveRead (me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveRead)1 RoverModbusSlaveWrite (me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveWrite)1