Search in sources :

Example 6 with ModbusRuntimeException

use of me.retrodaredevil.io.modbus.ModbusRuntimeException 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 7 with ModbusRuntimeException

use of me.retrodaredevil.io.modbus.ModbusRuntimeException in project solarthing by wildmountainfarms.

the class PzemShuntPacketListUpdater method receive.

@Override
public void receive(List<Packet> packets) {
    final PzemShuntStatusPacket packet;
    try {
        packet = ImmutablePzemShuntStatusPacket.createFromReadTable(dataId, modbusAddress, read);
    } catch (ModbusRuntimeException ex) {
        LOGGER.error("Modbus exception", ex);
        return;
    }
    packets.add(packet);
}
Also used : ImmutablePzemShuntStatusPacket(me.retrodaredevil.solarthing.solar.pzem.ImmutablePzemShuntStatusPacket) PzemShuntStatusPacket(me.retrodaredevil.solarthing.solar.pzem.PzemShuntStatusPacket) ModbusRuntimeException(me.retrodaredevil.io.modbus.ModbusRuntimeException)

Aggregations

ModbusRuntimeException (me.retrodaredevil.io.modbus.ModbusRuntimeException)7 ModbusTimeoutException (me.retrodaredevil.io.modbus.ModbusTimeoutException)2 RoverModbusEnvironment (me.retrodaredevil.solarthing.actions.environment.RoverModbusEnvironment)2 RoverWriteTable (me.retrodaredevil.solarthing.solar.renogy.rover.RoverWriteTable)2 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 IOModbusSlaveBus (me.retrodaredevil.io.modbus.IOModbusSlaveBus)1 ModbusMessage (me.retrodaredevil.io.modbus.ModbusMessage)1 ModbusSlaveBus (me.retrodaredevil.io.modbus.ModbusSlaveBus)1 RtuDataEncoder (me.retrodaredevil.io.modbus.RtuDataEncoder)1 FunctionCodeException (me.retrodaredevil.io.modbus.handling.FunctionCodeException)1 ParsedResponseException (me.retrodaredevil.io.modbus.handling.ParsedResponseException)1 RawResponseException (me.retrodaredevil.io.modbus.handling.RawResponseException)1 JSerialIOBundle (me.retrodaredevil.io.serial.JSerialIOBundle)1 TracerModbusEnvironment (me.retrodaredevil.solarthing.actions.environment.TracerModbusEnvironment)1 NotInitializedIOException (me.retrodaredevil.solarthing.io.NotInitializedIOException)1 ImmutableExceptionErrorPacket (me.retrodaredevil.solarthing.misc.error.ImmutableExceptionErrorPacket)1 MutableAddressModbusSlave (me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave)1 BatteryVoltage (me.retrodaredevil.solarthing.solar.common.BatteryVoltage)1