iso-can better code reuse/refactoring
This commit is contained in:
parent
0c4e27e557
commit
6d70b0d3ba
|
@ -151,8 +151,9 @@ public class Elm327Connector implements Closeable {
|
|||
|
||||
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);
|
||||
public void sendCanData(byte[] hdr, byte[] data, int dataOffset, int dataLength) {
|
||||
byte[] total = combineArrays(hdr, data, dataOffset, dataLength);
|
||||
Elm327Connector.this.sendCanData(total);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -216,14 +217,13 @@ public class Elm327Connector implements Closeable {
|
|||
return null;
|
||||
}
|
||||
|
||||
private void sendCanData(byte [] hdr, byte [] data, int offset, int len) {
|
||||
private void sendCanData(byte [] data) {
|
||||
if (log.debugEnabled()) {
|
||||
log.debug("sendCanData header " + IoStream.printByteArray(hdr));
|
||||
log.debug("sendCanData payload " + IoStream.printByteArray(data) + " len=" + len + " from offset=" + offset);
|
||||
log.debug("sendCanData total " + IoStream.printByteArray(data));
|
||||
}
|
||||
//log.info("--------sendData offset="+Integer.toString(offset) + " len=" + Integer.toString(len) + "hdr.len=" + Integer.toString(hdr.length));
|
||||
|
||||
byte[] hexData = byteToString(hdr, data, offset, len);
|
||||
byte[] hexData = byteToString(data);
|
||||
|
||||
//log.info("* Elm327.data: " + (new String(hexData)));
|
||||
|
||||
|
@ -235,16 +235,16 @@ public class Elm327Connector implements Closeable {
|
|||
}
|
||||
|
||||
@NotNull
|
||||
public static byte[] byteToString(byte[] hdr, byte[] data, int offset, int payloadLength) {
|
||||
int totalLength = hdr.length + payloadLength;
|
||||
byte[] hexData = new byte[totalLength * 2 + 1];
|
||||
for (int i = 0; i < totalLength; i++) {
|
||||
public static byte[] byteToString(byte[] data) {
|
||||
int length = data.length;
|
||||
byte[] hexData = new byte[length * 2 + 1];
|
||||
for (int i = 0; i < length; i++) {
|
||||
int j = i * 2;
|
||||
int v = ((i < hdr.length) ? hdr[i] : data[i - hdr.length + offset]) & 0xFF;
|
||||
int v = data[i] & 0xFF;
|
||||
hexData[j] = HEX_ARRAY[v >>> 4];
|
||||
hexData[j + 1] = HEX_ARRAY[v & 0x0F];
|
||||
}
|
||||
hexData[totalLength * 2] = '\r';
|
||||
hexData[length * 2] = '\r';
|
||||
return hexData;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.rusefi.io.can;
|
|||
|
||||
import com.devexperts.logging.Logging;
|
||||
import com.rusefi.io.IoStream;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @see IsoTpCanDecoder
|
||||
|
@ -39,15 +40,23 @@ public abstract class IsoTpConnector {
|
|||
}
|
||||
}
|
||||
|
||||
public void sendCanFrame(int hdr0, byte[] data, int offset, int len) {
|
||||
sendCanData(new byte[]{(byte) hdr0}, data, offset, len);
|
||||
@NotNull
|
||||
public static byte[] combineArrays(byte[] hdr, byte[] data, int dataOffset, int dataLength) {
|
||||
byte[] total = new byte[hdr.length + dataLength];
|
||||
System.arraycopy(hdr, 0, total, 0, hdr.length);
|
||||
System.arraycopy(data, dataOffset, total, hdr.length, dataLength);
|
||||
return total;
|
||||
}
|
||||
|
||||
public void sendCanFrame(int hdr0, int hdr1, byte[] data, int offset, int len) {
|
||||
sendCanData(new byte[]{(byte) hdr0, (byte) hdr1}, data, offset, len);
|
||||
public void sendCanFrame(int hdr0, byte[] data, int offset, int dataLength) {
|
||||
sendCanData(new byte[]{(byte) hdr0}, data, offset, dataLength);
|
||||
}
|
||||
|
||||
public abstract void sendCanData(byte[] hdr, byte[] data, int offset, int len);
|
||||
public void sendCanFrame(int hdr0, int hdr1, byte[] data, int dataOffset, int dataLength) {
|
||||
sendCanData(new byte[]{(byte) hdr0, (byte) hdr1}, data, dataOffset, dataLength);
|
||||
}
|
||||
|
||||
public abstract void sendCanData(byte[] hdr, byte[] data, int dataOffset, int dataLength);
|
||||
|
||||
public abstract void receiveData();
|
||||
}
|
||||
|
|
|
@ -35,10 +35,8 @@ public class PCanIoStream extends AbstractIoStream {
|
|||
|
||||
private final IsoTpConnector isoTpConnector = new IsoTpConnector() {
|
||||
@Override
|
||||
public void sendCanData(byte[] hdr, byte[] data, int offset, int len) {
|
||||
byte[] total = new byte[hdr.length + len];
|
||||
System.arraycopy(hdr, 0, total, 0, hdr.length);
|
||||
System.arraycopy(data, offset, total, hdr.length, len);
|
||||
public void sendCanData(byte[] hdr, byte[] data, int dataOffset, int dataLength) {
|
||||
byte[] total = combineArrays(hdr, data, dataOffset, dataLength);
|
||||
|
||||
log.info("-------sendIsoTp " + total.length + " byte(s):");
|
||||
|
||||
|
|
|
@ -9,12 +9,12 @@ public class Elm327ConnectorTest {
|
|||
@Test
|
||||
public void testBytesToString() {
|
||||
assertEquals("30 46 0D ",
|
||||
IoStream.printHexBinary(Elm327Connector.byteToString(new byte[]{0xF}, new byte[]{}, 0, 0))
|
||||
IoStream.printHexBinary(Elm327Connector.byteToString(new byte[]{0xF}))
|
||||
);
|
||||
|
||||
|
||||
assertEquals("31 30 30 42 30 30 30 35 36 42 30 30 30 30 35 30 0D ",
|
||||
IoStream.printHexBinary(Elm327Connector.byteToString(new byte[]{16, 11}, new byte[]{0, 5, 107, 0, 0, 80, 95, 105, -81, -96, 112}, 0, 6))
|
||||
IoStream.printHexBinary(Elm327Connector.byteToString(new byte[]{16, 11, 0, 5, 107, 0, 0, 80}))
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
@ -20,9 +20,10 @@ public class IsoTpConnectorTest {
|
|||
|
||||
IsoTpConnector testConnector = new IsoTpConnector() {
|
||||
@Override
|
||||
public void sendCanData(byte[] hdr, byte[] data, int offset, int payloadLength) {
|
||||
String packetAsString =
|
||||
IoStream.printHexBinary(Elm327Connector.byteToString(hdr, data, offset, payloadLength));
|
||||
public void sendCanData(byte[] hdr, byte[] data, int dataOffset, int dataLength) {
|
||||
byte[] total = combineArrays(hdr, data, dataOffset, dataLength);
|
||||
|
||||
String packetAsString = IoStream.printHexBinary(Elm327Connector.byteToString(total));
|
||||
packets.add(packetAsString);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue