mirror of https://github.com/rusefi/RomRaider.git
plugin connection update
git-svn-id: https://svn2.assembla.com/svn/romraider/trunk@33 38686702-15cf-42e4-a595-3071df8bf5ea
This commit is contained in:
parent
5e91dd19e2
commit
af89c1087b
|
@ -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<Byte> buffer = new ArrayList<Byte>();
|
||||
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<Byte> buffer = new ArrayList<Byte>();
|
||||
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");
|
||||
|
|
|
@ -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]");
|
||||
|
|
Loading…
Reference in New Issue