From af89c1087b7c3699b552176b505ad7a8c4549f01 Mon Sep 17 00:00:00 2001 From: kascade Date: Sat, 12 Apr 2008 12:24:26 +0000 Subject: [PATCH] plugin connection update git-svn-id: https://svn2.assembla.com/svn/romraider/trunk@33 38686702-15cf-42e4-a595-3071df8bf5ea --- .../logger/aem/io/AemConnectionImpl.java | 30 +++++++++++++------ .../generic/io/InnovateConnectionImpl.java | 4 +++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/enginuity/logger/aem/io/AemConnectionImpl.java b/src/enginuity/logger/aem/io/AemConnectionImpl.java index 5558c69f..02e586a9 100644 --- a/src/enginuity/logger/aem/io/AemConnectionImpl.java +++ b/src/enginuity/logger/aem/io/AemConnectionImpl.java @@ -9,6 +9,7 @@ import static enginuity.util.ParamChecker.checkNotNull; import static enginuity.util.ParamChecker.checkNotNullOrEmpty; import static enginuity.util.ThreadUtil.sleep; import org.apache.log4j.Logger; +import static java.lang.System.currentTimeMillis; import java.util.ArrayList; import java.util.List; @@ -29,12 +30,17 @@ public final class AemConnectionImpl implements AemConnection { public byte[] read() { try { serialConnection.readStaleData(); - List buffer = new ArrayList(); - long start = System.currentTimeMillis(); - while (System.currentTimeMillis() - start <= sendTimeout) { - byte[] bytes = serialConnection.readAvailable(); - if (bytes.length > 0) { - for (byte b : bytes) { + long start = currentTimeMillis(); + while (currentTimeMillis() - start <= sendTimeout) { + if (serialConnection.available() > 10) { + byte[] bytes = serialConnection.readAvailable(); + LOGGER.trace("AEM UEGO input: " + asHex(bytes)); + int startIndex = findStart(bytes); + LOGGER.trace("AEM UEGO start index: " + startIndex); + if (startIndex < 0 || startIndex >= bytes.length) continue; + List buffer = new ArrayList(); + for (int i = startIndex; i < bytes.length; i++) { + byte b = bytes[i]; if (b == (byte) 0x0D) { byte[] response = toArray(buffer); LOGGER.trace("AEM UEGO Response: " + asHex(response)); @@ -46,15 +52,21 @@ public final class AemConnectionImpl implements AemConnection { } sleep(1); } - byte[] badBytes = toArray(buffer); - LOGGER.warn("AEM UEGO Response [read timeout]: " + asHex(badBytes)); - return badBytes; + LOGGER.warn("AEM UEGO Response [read timeout]"); + return new byte[0]; } catch (Exception e) { close(); throw new SerialCommunicationException(e); } } + private int findStart(byte[] bytes) { + for (int i = 0; i < bytes.length; i++) { + if (bytes[i] == (byte) 0x0D) return i + 1; + } + return -1; + } + public void close() { serialConnection.close(); LOGGER.info("AEM disconnected"); diff --git a/src/enginuity/logger/innovate/generic/io/InnovateConnectionImpl.java b/src/enginuity/logger/innovate/generic/io/InnovateConnectionImpl.java index 70b2ca44..412f6425 100644 --- a/src/enginuity/logger/innovate/generic/io/InnovateConnectionImpl.java +++ b/src/enginuity/logger/innovate/generic/io/InnovateConnectionImpl.java @@ -7,6 +7,7 @@ import enginuity.logger.ecu.exception.SerialCommunicationException; import static enginuity.util.ByteUtil.matchOnes; import static enginuity.util.ByteUtil.matchZeroes; import static enginuity.util.HexUtil.asBytes; +import static enginuity.util.HexUtil.asHex; import static enginuity.util.ParamChecker.checkGreaterThanZero; import static enginuity.util.ParamChecker.checkNotNull; import static enginuity.util.ParamChecker.checkNotNullOrEmpty; @@ -50,11 +51,13 @@ public final class InnovateConnectionImpl implements InnovateConnection { if (available < bufferLength) continue; byte[] buffer = new byte[bufferLength]; serialConnection.read(buffer); + LOGGER.trace(device + " input: " + asHex(buffer)); int responseBeginIndex = 0; int bufferBeginIndex = findHeader(buffer); if (bufferBeginIndex < 0) { bufferBeginIndex = findLm1(buffer); if (bufferBeginIndex < 0) continue; + LOGGER.trace(device + ": v1 protocol found - appending header..."); arraycopy(INNOVATE_HEADER, 0, response, 0, INNOVATE_HEADER.length); responseBeginIndex = INNOVATE_HEADER.length; } @@ -66,6 +69,7 @@ public final class InnovateConnectionImpl implements InnovateConnection { if (remainder.length == 0) continue; arraycopy(remainder, 0, response, responseLength - remainderLength, remainderLength); } + LOGGER.trace(device + " Response: " + asHex(response)); return response; } LOGGER.warn(device + " Response [read timeout]");