use of me.retrodaredevil.solarthing.program.SolarReader 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];
}
}
Aggregations