tune via CAN #3361

useful abstract class
This commit is contained in:
rusefillc 2021-12-05 22:22:39 -05:00
parent b0eb01a8fb
commit 333f87863c
2 changed files with 62 additions and 27 deletions

View File

@ -142,36 +142,21 @@ public class Elm327Connector implements Closeable {
}
};
public void sendBytesToSerial(byte [] bytes) {
log.info("-------sendBytesToSerial " + bytes.length + " byte(s):");
for (int i = 0; i < bytes.length; i++) {
log.info("[index=" + i + "] " + ((int) bytes[i] & 0xff));
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);
}
// 1 frame
if (bytes.length <= 7) {
sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_SINGLE << 4) | bytes.length, bytes, 0, bytes.length);
return;
}
@Override
public void receiveData() {
Elm327Connector.this.receiveData();
}
};
// multiple frames
// send the first header frame
sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_FIRST << 4) | ((bytes.length >> 8) & 0x0f), bytes.length & 0xff, bytes, 0, 6);
// get a flow control frame
receiveData();
// send the rest of the data
int idx = 1, offset = 6;
int remaining = bytes.length - 6;
while (remaining > 0) {
int len = Math.min(remaining, 7);
// send the consecutive frames
sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_CONSECUTIVE << 4) | ((idx++) & 0x0f), bytes, offset, len);
offset += len;
remaining -= len;
}
}
public void sendBytesToSerial(byte [] bytes) {
IsoTpConnector.sendStrategy(bytes, connector);
}
///////////////////////////////////////////////////////

View File

@ -0,0 +1,50 @@
package com.rusefi.io.can;
import com.devexperts.logging.Logging;
public abstract class IsoTpConnector {
private final static Logging log = Logging.getLogging(Elm327Connector.class);
public static void sendStrategy(byte[] bytes, IsoTpConnector connector) {
log.info("-------sendBytesToSerial " + bytes.length + " byte(s):");
for (int i = 0; i < bytes.length; i++) {
log.info("[index=" + i + "] " + ((int) bytes[i] & 0xff));
}
// 1 frame
if (bytes.length <= 7) {
connector.sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_SINGLE << 4) | bytes.length, bytes, 0, bytes.length);
return;
}
// multiple frames
// send the first header frame
connector.sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_FIRST << 4) | ((bytes.length >> 8) & 0x0f), bytes.length & 0xff, bytes, 0, 6);
// get a flow control frame
connector.receiveData();
// send the rest of the data
int idx = 1, offset = 6;
int remaining = bytes.length - 6;
while (remaining > 0) {
int len = Math.min(remaining, 7);
// send the consecutive frames
connector.sendCanFrame((IsoTpCanDecoder.ISO_TP_FRAME_CONSECUTIVE << 4) | ((idx++) & 0x0f), bytes, offset, len);
offset += len;
remaining -= len;
}
}
public void sendCanFrame(int hdr0, byte[] data, int offset, int len) {
sendCanData(new byte[]{(byte) hdr0}, data, offset, len);
}
public void sendCanFrame(int hdr0, int hdr1, byte[] data, int offset, int len) {
sendCanData(new byte[]{(byte) hdr0, (byte) hdr1}, data, offset, len);
}
public abstract void sendCanData(byte[] hdr, byte[] data, int offset, int len);
public abstract void receiveData();
}