diff --git a/java_console/io/src/main/java/com/rusefi/io/LinkManager.java b/java_console/io/src/main/java/com/rusefi/io/LinkManager.java index 89a67589d9..86f17668e9 100644 --- a/java_console/io/src/main/java/com/rusefi/io/LinkManager.java +++ b/java_console/io/src/main/java/com/rusefi/io/LinkManager.java @@ -253,7 +253,7 @@ public class LinkManager implements Closeable { @Override public IoStream call() { messageListener.postMessage(getClass(), "Opening port: " + port); - IoStream stream = ((Callable) () -> BufferedSerialIoStream.openPort(port)).call(); + IoStream stream = BufferedSerialIoStream.openPort(port); if (stream == null) { // error already reported return null; @@ -293,7 +293,7 @@ public class LinkManager implements Closeable { close(); // Explicitly kill the connection (call connectors destructor??????) String[] ports = getCommPorts(); - boolean isPortAvailableAgain = Arrays.stream(ports).anyMatch(lastTriedPort::equals); + boolean isPortAvailableAgain = Arrays.asList(ports).contains(lastTriedPort); if (isPortAvailableAgain) { connect(lastTriedPort); } 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 6ebb7e1958..0f945d530c 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 @@ -55,10 +55,16 @@ public class SerialIoStream extends AbstractIoStream { } @Override - public void write(byte[] bytes) { + public void write(byte[] bytes) throws IOException { if (Bug3923.obscene) log.info("Writing " + bytes.length + " byte(s)"); - sp.writeBytes(bytes, bytes.length); + + int written = sp.writeBytes(bytes, bytes.length); + + // If we failed to write all the bytes, the ECU probably disconnected + if (written != bytes.length) { + throw new IOException("write failed: wrote " + written + " but expected " + bytes.length); + } } @Override @@ -87,7 +93,7 @@ public class SerialIoStream extends AbstractIoStream { @Override public void serialEvent(SerialPortEvent event) { if (Bug3923.obscene) - log.info("serialEvent " + event); + log.info("serialEvent " + event.getEventType()); if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) return; if (isFirstEvent) { @@ -100,10 +106,16 @@ public class SerialIoStream extends AbstractIoStream { log.info("serialEvent bytesAvailable " + bytesAvailable); if (bytesAvailable <= 0) return; // sometimes negative value is returned at least on Mac - byte[] newData = new byte[bytesAvailable]; - int numRead = sp.readBytes(newData, newData.length); - byte[] data = new byte[numRead]; - System.arraycopy(newData, 0, data, 0, numRead); + byte[] data = new byte[bytesAvailable]; + int numRead = sp.readBytes(data, data.length); + + // Copy in to a smaller array if the read was incomplete + if (numRead != bytesAvailable) { + byte[] dataSmaller = new byte[numRead]; + System.arraycopy(data, 0, dataSmaller, 0, numRead); + data = dataSmaller; + } + listener.onDataArrived(data); } }); diff --git a/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java b/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java index 901b239275..64b3c5fced 100644 --- a/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java +++ b/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java @@ -86,6 +86,7 @@ public class ConsoleUI { if (LinkManager.isLogViewerMode(port)) tabbedPane.addTab("Log Viewer", new LogViewer(uiContext, engineSnifferPanel)); + // TODO: this is a race if the ECU is slow to connect new ConnectionWatchdog(Timeouts.CONNECTION_RESTART_DELAY, () -> { uiContext.getLinkManager().execute(() -> { FileLog.MAIN.logLine("ConnectionWatchdog.reconnectTimer restarting: " + Timeouts.CONNECTION_RESTART_DELAY);