Skip to content

Commit 84112b4

Browse files
committed
ooof, didnt add the canbus sim files. here they are
1 parent 44e031a commit 84112b4

File tree

5 files changed

+702
-0
lines changed

5 files changed

+702
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
variable name;data type;register;documented name;length;writeable;unit;values;note;
2+
;USHORT;15;debug;2byte;R;10mA;;Positive: charging Negative: discharging;

protocols/debug.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"reader" : "pace",
3+
"batch_size": 1
4+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
variable name;data type;register;documented name;length;writeable;unit;values;note;
2+
;SHORT;0;Current;2byte;R;10mA;;Positive: charging Negative: discharging;
3+
;USHORT;1;Voltage of pack;2byte;R;10mV;;;
4+
;8BIT;2;SOC;2byte;R;%;0~100%;;
5+
;8BIT;3;SOH;2byte;R;%;0~100%;;
6+
;USHORT;4;Remain capacity;2byte;R;10mAH;;;
7+
;USHORT;5;Full capacity;2byte;R;10mAH;;;
8+
;USHORT;6;Design capacity;2byte;R;10mAH;;;
9+
;USHORT;7;Battery cycle counts;2byte;R;Cycles;;;
10+
;16BIT_FLAGS;9;Warning flag;2byte;R;;;See description-1;
11+
;16BIT_FLAGS ;10;Protection flag;2byte;R;;;See description-2;
12+
;8BIT_FLAGS ;11;Fault flag;2byte;R;;;See description-3;
13+
;8BIT_FLAGS ;11.b8;Status Flag;;R;;;;
14+
;16BIT_FLAGS ;12;Balance status;2byte;R;;;;
15+
;USHORT;15;Cell voltage 1;32byte;R;mV;;Voltage of 16 cells; 2 byte for each cell
16+
;USHORT;16;Cell voltage 2;;R;mV;;;
17+
;USHORT;17;Cell voltage 3;;R;mV;;;
18+
;USHORT;18;Cell voltage 4;;R;mV;;;
19+
;USHORT;19;Cell voltage 5;;R;mV;;;
20+
;USHORT;20;Cell voltage 6;;R;mV;;;
21+
;USHORT;21;Cell voltage 7;;R;mV;;;
22+
;USHORT;22;Cell voltage 8;;R;mV;;;
23+
;USHORT;23;Cell voltage 9;;R;mV;;;
24+
;USHORT;24;Cell voltage 10;;R;mV;;;
25+
;USHORT;25;Cell voltage 11;;R;mV;;;
26+
;USHORT;26;Cell voltage 12;;R;mV;;;
27+
;USHORT;27;Cell voltage 13;;R;mV;;;
28+
;USHORT;28;Cell voltage 14;;R;mV;;;
29+
;USHORT;29;Cell voltage 15;;R;mV;;;
30+
;USHORT;30;Cell voltage 16;;R;mV;;;
31+
;4bit;31;Cell temperature 1;8byte;R;01.c;;4 cell temperature; 2 byte for each cell
32+
;4bit;31.b4;Cell temperature 2;;R;01.c;;;
33+
;4bit;31.b8;Cell temperature 3;;R;01.c;;;
34+
;4bit;31.b12;Cell temperature 4;;R;01.c;;;
35+
;4bit;32;Cell temperature 5;;R;01.c;;;
36+
;4bit;32.b4;Cell temperature 6;;R;01.c;;;
37+
;4bit;32.b8;Cell temperature 7;;R;01.c;;;
38+
;4bit;32.b12;Cell temperature 8;;R;01.c;;;
39+
;4bit;33;Cell temperature 9;;R;01.c;;;
40+
;4bit;33.b4;Cell temperature 10;;R;01.c;;;
41+
;4bit;33.b8;Cell temperature 11;;R;01.c;;;
42+
;4bit;33.b12;Cell temperature 12;;R;01.c;;;
43+
;4bit;34;Cell temperature 13;;R;01.c;;;
44+
;4bit;34.b4;Cell temperature 14;;R;01.c;;;
45+
;4bit;34.b8;Cell temperature 15;;R;01.c;;;
46+
;4bit;34.b12;Cell temperature 16;;R;01.c;;;
47+
;SHORT;35;MOSFET temperature;2byte;R;0.1c;;Or invalid;
48+
;SHORT;36;Environment temperature;2byte;R;0.1c;;Or invalid;
49+
;USHORT;60;Pack OV alarm;2byte;RW;mV;;;
50+
;USHORT;61;Pack OV protection;2byte;RW;mV;;;
51+
;USHORT;62;Pack OV release protection;2byte;RW;mV;;;
52+
;UINT8;63;Pack OV protection delay time;2byte;RW;0.1S;1~255;;
53+
;USHORT;64;Cell OV alarm;2byte;RW;mV;;;
54+
;USHORT;65;Cell OV protection;2byte;RW;mV;;;
55+
;USHORT;66;Cell OV release protection;2byte;RW;mV;;;
56+
;UINT8;67;Cell OV protection delay time;2byte;RW;0.1S;1~255;;
57+
;USHORT;68;Pack UV alarm;2byte;RW;mV;;;
58+
;USHORT;69;Pack UV protection;2byte;RW;mV;;;
59+
;USHORT;70;Pack UV release protection;2byte;RW;mV;;;
60+
;UINT8;71;Pack UV protection delay time;2byte;RW;0.1S;1~255;;
61+
;USHORT;72;Cell UV alarm;2byte;RW;mV;;;
62+
;USHORT;73;Cell UV protection;2byte;RW;mV;;;
63+
;USHORT;74;Cell UV release protection;2byte;RW;mV;;;
64+
;UINT8;75;Cell UV protection delay time;2byte;RW;0.1S;1~255;;
65+
;USHORT;76;Charging OC alarm;2byte;RW;A;;;
66+
;USHORT;77;Charging OC protection;2byte;RW;A;;;
67+
;UINT8;78;Charging OC protection delay time;2byte;RW;0.1S;1~255;;
68+
;USHORT;79;Discharging OC alarm;2byte;RW;A;;;
69+
;USHORT;80;DischargingOCprotection;2byte;RW;A;;;
70+
;UINT8;81;DischargingOCprotection delay time;2byte;RW;0.1S;1~255;;
71+
;USHORT;82;Discharging OC-2 protection;2byte;RW;A;;;
72+
;UINT8;83;Discharging OC-2 protection delay time;2byte;RW;0.025S;1~255;;
73+
;SHORT;84;Charging OT alarm;2byte;RW;0.1c;;;
74+
;SHORT;85;Charging OT protection;2byte;RW;0.1c;;;
75+
;SHORT;86;Charging OT release protection;2byte;RW;0.1c;;;
76+
;SHORT;87;Discharging OT alarm;2byte;RW;0.1c;;;
77+
;SHORT;88;DischargingOTprotection;2byte;RW;0.1c;;;
78+
;SHORT;89;Discharging OT release;2byte;RW;0.1c;;;
79+
;SHORT;90;Charging UT alarm;2byte;RW;0.1c;;;
80+
;SHORT;91;Charging UT protection;2byte;RW;0.1c;;;
81+
;SHORT;92;Charging UT release protection;2byte;RW;0.1c;;;
82+
;SHORT;93;Discharging UT alarm;2byte;RW;0.1c;;;
83+
;SHORT;94;DischargingUTprotection;2byte;RW;0.1c;;;
84+
;SHORT;95;Discharging UT release protection;2byte;RW;0.1c;;;
85+
;SHORT;96;MOSFET OT alarm;2byte;RW;0.1c;;Or invalid parameters in BMS-4820;
86+
;SHORT;97;MOSFET OT protection;2byte;RW;0.1c;;;
87+
;SHORT;98;MOSFET OT release protection;2byte;RW;0.1c;;;
88+
;SHORT;99;Environment OT alarm;2byte;RW;0.1c;;;
89+
;SHORT;100;EnvironmentOTprotection;2byte;RW;0.1c;;;
90+
;SHORT;101;Environment OT release protection;2byte;RW;0.1c;;;
91+
;SHORT;102;Environment UT alarm;2byte;RW;0.1c;;;
92+
;SHORT;103;EnvironmentUTprotection;2byte;RW;0.1c;;;
93+
;SHORT;104;Environment UT release protection;2byte;RW;0.1c;;;
94+
;USHORT;105;Balance start cell voltage;2byte;RW;mV;;;
95+
;USHORT;106;Balance start delta voltage;2byte;RW;mV;;;
96+
;USHORT;107;Pack full-charge voltage;2byte;RW;mV;;;
97+
;USHORT;108;Pack full-charge current;2byte;RW;mA;;;
98+
;USHORT;109;Cell sleep voltage;2byte;RW;mV;;;
99+
;USHORT;110;Cell sleep delay time;2byte;RW;min;;;
100+
;UINT8;111;Short circuit protect delay time;2byte;RW;25uS;1~20;Max 500uS;
101+
;UINT8;112;SOC alarm threshold;2byte;RW;%;0~100%;;
102+
;USHORT;113;Charging OC-2 protection;2byte;RW;A;;;
103+
;UINT8;114;Charging OC-2 protection delay time;2byte;RW;0.025S;1~255;;
104+
;;0150-0159;Version information;20byte;R;ASCII;;;
105+
;;0160-0169;Model SN;20byte;RW;ASCII;;BMS Manufacturer;
106+
Serial Number ;ASCII;0170-0179;PACK SN;20byte;RW;ASCII;;PACK Manufacturer;

