Skip to content

Commit 6d1c071

Browse files
committed
[bug]:fix dbc calc unmatch
1 parent 47030b3 commit 6d1c071

File tree

3 files changed

+113
-12
lines changed

3 files changed

+113
-12
lines changed

src/renderer/src/database/dbc/calc.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,14 @@ function readSignalFromBuffer(signal: Signal, data: Buffer, db: DBC) {
215215
} else {
216216
// Motorola格式 (Big Endian)
217217
let startByte = Math.floor(signal.startBit / 8)
218-
let startBitInByte = 7 - (signal.startBit % 8)
218+
let startBitInByte = signal.startBit % 8
219219
let remainingBits = signal.length
220220

221-
while (remainingBits > 0 && startByte >= 0 && startByte < data.length) {
222-
const bitsInThisByte = Math.min(startBitInByte + 1, remainingBits)
223-
const position = startBitInByte - bitsInThisByte + 1
221+
while (remainingBits > 0) {
222+
if (startByte < 0 || startByte >= data.length) break
223+
224+
const bitsInThisByte = Math.min(8 - startBitInByte, remainingBits)
225+
const position = startBitInByte
224226
const mask = (1 << bitsInThisByte) - 1
225227
const value = (data[startByte] >> position) & mask
226228

@@ -391,24 +393,23 @@ function writeSignalToBuffer(signal: Signal, data: Buffer) {
391393
} else {
392394
// Motorola格式 (Big Endian)
393395
let startByte = Math.floor(signal.startBit / 8)
394-
let startBitInByte = 7 - (signal.startBit % 8)
396+
let startBitInByte = signal.startBit % 8
395397
let remainingBits = signal.length
396-
const valueIndex = signal.length - 1
397398

398399
while (remainingBits > 0) {
399-
if (startByte >= data.length) break
400+
if (startByte < 0 || startByte >= data.length) break
400401

401-
const bitsInThisByte = Math.min(startBitInByte + 1, remainingBits)
402+
const bitsInThisByte = Math.min(8 - startBitInByte, remainingBits)
403+
const position = startBitInByte
402404
const mask = (1 << bitsInThisByte) - 1
403405
const value = (rawValue >> (signal.length - remainingBits)) & mask
404-
const position = startBitInByte - bitsInThisByte + 1
405406

406407
data[startByte] &= ~(mask << position)
407408
data[startByte] |= value << position
408409

409410
remainingBits -= bitsInThisByte
410-
startByte -= 1
411-
startBitInByte = 7
411+
startByte += 1
412+
startBitInByte = 0
412413
}
413414
}
414415
}

test/dbc/ID200.dbc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
VERSION ""
2+
3+
4+
NS_ :
5+
NS_DESC_
6+
CM_
7+
BA_DEF_
8+
BA_
9+
VAL_
10+
CAT_DEF_
11+
CAT_
12+
FILTER
13+
BA_DEF_DEF_
14+
EV_DATA_
15+
ENVVAR_DATA_
16+
SGTYPE_
17+
SGTYPE_VAL_
18+
BA_DEF_SGTYPE_
19+
BA_SGTYPE_
20+
SIG_TYPE_REF_
21+
VAL_TABLE_
22+
SIG_GROUP_
23+
SIG_VALTYPE_
24+
SIGTYPE_VALTYPE_
25+
BO_TX_BU_
26+
BA_DEF_REL_
27+
BA_REL_
28+
BA_DEF_DEF_REL_
29+
BU_SG_REL_
30+
BU_EV_REL_
31+
BU_BO_REL_
32+
SG_MUL_VAL_
33+
34+
BS_:
35+
36+
BU_: IG_Node
37+
38+
39+
BO_ 512 Message_200: 8 Vector__XXX
40+
SG_ LwBmIO : 0|1@0+ (1,0) [0|1] "" Vector__XXX
41+
SG_ HhBmIO : 7|1@0+ (1,0) [0|1] "" Vector__XXX
42+
43+
BO_ 12 ID00CUI_status: 8 Vector__XXX
44+
SG_ UI_audioActive : 1|1@1+ (1,0) [0|1] "" Vector__XXX
45+
SG_ UI_autopilotTrial : 12|2@1+ (1,0) [0|3] "" Vector__XXX
46+
SG_ UI_bluetoothActive : 2|1@1+ (1,0) [0|1] "" Vector__XXX
47+
SG_ UI_cameraActive : 41|1@1+ (1,0) [0|1] "" Vector__XXX
48+
SG_ UI_cellActive : 3|1@1+ (1,0) [0|1] "" Vector__XXX
49+
SG_ UI_cellConnected : 10|1@1+ (1,0) [0|1] "" Vector__XXX
50+
SG_ UI_cellNetworkTechnology : 19|4@1+ (1,0) [0|15] "" Vector__XXX
51+
SG_ UI_cellVector__XXXPower : 24|8@1+ (1,-128) [-128|127] "dB" Vector__XXX
52+
SG_ UI_cellSignalBars : 42|3@1+ (1,0) [0|7] "" Vector__XXX
53+
SG_ UI_cpuTemperature : 56|8@1- (1,40) [-20|100] "C" Vector__XXX
54+
SG_ UI_developmentCar : 40|1@1+ (1,0) [0|1] "" Vector__XXX
55+
SG_ UI_displayOn : 5|1@1+ (1,0) [0|1] "" Vector__XXX
56+
SG_ UI_displayReady : 4|1@1+ (1,0) [0|1] "" Vector__XXX
57+
SG_ UI_factoryReset : 14|2@1+ (1,0) [0|3] "" Vector__XXX
58+
SG_ UI_falseTouchCounter : 32|8@1+ (1,0) [0|255] "1" Vector__XXX
59+
SG_ UI_gpsActive : 16|1@1+ (1,0) [0|1] "" Vector__XXX
60+
SG_ UI_pcbTemperature : 48|8@1- (1,40) [-20|100] "C" Vector__XXX
61+
SG_ UI_radioActive : 18|1@1+ (1,0) [0|1] "" Vector__XXX
62+
SG_ UI_readyForDrive : 9|1@1+ (1,0) [0|1] "" Vector__XXX
63+
SG_ UI_screenshotActive : 17|1@1+ (1,0) [0|1] "" Vector__XXX
64+
SG_ UI_systemActive : 8|1@1+ (1,0) [0|1] "" Vector__XXX
65+
SG_ UI_touchActive : 0|1@1+ (1,0) [0|1] "" Vector__XXX
66+
SG_ UI_vpnActive : 11|1@1+ (1,0) [0|1] "" Vector__XXX
67+
SG_ UI_wifiActive : 6|1@1+ (1,0) [0|1] "" Vector__XXX
68+
SG_ UI_wifiConnected : 7|1@1+ (1,0) [0|1] "" Vector__XXX
69+
70+
BA_DEF_ "BusType" STRING ;
71+
BA_DEF_DEF_ "BusType" "CAN";
72+

test/dbc/dbc.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { beforeAll, describe, expect, test } from 'vitest'
22
import parse, { isCanFd } from 'src/renderer/src/database/dbcParse'
3+
import { updateSignalPhys } from 'src/renderer/src/database/dbc/calc'
34
import fs from 'fs'
45
import path from 'path'
56
import { CAN_ID_TYPE } from 'src/main/share/can'
@@ -17,6 +18,7 @@ describe('DBC Parser Tests', () => {
1718
let floatDbc: string
1819
let evDbc: string
1920
let sgDbc: string
21+
let id200Dbc: string
2022
beforeAll(() => {
2123
dbcContentModel3 = fs.readFileSync(path.join(__dirname, 'Model3CAN.dbc'), 'utf-8')
2224
dbcContentHyundaiKia = fs.readFileSync(
@@ -31,8 +33,34 @@ describe('DBC Parser Tests', () => {
3133
floatDbc = fs.readFileSync(path.join(__dirname, 'float.dbc'), 'utf-8')
3234
evDbc = fs.readFileSync(path.join(__dirname, 'ev.dbc'), 'utf-8')
3335
sgDbc = fs.readFileSync(path.join(__dirname, 'sig_group.dbc'), 'utf-8')
36+
id200Dbc = fs.readFileSync(path.join(__dirname, 'ID200.dbc'), 'utf-8')
3437
})
38+
test('id200', () => {
39+
const result = parse(id200Dbc)
40+
expect(result).toBeDefined()
41+
expect(result.messages[0x200].name).toBe('Message_200')
42+
const s = result.messages[0x200].signals['HhBmIO']
43+
const s1 = result.messages[0x200].signals['LwBmIO']
44+
45+
s.physValue = 1
46+
s1.physValue = 1
47+
updateSignalPhys(s)
48+
updateSignalPhys(s1)
49+
50+
const buf = getMessageData(result.messages[0x200])
51+
expect(buf).toEqual(Buffer.from([0x81, 0, 0, 0, 0, 0, 0, 0]))
52+
53+
const ns = result.messages[12].signals['UI_audioActive']
54+
const ns1 = result.messages[12].signals['UI_cellVector__XXXPower']
3555

56+
ns.physValue = 1
57+
ns1.physValue = -116
58+
updateSignalPhys(ns)
59+
updateSignalPhys(ns1)
60+
61+
const buf1 = getMessageData(result.messages[12])
62+
expect(buf1).toEqual(Buffer.from([0x2, 0, 0, 0xc, 0, 0, 0, 0]))
63+
})
3664
test('dbc model3', () => {
3765
const result = parse(dbcContentModel3)
3866
// Add assertions to verify the parsed values for Model3CAN.dbc
@@ -66,7 +94,7 @@ describe('DBC Parser Tests', () => {
6694
// Add assertions to verify the parsed values for can1-hyundai-kia-uds-v2.4.dbc
6795
expect(result).toBeDefined()
6896
expect(result.environmentVariables['V2CTxTime'].name).toBe('V2CTxTime')
69-
console.log(result.environmentVariables['V2CTxTime'])
97+
7098
// Add more specific assertions based on the expected structure of can1-hyundai-kia-uds-v2.4.dbc
7199
})
72100
test('float', () => {

0 commit comments

Comments
 (0)