now supporting offset

This commit is contained in:
rusefillc 2022-08-27 19:57:29 -04:00
parent 984089898f
commit dd10271de9
3 changed files with 64 additions and 26 deletions

View File

@ -7,17 +7,44 @@ public class DbcField {
private final int startOffset;
private final int length;
private final double mult;
private final double offset;
private String category;
private boolean isNiceName;
public DbcField(String name, int startOffset, int length, double mult, String category) {
public DbcField(String name, int startOffset, int length, double mult, double offset, String category) {
this.name = name;
this.startOffset = startOffset;
this.length = length;
this.mult = mult;
this.offset = offset;
this.category = category;
}
public static DbcField parseField(DbcPacket parent, String line) {
line = DbcFile.replaceSpecialWithSpaces(line);
String[] tokens = line.split(" ");
String name = tokens[1];
int index = 1;
while (!tokens[index - 1].equals(":"))
index++;
if (DbcFile.debugEnabled)
System.out.println(line);
int startOffset;
try {
startOffset = Integer.parseInt(tokens[index]);
} catch (NumberFormatException e) {
throw new IllegalStateException("While " + line, e);
}
int length = Integer.parseInt(tokens[index + 1]);
double mult = Double.parseDouble(tokens[index + 3]);
double offset = Double.parseDouble(tokens[index + 4]);
return new DbcField(name, startOffset, length, mult, offset, parent.getName());
}
public String getCategory() {
return category;
}
@ -42,6 +69,10 @@ public class DbcField {
return mult;
}
public double getOffset() {
return offset;
}
@Override
public String toString() {
return "DbcField{" +
@ -70,7 +101,7 @@ public class DbcField {
}
public double getValue(CANPacket packet) {
return getBitIndex(packet.getData(), startOffset, length) * mult;
return getBitIndex(packet.getData(), startOffset, length) * mult + offset;
}
public void rename(String niceName) {

View File

@ -6,7 +6,6 @@ import com.rusefi.sensor_logs.BinaryLogEntry;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
@ -14,7 +13,7 @@ import java.util.Objects;
public class DbcFile {
public final LinkedHashMap<Integer, DbcPacket> packets = new LinkedHashMap<>();
private static final boolean debugEnabled = false;
public static final boolean debugEnabled = false;
private List<BinaryLogEntry> list;
@ -60,27 +59,7 @@ public class DbcFile {
} else if (line.startsWith("SG_")) {
line = replaceSpecialWithSpaces(line);
String[] tokens = line.split(" ");
String name = tokens[1];
int index = 1;
while (!tokens[index - 1].equals(":"))
index++;
if (debugEnabled)
System.out.println(line);
int startOffset;
try {
startOffset = Integer.parseInt(tokens[index]);
} catch (NumberFormatException e) {
throw new IllegalStateException("While " + line, e);
}
int length = Integer.parseInt(tokens[index + 1]);
double mult = Double.parseDouble(tokens[index + 3]);
DbcField field = new DbcField(name, startOffset, length, mult, currentPacket.getName());
DbcField field = DbcField.parseField(currentPacket, line);
if (debugEnabled)
System.out.println("Found " + field);
currentPacket.add(field);
@ -109,7 +88,7 @@ public class DbcFile {
packets.put(currentPacket.getId(), currentPacket);
}
private String replaceSpecialWithSpaces(String line) {
public static String replaceSpecialWithSpaces(String line) {
line = line.replaceAll("[|+@(,)\\[\\]]", " ");
line = line.replaceAll(" +", " ");
return line;

View File

@ -0,0 +1,28 @@
package com.rusefi.can.reader.impl;
import com.rusefi.can.CANPacket;
import com.rusefi.can.reader.dbc.DbcField;
import com.rusefi.can.reader.dbc.DbcPacket;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ParseDbcFieldTest {
private static final double EPS = 0.001;
@Test
public void parseIat() {
String line = "SG_ Ansauglufttemperatur : 8|8@1+ (0.75,-48) [-48|142.5] \"\" XXX";
DbcPacket parent = new DbcPacket(1, "hello");
DbcField iatField = DbcField.parseField(parent, line);
assertEquals("Ansauglufttemperatur", iatField.getName());
assertEquals(0.75, iatField.getMult(), EPS);
assertEquals(-48, iatField.getOffset(), EPS);
CANPacket packet = new PcanTrcReader().readLine(" 2197) 1234.8 Rx 0380 8 00 62 FA 00 22 00 00 FA");
assertEquals(8, packet.getData().length);
assertEquals(25.5, iatField.getValue(packet), EPS);
}
}