// TODO: This a guess!!...untested!! public byte[] read() { try { serialConnection.readStaleData(); 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)); return response; } else { buffer.add(b); } } } sleep(1); } LOGGER.warn("AEM UEGO Response [read timeout]"); return new byte[0]; } catch (Exception e) { close(); throw new SerialCommunicationException(e); } }
/** * Retrieve a message through the existing communication channel from the vehicle. * * @param channelId - handle to the open communications channel * @param maxWait - maximum time (in milliseconds) for read completion * @return bytes read from the vehicle network */ public byte[] readMsg(int channelId, long maxWait) { List<byte[]> responses = new ArrayList<byte[]>(); long end = currentTimeMillis() + maxWait; do { PASSTHRU_MSG msg = doReadMsg(channelId); LOGGER.trace("Read Msg: " + toString(msg)); if (isResponse(msg)) responses.add(data(msg)); ThreadUtil.sleep(2); } while (currentTimeMillis() <= end); return concat(responses); }
/** * Retrieve the indicated number of messages through the existing communication channel from the * vehicle. If the number of messages can not be read before the timeout expires, throw an * exception. * * @param channelId - handle to the open communications channel * @param numMsg - number of valid messages to retrieve * @return bytes read from the vehicle network * @throws J2534Exception */ public byte[] readMsg(int channelId, int numMsg, long timeout) { if (loopback) { numMsg++; } List<byte[]> responses = new ArrayList<byte[]>(); long end = currentTimeMillis() + timeout; do { if (currentTimeMillis() >= end) { String errString = String.format("readMsg error: timeout expired waiting for %d more message(s)", numMsg); throw new J2534Exception(errString); } PASSTHRU_MSG msg = doReadMsg(channelId); LOGGER.trace("Read Msg: " + toString(msg)); if (isResponse(msg)) { responses.add(data(msg)); numMsg--; } ThreadUtil.sleep(2); } while (numMsg != 0); return concat(responses); }