diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java index e8b79bf278..f339cb0cf5 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; +import java.util.Date; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -126,7 +127,7 @@ public class BinaryProtocol { } catch (ExecutionException e) { throw new IllegalStateException(e); } catch (TimeoutException e) { - getLogger().error("timeout, giving up: " + e); + getLogger().error("timeout sending [" + command + "] giving up: " + e); return; } /** @@ -204,7 +205,7 @@ public class BinaryProtocol { synchronized (ioLock) { boolean isTimeout = incomingData.waitForBytes(2, start, "switch to binary"); if (isTimeout) { - logger.info("Timeout waiting for switch response"); + logger.info(new Date() + ": Timeout waiting for switch response"); close(); return; } diff --git a/java_console/io/src/com/rusefi/io/serial/SerialIoStream.java b/java_console/io/src/com/rusefi/io/serial/SerialIoStream.java index 6daf569567..c0c33aa31f 100644 --- a/java_console/io/src/com/rusefi/io/serial/SerialIoStream.java +++ b/java_console/io/src/com/rusefi/io/serial/SerialIoStream.java @@ -38,7 +38,9 @@ public class SerialIoStream implements IoStream { @Override public void write(byte[] bytes) throws IOException { try { - serialPort.writeBytes(bytes); + synchronized (serialPort) { + serialPort.writeBytes(bytes); + } } catch (SerialPortException e) { throw new IOException(e); } @@ -47,7 +49,9 @@ public class SerialIoStream implements IoStream { @Override public void purge() { try { - serialPort.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR); + synchronized (serialPort) { + serialPort.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR); + } } catch (SerialPortException e) { logger.info("Error while purge: " + e); close(); diff --git a/java_console/io/src/com/rusefi/io/serial/SerialPortReader.java b/java_console/io/src/com/rusefi/io/serial/SerialPortReader.java index d6f54d04d4..c9e39a9003 100644 --- a/java_console/io/src/com/rusefi/io/serial/SerialPortReader.java +++ b/java_console/io/src/com/rusefi/io/serial/SerialPortReader.java @@ -12,33 +12,57 @@ import jssc.SerialPortException; * (c) Andrey Belomutskiy */ public class SerialPortReader implements SerialPortEventListener { - private SerialPort serialPort; + private final SerialPort serialPort; private DataListener listener; - public SerialPortReader(SerialPort serialPort, DataListener listener) { + public SerialPortReader(final SerialPort serialPort, final DataListener listener) { this.serialPort = serialPort; this.listener = listener; + new Thread(new Runnable() { + @Override + public void run() { + try { + while (serialPort.isOpened()) { + byte[] data; + synchronized (serialPort) { + data = serialPort.readBytes(); + } + if (data != null) { + listener.onDataArrived(data); + } else { + Thread.sleep(100); + } + } + } catch (SerialPortException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + } + }, "Reader_" + serialPort).start(); } public void serialEvent(SerialPortEvent spe) { if (spe.isRXCHAR() || spe.isRXFLAG()) { - try { - handleRx(spe); - } catch (SerialPortException e) { - e.printStackTrace(System.err); - } +// event-based serial read implementation does not work well on Windows 10 for some reason +// https://sourceforge.net/p/rusefi/tickets/264/ +// try { +// handleRx(spe); +// } catch (SerialPortException e) { +// e.printStackTrace(System.err); +// } } else if (spe.getEventType() != SerialPortEvent.TXEMPTY) { FileLog.MAIN.logLine("less expected SerialPortReader serialEvent " + spe.getEventType()); } } - private void handleRx(SerialPortEvent spe) throws SerialPortException { - if (spe.getEventValue() > 0) { - byte[] buffer = serialPort.readBytes(spe.getEventValue()); - listener.onDataArrived(buffer); - // System.out.println("arrived [" + str + "]"); - } - } +// private void handleRx(SerialPortEvent spe) throws SerialPortException { +// if (spe.getEventValue() > 0) { +// byte[] buffer = serialPort.readBytes(spe.getEventValue()); +// listener.onDataArrived(buffer); +// // System.out.println("arrived [" + str + "]"); +// } +// } public void readInitial() throws SerialPortException { int input = serialPort.getInputBufferBytesCount(); diff --git a/java_console/jssc/src/jssc/SerialPort.java b/java_console/jssc/src/jssc/SerialPort.java index a5921509fa..da2cebffe4 100644 --- a/java_console/jssc/src/jssc/SerialPort.java +++ b/java_console/jssc/src/jssc/SerialPort.java @@ -27,6 +27,7 @@ package jssc; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.nio.charset.Charset; +import java.util.Arrays; import java.util.Date; /** @@ -137,6 +138,7 @@ public class SerialPort { * @return Method returns true if port is open, otherwise false */ public boolean isOpened() { + log("isOpened " + portOpened); return portOpened; } @@ -305,6 +307,7 @@ public class SerialPort { * @throws SerialPortException */ public int getEventsMask() throws SerialPortException { + log("getEventsMask"); checkPortOpened("getEventsMask()"); if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX || SerialNativeInterface.getOsType() == SerialNativeInterface.OS_SOLARIS || @@ -357,6 +360,7 @@ public class SerialPort { * @throws SerialPortException */ public boolean writeBytes(byte[] buffer) throws SerialPortException { + log("writeBytes " + Arrays.toString(buffer)); checkPortOpened("writeBytes()"); return serialInterface.writeBytes(portHandle, buffer); } @@ -371,6 +375,7 @@ public class SerialPort { * @since 0.8 */ public boolean writeByte(byte singleByte) throws SerialPortException { + log("writeByte " + singleByte); checkPortOpened("writeByte()"); return writeBytes(new byte[]{singleByte}); } @@ -385,6 +390,7 @@ public class SerialPort { * @since 0.8 */ public boolean writeString(String string) throws SerialPortException { + log("writeString"); checkPortOpened("writeString()"); return writeBytes(string.getBytes()); } @@ -399,6 +405,7 @@ public class SerialPort { * @since 2.8.0 */ public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException { + log("writeString"); checkPortOpened("writeString()"); return writeBytes(string.getBytes(charsetName)); } @@ -413,6 +420,7 @@ public class SerialPort { * @since 0.8 */ public boolean writeInt(int singleInt) throws SerialPortException { + log("writeInt " + singleInt); checkPortOpened("writeInt()"); return writeBytes(new byte[]{(byte)singleInt}); } @@ -427,6 +435,7 @@ public class SerialPort { * @since 0.8 */ public boolean writeIntArray(int[] buffer) throws SerialPortException { + log("writeIntArray"); checkPortOpened("writeIntArray()"); byte[] byteArray = new byte[buffer.length]; for(int i = 0; i < buffer.length; i++){ @@ -462,6 +471,7 @@ public class SerialPort { * @since 0.8 */ public String readString(int byteCount) throws SerialPortException { + log("readString " + byteCount); checkPortOpened("readString()"); return new String(readBytes(byteCount)); } @@ -478,6 +488,7 @@ public class SerialPort { * @since 0.8 */ public String readHexString(int byteCount) throws SerialPortException { + log("readHexString " + byteCount); checkPortOpened("readHexString()"); return readHexString(byteCount, " "); } @@ -494,6 +505,7 @@ public class SerialPort { * @since 0.8 */ public String readHexString(int byteCount, String separator) throws SerialPortException { + log("readHexString"); checkPortOpened("readHexString()"); String[] strBuffer = readHexStringArray(byteCount); String returnString = ""; @@ -709,6 +721,7 @@ public class SerialPort { * @since 0.8 */ public byte[] readBytes() throws SerialPortException { + log("readBytes all"); checkPortOpened("readBytes()"); int byteCount = getInputBufferBytesCount(); if(byteCount <= 0){ @@ -865,6 +878,7 @@ public class SerialPort { * @since 0.8 */ public int getFlowControlMode() throws SerialPortException { + log("getFlowControlMode"); checkPortOpened("getFlowControlMode()"); return serialInterface.getFlowControlMode(portHandle); } @@ -915,6 +929,7 @@ public class SerialPort { * @throws SerialPortException */ public int[] getLinesStatus() throws SerialPortException { + log("getLinesStatus"); checkPortOpened("getLinesStatus()"); return serialInterface.getLinesStatus(portHandle); } @@ -1086,6 +1101,7 @@ public class SerialPort { * @throws SerialPortException */ public boolean removeEventListener() throws SerialPortException { + log("removeEventListener"); checkPortOpened("removeEventListener()"); if(!eventListenerAdded){ throw new SerialPortException(portName, "removeEventListener()", SerialPortException.TYPE_CANT_REMOVE_LISTENER); @@ -1115,6 +1131,7 @@ public class SerialPort { * @throws SerialPortException */ public boolean closePort() throws SerialPortException { + log("closePort"); checkPortOpened("closePort()"); if(eventListenerAdded){ removeEventListener(); diff --git a/java_console/ui/src/com/rusefi/Launcher.java b/java_console/ui/src/com/rusefi/Launcher.java index 0ce0531256..090750acd2 100644 --- a/java_console/ui/src/com/rusefi/Launcher.java +++ b/java_console/ui/src/com/rusefi/Launcher.java @@ -34,7 +34,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; * @see EngineSnifferPanel */ public class Launcher { - public static final int CONSOLE_VERSION = 20160212; + public static final int CONSOLE_VERSION = 20160213; public static final boolean SHOW_STIMULATOR = false; private static final String TAB_INDEX = "main_tab"; protected static final String PORT_KEY = "port";