This commit is contained in:
rusefillc 2022-06-20 21:29:27 -04:00
parent 11673bf87c
commit 58baac6d3f
5 changed files with 70 additions and 7 deletions

View File

@ -3,7 +3,11 @@ package com.rusefi.can.reader;
import com.rusefi.can.CANPacket;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public interface CANLineReader {
static byte[] readHexArray(String[] tokens, int start, int size) {
@ -15,5 +19,15 @@ public interface CANLineReader {
CANPacket readLine(String line);
List<CANPacket> readFile(String fileName) throws IOException;
default List<CANPacket> readFile(String fileName) throws IOException {
List<CANPacket> result = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(s -> {
CANPacket packet = readLine(s);
if (packet != null)
result.add(packet);
});
return result;
}
}
}

View File

@ -38,4 +38,15 @@ public class DbcField {
", mult=" + mult +
'}';
}
public static int getBitIndex(byte[] data, int bitIndex, int bitWidth) {
int byteIndex = bitIndex >> 3;
int shift = bitIndex - byteIndex * 8;
int value = data[byteIndex];
if (shift + bitWidth > 8) {
value = value + data[1 + byteIndex] * 256;
}
int mask = (1 << bitWidth) - 1;
return (value >> shift) & mask;
}
}

View File

@ -1,7 +1,5 @@
package com.rusefi.can.reader.dbc;
import com.rusefi.can.reader.dbc.DbcField;
import java.util.ArrayList;
import java.util.List;

View File

@ -0,0 +1,23 @@
package com.rusefi.can.reader.impl;
import com.rusefi.can.CANPacket;
import com.rusefi.can.reader.CANLineReader;
public class PcanTrcReader implements CANLineReader {
@Override
public CANPacket readLine(String line) {
line = line.trim();
if (line.startsWith(";"))
return null;
String[] tokens = line.split("\\s+");
double timeStamp = Double.parseDouble(tokens[1]);
int sid = Integer.parseInt(tokens[3], 16);
int size = Integer.parseInt(tokens[4]);
byte[] data = CANLineReader.readHexArray(tokens, 5, size);
return new CANPacket(timeStamp, sid, data);
}
}

View File

@ -1,5 +1,7 @@
package com.rusefi.can.reader.impl;
import com.rusefi.can.CANPacket;
import com.rusefi.can.reader.dbc.DbcField;
import com.rusefi.can.reader.dbc.DbcFile;
import org.junit.Test;
@ -8,17 +10,32 @@ import java.io.IOException;
import java.io.StringReader;
import static com.rusefi.can.reader.impl.ParseDBC.VAG_MOTOR_1;
import static org.junit.Assert.assertEquals;
public class GetValueFromTrc {
@Test
public void test() throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(VAG_MOTOR_1));
DbcFile dbc = new DbcFile();
dbc.read(reader);
{
BufferedReader reader = new BufferedReader(new StringReader(VAG_MOTOR_1));
dbc.read(reader);
}
String trcLine = " 3769) 2117.7 Rx 0280 8 01 1D DF 12 1E 00 1A 1E ";
PcanTrcReader reader = new PcanTrcReader();
CANPacket packet = reader.readLine(trcLine);
assertEquals(8, packet.getData().length);
assertEquals(640, packet.getId());
assertEquals(0xDF1D, DbcField.getBitIndex(packet.getData(), 8, 16));
assertEquals(1, DbcField.getBitIndex(packet.getData(), 0, 3));
assertEquals(0x1D, DbcField.getBitIndex(packet.getData(), 8, 8));
assertEquals(13 , DbcField.getBitIndex(packet.getData(), 8, 4));
System.out.println(packet);
}
}