From 81b9f9b44e699d77e58c23355a36640d3c05f260 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sun, 5 Dec 2021 22:22:39 -0500 Subject: [PATCH] tune via CAN #3361 useful abstract class --- .../com/rusefi/io/can/Elm327Connector.java | 39 +++++---------- .../com/rusefi/io/can/IsoTpConnector.java | 50 +++++++++++++++++++ 2 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 java_console/io/src/main/java/com/rusefi/io/can/IsoTpConnector.java diff --git a/java_console/io/src/main/java/com/rusefi/io/can/Elm327Connector.java b/java_console/io/src/main/java/com/rusefi/io/can/Elm327Connector.java index cadfe60a58..8967077ac1 100644 --- a/java_console/io/src/main/java/com/rusefi/io/can/Elm327Connector.java +++ b/java_console/io/src/main/java/com/rusefi/io/can/Elm327Connector.java @@ -142,36 +142,21 @@ public class Elm327Connector implements Closeable { } }; - public void sendBytesToSerial(byte [] bytes) { - log.info("-------sendBytesToSerial " + bytes.length + " byte(s):"); - - for (int i = 0; i < bytes.length; i++) { - log.info("[index=" + i + "] " + ((int) bytes[i] & 0xff)); + private final IsoTpConnector connector = new IsoTpConnector() { + @Override + public void sendCanData(byte[] hdr, byte[] data, int offset, int len) { + Elm327Connector.this.sendCanData(hdr, data, offset, len); } - // 1 frame - if (bytes.length <= 7) { - sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_SINGLE << 4) | bytes.length, bytes, 0, bytes.length); - return; - } + @Override + public void receiveData() { + Elm327Connector.this.receiveData(); + } + }; - // multiple frames - // send the first header frame - sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_FIRST << 4) | ((bytes.length >> 8) & 0x0f), bytes.length & 0xff, bytes, 0, 6); - // get a flow control frame - receiveData(); - - // send the rest of the data - int idx = 1, offset = 6; - int remaining = bytes.length - 6; - while (remaining > 0) { - int len = Math.min(remaining, 7); - // send the consecutive frames - sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_CONSECUTIVE << 4) | ((idx++) & 0x0f), bytes, offset, len); - offset += len; - remaining -= len; - } - } + public void sendBytesToSerial(byte [] bytes) { + IsoTpConnector.sendStrategy(bytes, connector); + } /////////////////////////////////////////////////////// diff --git a/java_console/io/src/main/java/com/rusefi/io/can/IsoTpConnector.java b/java_console/io/src/main/java/com/rusefi/io/can/IsoTpConnector.java new file mode 100644 index 0000000000..e07f023add --- /dev/null +++ b/java_console/io/src/main/java/com/rusefi/io/can/IsoTpConnector.java @@ -0,0 +1,50 @@ +package com.rusefi.io.can; + +import com.devexperts.logging.Logging; + +public abstract class IsoTpConnector { + private final static Logging log = Logging.getLogging(Elm327Connector.class); + + public static void sendStrategy(byte[] bytes, IsoTpConnector connector) { + log.info("-------sendBytesToSerial " + bytes.length + " byte(s):"); + + for (int i = 0; i < bytes.length; i++) { + log.info("[index=" + i + "] " + ((int) bytes[i] & 0xff)); + } + + // 1 frame + if (bytes.length <= 7) { + connector.sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_SINGLE << 4) | bytes.length, bytes, 0, bytes.length); + return; + } + + // multiple frames + // send the first header frame + connector.sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_FIRST << 4) | ((bytes.length >> 8) & 0x0f), bytes.length & 0xff, bytes, 0, 6); + // get a flow control frame + connector.receiveData(); + + // send the rest of the data + int idx = 1, offset = 6; + int remaining = bytes.length - 6; + while (remaining > 0) { + int len = Math.min(remaining, 7); + // send the consecutive frames + connector.sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_CONSECUTIVE << 4) | ((idx++) & 0x0f), bytes, offset, len); + offset += len; + remaining -= len; + } + } + + public void sendCanFrame(int hdr0, byte[] data, int offset, int len) { + sendCanData(new byte[]{(byte) hdr0}, data, offset, len); + } + + public void sendCanFrame(int hdr0, int hdr1, byte[] data, int offset, int len) { + sendCanData(new byte[]{(byte) hdr0, (byte) hdr1}, data, offset, len); + } + + public abstract void sendCanData(byte[] hdr, byte[] data, int offset, int len); + + public abstract void receiveData(); +}