tools/canbus_server_sim.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import subprocess
2+
import can
3+
import time
4+
import atexit
5+
import signal
6+
import sys
7+
import os
8+
from pathlib import Path
9+
10+
11+
12+
VCAN_IFACE = 'vcan0'
13+
vcan_messages = []
14+
15+
16+
def load_candump_file(filepath):
17+
os.chdir(Path(__file__).resolve().parent)
18+
19+
messages = []
20+
21+
with open(filepath, 'r') as f:
22+
for line in f:
23+
line = line.strip()
24+
if not line or '#' not in line:
25+
continue
26+
27+
try:
28+
can_id_str, data_str = line.split('#')
29+
can_id = int(can_id_str, 16)
30+
data = bytes.fromhex(data_str)
31+
32+
msg = can.Message(
33+
arbitration_id=can_id,
34+
data=data,
35+
is_extended_id=False
36+
)
37+
messages.append(msg)
38+
except Exception as e:
39+
print(f"Failed to parse line '{line}': {e}")
40+
41+
return messages
42+
43+
44+
def emulate_device():
45+
bus = can.interface.Bus(channel='vcan0', bustype='socketcan')
46+
47+
while True:
48+
for msg in vcan_messages:
49+
try:
50+
bus.send(msg)
51+
print(f"Sent message: {msg}")
52+
except can.CanError:
53+
print("Message NOT sent")
54+
time.sleep(1) # Send message every 1 second
55+
56+
def setup_vcan(interface=VCAN_IFACE):
57+
try:
58+
# Load vcan kernel module
59+
subprocess.run(['sudo', 'modprobe', 'vcan'], check=True)
60+
61+
# Add virtual CAN interface
62+
subprocess.run(['sudo', 'ip', 'link', 'add', 'dev', interface, 'type', 'vcan'], check=True)
63+
64+
# Bring the interface up
65+
subprocess.run(['sudo', 'ip', 'link', 'set', 'up', interface], check=True)
66+
67+
print(f"Virtual CAN interface {interface} is ready.")
68+
except subprocess.CalledProcessError as e:
69+
print(f"Failed to set up {interface}: {e}")
70+
71+
72+
def cleanup_vcan(interface=VCAN_IFACE):
73+
try:
74+
subprocess.run(['sudo', 'ip', 'link', 'delete', interface], check=True)
75+
print(f"Removed {interface}")
76+
except subprocess.CalledProcessError as e:
77+
print(f"Error removing {interface}: {e}")
78+
79+
# Register cleanup to run at program exit
80+
atexit.register(cleanup_vcan)
81+
82+
# Optional: Handle Ctrl+C gracefully
83+
signal.signal(signal.SIGINT, lambda sig, frame: sys.exit(0))
84+
85+
86+
if __name__ == "__main__":
87+
setup_vcan()
88+
vcan_messages = load_candump_file("candump.txt")
89+
emulate_device()
90+

0 commit comments

Comments
 (0)