From 2298924a4153960e581bc14d503f93a940fbab7b Mon Sep 17 00:00:00 2001 From: rusEfi Date: Fri, 6 Mar 2015 16:11:10 -0600 Subject: [PATCH] auto-sync --- .../io/src/com/rusefi/binaryprotocol/CRC.java | 15 ++++++++++++ .../rusefi/binaryprotocol/test/CRCTest.java | 24 +++++++++++++++++++ .../io/src/com/rusefi/io/DataListener.java | 2 +- .../src/com/rusefi/io/serial/PortHolder.java | 12 ++++++---- .../com/rusefi/io/serial/SerialManager.java | 12 +++++----- .../rusefi/io/serial/SerialPortReader.java | 3 +-- 6 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 java_console/io/src/com/rusefi/binaryprotocol/CRC.java create mode 100644 java_console/io/src/com/rusefi/binaryprotocol/test/CRCTest.java diff --git a/java_console/io/src/com/rusefi/binaryprotocol/CRC.java b/java_console/io/src/com/rusefi/binaryprotocol/CRC.java new file mode 100644 index 0000000000..980492ffb0 --- /dev/null +++ b/java_console/io/src/com/rusefi/binaryprotocol/CRC.java @@ -0,0 +1,15 @@ +package com.rusefi.binaryprotocol; + +import java.util.zip.CRC32; + +/** + * (c) Andrey Belomutskiy + * 3/6/2015 + */ +public class CRC { + public static int crc32(byte buf[], int size) { + CRC32 c = new CRC32(); + c.update(buf, 0, size); + return (int) c.getValue(); + } +} diff --git a/java_console/io/src/com/rusefi/binaryprotocol/test/CRCTest.java b/java_console/io/src/com/rusefi/binaryprotocol/test/CRCTest.java new file mode 100644 index 0000000000..3e3cf6606d --- /dev/null +++ b/java_console/io/src/com/rusefi/binaryprotocol/test/CRCTest.java @@ -0,0 +1,24 @@ +package com.rusefi.binaryprotocol.test; + +import com.rusefi.binaryprotocol.CRC; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * (c) Andrey Belomutskiy + * 3/6/2015 + */ +public class CRCTest { + @Test + public void testCrc() { + { + byte[] a = {'A'}; + assertEquals(0xD3D99E8B, CRC.crc32(a, a.length)); + } + { + byte[] a = {'S'}; + assertEquals(0x2060EFC3, CRC.crc32(a, a.length)); + } + } +} diff --git a/java_console/io/src/com/rusefi/io/DataListener.java b/java_console/io/src/com/rusefi/io/DataListener.java index 524831c7e4..0254ba5529 100644 --- a/java_console/io/src/com/rusefi/io/DataListener.java +++ b/java_console/io/src/com/rusefi/io/DataListener.java @@ -5,5 +5,5 @@ package com.rusefi.io; * 3/3/14 */ public interface DataListener { - void onStringArrived(String string); + void onDataArrived(byte freshData[]); } diff --git a/java_console/io/src/com/rusefi/io/serial/PortHolder.java b/java_console/io/src/com/rusefi/io/serial/PortHolder.java index 6e11df6e76..2670228756 100644 --- a/java_console/io/src/com/rusefi/io/serial/PortHolder.java +++ b/java_console/io/src/com/rusefi/io/serial/PortHolder.java @@ -49,10 +49,7 @@ public class PortHolder { boolean opened = serialPort.openPort();//Open serial port if (!opened) FileLog.MAIN.logLine("not opened!"); - serialPort.setParams(BAUD_RATE, 8, 1, 0);//Set params. - int mask = SerialPort.MASK_RXCHAR; - //Set the prepared mask - serialPort.setEventsMask(mask); + setupPort(serialPort); serialPort.addEventListener(new SerialPortReader(serialPort, listener)); } catch (SerialPortException e) { FileLog.rlog("ERROR " + e.getMessage()); @@ -84,6 +81,13 @@ public class PortHolder { return true; } + public static void setupPort(SerialPort serialPort) throws SerialPortException { + serialPort.setParams(BAUD_RATE, 8, 1, 0);//Set params. + int mask = SerialPort.MASK_RXCHAR; + //Set the prepared mask + serialPort.setEventsMask(mask); + } + public void close() { synchronized (portLock) { if (serialPort != null) { diff --git a/java_console/io/src/com/rusefi/io/serial/SerialManager.java b/java_console/io/src/com/rusefi/io/serial/SerialManager.java index 2554918d05..13aab42462 100644 --- a/java_console/io/src/com/rusefi/io/serial/SerialManager.java +++ b/java_console/io/src/com/rusefi/io/serial/SerialManager.java @@ -12,12 +12,12 @@ import com.rusefi.io.LinkManager; class SerialManager { public static String port; - private static boolean closed; +// private static boolean closed; static DataListener dataListener = new DataListener() { - public void onStringArrived(String string) { + public void onDataArrived(byte freshData[]) { // jTextAreaIn.append(string); - LinkManager.engineState.processNewData(string); + LinkManager.engineState.processNewData(new String(freshData)); } }; private static LinkManager.LinkStateListener listener; @@ -39,10 +39,10 @@ class SerialManager { @Override public void run() { MessagesCentral.getInstance().postMessage(SerialManager.class, "Restarting serial IO"); - if (closed) - return; +// if (closed) +// return; PortHolder.getInstance().close(); - boolean result = PortHolder.getInstance().openPort(port, dataListener, SerialManager.listener); + PortHolder.getInstance().openPort(port, dataListener, SerialManager.listener); } }); } 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 cb8e4faf1d..b0c7067a42 100644 --- a/java_console/io/src/com/rusefi/io/serial/SerialPortReader.java +++ b/java_console/io/src/com/rusefi/io/serial/SerialPortReader.java @@ -35,8 +35,7 @@ class SerialPortReader implements SerialPortEventListener { private void handleRx(SerialPortEvent spe) throws SerialPortException { if (spe.getEventValue() > 0) { byte[] buffer = serialPort.readBytes(spe.getEventValue()); - String str = new String(buffer); - listener.onStringArrived(str); + listener.onDataArrived(buffer); // System.out.println("arrived [" + str + "]"); } }