From 875dbc7189d6a27c20cb6dd50507eab4e63a167c Mon Sep 17 00:00:00 2001 From: rusefillc Date: Mon, 21 Feb 2022 16:42:12 -0500 Subject: [PATCH] Dron reports that MRE console does not work from linux #3937 --- .../com/rusefi/io/serial/SerialIoStream.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java b/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java index 9f43671453..d860c90919 100644 --- a/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java @@ -8,7 +8,7 @@ import com.opensr5.io.DataListener; import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.test.Bug3923; import com.rusefi.io.IoStream; -import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.IOException; @@ -17,6 +17,7 @@ import static com.devexperts.logging.Logging.getLogging; public class SerialIoStream extends AbstractIoStream { static Logging log = getLogging(SerialIoStream.class); + @Nullable // null in case of port open error, for instance lack of permissions on Unix protected final SerialPort sp; protected final String port; private boolean withListener; @@ -25,7 +26,7 @@ public class SerialIoStream extends AbstractIoStream { log.info("Using com.fazecast.jSerialComm " + SerialPort.getVersion()); } - public SerialIoStream(SerialPort sp, String port) { + public SerialIoStream(@Nullable SerialPort sp, String port) { this.sp = sp; this.port = port; } @@ -37,11 +38,16 @@ public class SerialIoStream extends AbstractIoStream { return new SerialIoStream(serialPort, port); } - @NotNull + @Nullable protected static SerialPort openSerial(String port) { SerialPort serialPort = SerialPort.getCommPort(port); serialPort.setBaudRate(BaudRateHolder.INSTANCE.baudRate); - serialPort.openPort(0); + boolean openedOk = serialPort.openPort(0); + if (!openedOk) { + log.error("Error opening " + port + " maybe no permissions?"); + // todo: leverage jSerialComm method once we start using version 2.9+ + return null; + } return serialPort; } @@ -54,7 +60,8 @@ public class SerialIoStream extends AbstractIoStream { public void close() { log.info(port + ": Closing port..."); super.close(); - sp.closePort(); + if (sp != null) + sp.closePort(); log.info(port + ": Closed port."); } @@ -62,6 +69,8 @@ public class SerialIoStream extends AbstractIoStream { public void write(byte[] bytes) throws IOException { if (Bug3923.obscene) log.info("Writing " + bytes.length + " byte(s)"); + if (sp == null) + throw new IOException("Port was never opened"); int written = sp.writeBytes(bytes, bytes.length); @@ -86,6 +95,8 @@ public class SerialIoStream extends AbstractIoStream { throw new IllegalStateException("Not possible to change listener"); } withListener = true; + if (sp == null) + return; sp.addDataListener(new SerialPortDataListener() { private boolean isFirstEvent = true;