Search in sources :

Example 1 with JSerialIOBundle

use of me.retrodaredevil.io.serial.JSerialIOBundle in project solarthing by wildmountainfarms.

the class SerialIOConfig method createIOBundle.

@Override
public IOBundle createIOBundle() throws Exception {
    SerialConfig serialConfig = this.serialConfig;
    if (serialConfig == null) {
        serialConfig = defaultSerialConfig;
        if (serialConfig == null) {
            throw new NullPointerException("serialConfig is null! This is likely because the injection didn't work properly.");
        }
    }
    String port = this.port;
    if (port == null) {
        throw new NullPointerException("null is not a valid value for the port!");
    }
    SerialPort serialPort = JSerialIOBundle.createSerialPortFromName(port);
    return new JSerialIOBundle(serialPort, serialConfig, new JSerialIOBundle.Config(200, 65536, 65536));
}
Also used : SerialPort(com.fazecast.jSerialComm.SerialPort) SerialConfig(me.retrodaredevil.io.serial.SerialConfig) JSerialIOBundle(me.retrodaredevil.io.serial.JSerialIOBundle)

Example 2 with JSerialIOBundle

use of me.retrodaredevil.io.serial.JSerialIOBundle in project solarthing by wildmountainfarms.

the class CheckMain method scanForMate.

/*
	Useful command when testing this:
	socat -d -d pty,raw,echo=0 pty,raw,echo=0
	 */
private static boolean scanForMate(String port) throws SerialPortException {
    System.out.println("Going to open serial port using MATE's default serial configuration...");
    try (JSerialIOBundle ioBundle = JSerialIOBundle.createPort(port, OutbackConstants.MATE_CONFIG)) {
        System.out.println("Successfully opened serial port...");
        boolean[] gotAnyData = new boolean[] { false };
        boolean[] parsedData = new boolean[] { false };
        PacketListReceiver receiver = (packets) -> {
            System.out.println("Got " + packets.size() + " packets");
            parsedData[0] = true;
        };
        // Note that lots of this code logs. Users don't see much of the debug logs because they run the solarthing command, which does INFO logging by default
        SolarReader solarReader = new SolarReader(ioBundle.getInputStream(), // we are testing a lot, so we don't care if the checksum is wrong
        new MatePacketCreator49(IgnoreCheckSum.IGNORE_AND_USE_CALCULATED), new TimedPacketReceiver(Duration.ofMillis(250), receiver, (firstData, stale) -> gotAnyData[0] = true));
        try {
            for (int i = 0; i < 40; i++) {
                // do this for about 4 seconds
                try {
                    solarReader.update();
                } catch (EOFException e) {
                    // This should never happen
                    System.err.println("The serial port gave us an EOF. This is unexpected");
                    return false;
                } catch (IOException e) {
                    System.err.println("Error reading from serial port");
                    return false;
                }
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
            return false;
        }
        if (parsedData[0]) {
            System.out.println("Got data from mate!");
        } else if (gotAnyData[0]) {
            System.out.println("Got some data, but was unable to parse it.");
        } else {
            System.out.println("Got no data from mate!");
        }
        return parsedData[0];
    }
}
Also used : RoverReadTable(me.retrodaredevil.solarthing.solar.renogy.rover.RoverReadTable) TracerModbusSlaveRead(me.retrodaredevil.solarthing.solar.tracer.modbus.TracerModbusSlaveRead) ModbusRuntimeException(me.retrodaredevil.io.modbus.ModbusRuntimeException) NotNull(me.retrodaredevil.solarthing.annotations.NotNull) HelpRequestedException(com.lexicalscope.jewel.cli.HelpRequestedException) Function(java.util.function.Function) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) Nullable(me.retrodaredevil.solarthing.annotations.Nullable) ModbusSlave(me.retrodaredevil.io.modbus.ModbusSlave) RoverModbusSlaveRead(me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveRead) Duration(java.time.Duration) ModbusTimeoutException(me.retrodaredevil.io.modbus.ModbusTimeoutException) JSerialIOBundle(me.retrodaredevil.io.serial.JSerialIOBundle) SerialPortException(me.retrodaredevil.io.serial.SerialPortException) MutableAddressModbusSlave(me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave) ArgumentValidationException(com.lexicalscope.jewel.cli.ArgumentValidationException) TimedPacketReceiver(me.retrodaredevil.solarthing.packets.handling.implementations.TimedPacketReceiver) SerialConfig(me.retrodaredevil.io.serial.SerialConfig) OutbackConstants(me.retrodaredevil.solarthing.solar.outback.OutbackConstants) BatteryVoltage(me.retrodaredevil.solarthing.solar.common.BatteryVoltage) Cli(com.lexicalscope.jewel.cli.Cli) ModbusSlaveBus(me.retrodaredevil.io.modbus.ModbusSlaveBus) IOException(java.io.IOException) EOFException(java.io.EOFException) IgnoreCheckSum(me.retrodaredevil.solarthing.util.IgnoreCheckSum) SolarThingConstants(me.retrodaredevil.solarthing.SolarThingConstants) CliFactory(com.lexicalscope.jewel.cli.CliFactory) SolarReader(me.retrodaredevil.solarthing.program.SolarReader) TracerReadTable(me.retrodaredevil.solarthing.solar.tracer.TracerReadTable) MatePacketCreator49(me.retrodaredevil.solarthing.solar.outback.MatePacketCreator49) UtilityClass(me.retrodaredevil.solarthing.annotations.UtilityClass) PacketListReceiver(me.retrodaredevil.solarthing.packets.handling.PacketListReceiver) RtuDataEncoder(me.retrodaredevil.io.modbus.RtuDataEncoder) MatePacketCreator49(me.retrodaredevil.solarthing.solar.outback.MatePacketCreator49) PacketListReceiver(me.retrodaredevil.solarthing.packets.handling.PacketListReceiver) EOFException(java.io.EOFException) SolarReader(me.retrodaredevil.solarthing.program.SolarReader) IOException(java.io.IOException) TimedPacketReceiver(me.retrodaredevil.solarthing.packets.handling.implementations.TimedPacketReceiver) JSerialIOBundle(me.retrodaredevil.io.serial.JSerialIOBundle)

