From 00293e1973cc4b1725cdf264016e5fb87aa2b14c Mon Sep 17 00:00:00 2001 From: kascade Date: Sun, 21 Sep 2008 09:52:31 +0000 Subject: [PATCH] add j2534 support git-svn-id: https://svn2.assembla.com/svn/romraider/trunk@152 38686702-15cf-42e4-a595-3071df8bf5ea --- src/com/romraider/io/j2534/api/J2534.java | 4 +++- .../io/j2534/api/J2534ConnectionManager.java | 11 ++++++++--- .../romraider/io/j2534/op20/J2534OpenPort20.java | 16 ++++++++++++++-- .../comms/io/connection/SSMLoggerConnection.java | 5 +++-- .../logger/ecu/comms/reset/ResetManagerImpl.java | 7 ++++--- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/com/romraider/io/j2534/api/J2534.java b/src/com/romraider/io/j2534/api/J2534.java index d9e74db2..86da004f 100644 --- a/src/com/romraider/io/j2534/api/J2534.java +++ b/src/com/romraider/io/j2534/api/J2534.java @@ -17,7 +17,9 @@ public interface J2534 { void writeMsg(int channelId, byte[] data, long timeout); - byte[] readMsg(int channelId, long timeout); + byte[] readMsg(int channelId, long maxWait); + + void readMsg(int channelId, byte[] response, long timeout); void stopMsgFilter(int channelId, int msgId); diff --git a/src/com/romraider/io/j2534/api/J2534ConnectionManager.java b/src/com/romraider/io/j2534/api/J2534ConnectionManager.java index d42ed2dc..c0bbe968 100644 --- a/src/com/romraider/io/j2534/api/J2534ConnectionManager.java +++ b/src/com/romraider/io/j2534/api/J2534ConnectionManager.java @@ -59,13 +59,18 @@ public final class J2534ConnectionManager implements ConnectionManager { public void send(byte[] request, byte[] response, long timeout) { checkNotNull(request, "request"); checkNotNull(request, "response"); - // FIX - Complete! +// api.writeMsg(channelId, request, connectionProperties.getSendTimeout()); + api.writeMsg(channelId, request, timeout); + // FIX - should timeout be connectionProperties.getReadTimeout() ?? + api.readMsg(channelId, response, timeout); } // Send request and wait specified time for response with unknown length public byte[] send(byte[] bytes, long maxWait) { checkNotNull(bytes, "bytes"); - api.writeMsg(channelId, bytes, connectionProperties.getSendTimeout()); +// api.writeMsg(channelId, bytes, connectionProperties.getSendTimeout()); + api.writeMsg(channelId, bytes, maxWait); + // FIX - should maxWait be connectionProperties.getReadTimeout() ?? return api.readMsg(channelId, maxWait); } @@ -77,7 +82,7 @@ public final class J2534ConnectionManager implements ConnectionManager { private void version(int deviceId) { Version version = api.readVersion(deviceId); - System.out.println("Version => firmware: " + version.firmware + ", dll: " + version.dll + ", api: " + version.api); + LOGGER.info("J2534 Version => firmware: " + version.firmware + ", dll: " + version.dll + ", api: " + version.api); } private void setConfig(int channelId) { diff --git a/src/com/romraider/io/j2534/op20/J2534OpenPort20.java b/src/com/romraider/io/j2534/op20/J2534OpenPort20.java index a5c222eb..ff7130ba 100644 --- a/src/com/romraider/io/j2534/op20/J2534OpenPort20.java +++ b/src/com/romraider/io/j2534/op20/J2534OpenPort20.java @@ -98,9 +98,21 @@ public final class J2534OpenPort20 implements J2534 { if (status != STATUS_NOERROR) handleError(status); } - public byte[] readMsg(int channelId, long timeout) { - List responses = new ArrayList(); + public void readMsg(int channelId, byte[] response, long timeout) { + int index = 0; long end = currentTimeMillis() + timeout; + do { + PassThruMessage msg = doReadMsg(channelId); + LOGGER.trace("Read Msg: " + toString(msg)); + if (!isResponse(msg)) continue; + arraycopy(msg.Data, 0, response, index, msg.DataSize); + index += msg.DataSize; + } while (currentTimeMillis() <= end && index < response.length - 1); + } + + public byte[] readMsg(int channelId, long maxWait) { + List responses = new ArrayList(); + long end = currentTimeMillis() + maxWait; do { PassThruMessage msg = doReadMsg(channelId); LOGGER.trace("Read Msg: " + toString(msg)); diff --git a/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java b/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java index a57f29a4..10b00406 100644 --- a/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java +++ b/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java @@ -23,7 +23,7 @@ package com.romraider.logger.ecu.comms.io.connection; import com.romraider.io.connection.ConnectionManager; import com.romraider.io.connection.ConnectionProperties; -import com.romraider.io.serial.connection.SerialConnectionManager; +import com.romraider.io.j2534.api.J2534ConnectionManager; import com.romraider.logger.ecu.comms.io.protocol.LoggerProtocol; import com.romraider.logger.ecu.comms.io.protocol.SSMLoggerProtocol; import com.romraider.logger.ecu.comms.query.EcuQuery; @@ -39,7 +39,8 @@ public final class SSMLoggerConnection implements LoggerConnection { public SSMLoggerConnection(String portName, ConnectionProperties connectionProperties) { checkNotNullOrEmpty(portName, "portName"); checkNotNull(connectionProperties); - this.manager = new SerialConnectionManager(portName, connectionProperties); +// this.manager = new SerialConnectionManager(portName, connectionProperties); + this.manager = new J2534ConnectionManager(connectionProperties); } public void sendAddressReads(Collection queries) { diff --git a/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java b/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java index 0604eefb..f0ba3429 100644 --- a/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java +++ b/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java @@ -24,9 +24,9 @@ package com.romraider.logger.ecu.comms.reset; import com.romraider.Settings; import com.romraider.io.connection.ConnectionManager; import com.romraider.io.connection.ConnectionProperties; +import com.romraider.io.j2534.api.J2534ConnectionManager; import com.romraider.io.protocol.Protocol; import static com.romraider.io.protocol.ProtocolFactory.getProtocol; -import com.romraider.io.serial.connection.SerialConnectionManager; import com.romraider.logger.ecu.ui.MessageListener; import static com.romraider.util.HexUtil.asHex; import static com.romraider.util.ParamChecker.checkNotNull; @@ -47,12 +47,13 @@ public final class ResetManagerImpl implements ResetManager { try { Protocol protocol = getProtocol(settings.getLoggerProtocol()); ConnectionProperties connectionProperties = settings.getLoggerConnectionProperties(); - ConnectionManager connectionManager = new SerialConnectionManager(settings.getLoggerPort(), connectionProperties); +// ConnectionManager connectionManager = new SerialConnectionManager(settings.getLoggerPort(), connectionProperties); + ConnectionManager connectionManager = new J2534ConnectionManager(connectionProperties); try { messageListener.reportMessage("Sending ECU Reset..."); byte[] request = protocol.constructEcuResetRequest(); LOGGER.debug("Ecu Reset Request ---> " + asHex(request)); - byte[] response = connectionManager.send(request, connectionProperties.getSendTimeout()); + byte[] response = connectionManager.send(request, 500L); byte[] processedResponse = protocol.preprocessResponse(request, response); protocol.checkValidEcuResetResponse(processedResponse); LOGGER.debug("Ecu Reset Response <--- " + asHex(processedResponse));