Example 3 with JSerialIOBundle

use of me.retrodaredevil.io.serial.JSerialIOBundle 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)

Aggregations

JSerialIOBundle (me.retrodaredevil.io.serial.JSerialIOBundle)3 IOModbusSlaveBus (me.retrodaredevil.io.modbus.IOModbusSlaveBus)2 ModbusRuntimeException (me.retrodaredevil.io.modbus.ModbusRuntimeException)2 ModbusSlaveBus (me.retrodaredevil.io.modbus.ModbusSlaveBus)2 ModbusTimeoutException (me.retrodaredevil.io.modbus.ModbusTimeoutException)2 RtuDataEncoder (me.retrodaredevil.io.modbus.RtuDataEncoder)2 SerialConfig (me.retrodaredevil.io.serial.SerialConfig)2 MutableAddressModbusSlave (me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave)2 BatteryVoltage (me.retrodaredevil.solarthing.solar.common.BatteryVoltage)2 SerialPort (com.fazecast.jSerialComm.SerialPort)1 ArgumentValidationException (com.lexicalscope.jewel.cli.ArgumentValidationException)1 Cli (com.lexicalscope.jewel.cli.Cli)1 CliFactory (com.lexicalscope.jewel.cli.CliFactory)1 HelpRequestedException (com.lexicalscope.jewel.cli.HelpRequestedException)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Function (java.util.function.Function)1 ModbusSlave (me.retrodaredevil.io.modbus.ModbusSlave)1 SerialPortException (me.retrodaredevil.io.serial.SerialPortException)1