forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_protocol_can.py
More file actions
149 lines (97 loc) · 2.75 KB
/
test_protocol_can.py
File metadata and controls
149 lines (97 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import random
from obd.protocols import *
from obd.protocols.protocol import Message
from obd import debug
debug.console = True
CAN_11_PROTOCOLS = [
ISO_15765_4_11bit_500k,
ISO_15765_4_11bit_250k,
]
CAN_29_PROTOCOLS = [
ISO_15765_4_29bit_500k,
ISO_15765_4_29bit_250k,
SAE_J1939
]
def check_message(m, num_frames, tx_id, data_bytes):
""" generic test for correct message values """
assert len(m.frames) == num_frames
assert m.tx_id == tx_id
assert m.data_bytes == data_bytes
def test_single_frame():
for protocol in CAN_11_PROTOCOLS:
p = protocol()
r = p(["7E8 06 41 00 00 01 02 03"])
assert len(r) == 1
check_message(r[0], 1, 0x0, list(range(4)))
def test_hex_straining():
for protocol in CAN_11_PROTOCOLS:
p = protocol()
r = p(["NO DATA"])
assert len(r) == 0
r = p(["TOTALLY NOT HEX"])
assert len(r) == 0
r = p(["NO DATA", "7E8 06 41 00 00 01 02 03"])
assert len(r) == 1
check_message(r[0], 1, 0x0, list(range(4)))
r = p(["NO DATA", "NO DATA"])
assert len(r) == 0
def test_multi_ecu():
for protocol in CAN_11_PROTOCOLS:
p = protocol()
test_case = [
"7E8 06 41 00 00 01 02 03",
"7EB 06 41 00 00 01 02 03",
"7EA 06 41 00 00 01 02 03",
]
correct_data = list(range(4))
# seperate ECUs, single frames each
r = p(test_case)
assert len(r) == 3
# messages are returned in ECU order
check_message(r[0], 1, 0x0, correct_data)
check_message(r[1], 1, 0x2, correct_data)
check_message(r[2], 1, 0x3, correct_data)
def test_multi_line():
for protocol in CAN_11_PROTOCOLS:
p = protocol()
test_case = [
"7E8 10 20 49 04 00 01 02 03",
"7E8 21 04 05 06 07 08 09 0A",
"7E8 22 0B 0C 0D 0E 0F 10 11",
"7E8 23 12 13 14 15 16 17 18"
]
correct_data = list(range(25))
# in-order
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0x0, correct_data)
# test a few out-of-order cases
for n in range(4):
random.shuffle(test_case) # mix up the frame strings
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0x0, correct_data)
# missing frames in a multi-frame message should drop the message
# (tests the contiguity check, and data length byte)
test_case = [
"7E8 10 20 49 04 00 01 02 03",
"7E8 21 04 05 06 07 08 09 0A",
"7E8 22 0B 0C 0D 0E 0F 10 11",
"7E8 23 12 13 14 15 16 17 18"
]
for n in range(len(test_case) - 1):
sub_test = list(test_case)
del sub_test[n]
r = p(sub_test)
assert len(r) == 0
# MODE 03 COMMANDS (GET_DTC) RETURN NO PID BYTE
test_case = [
"7E8 10 20 43 04 00 01 02 03",
"7E8 21 04 05 06 07 08 09 0A",
]
correct_data = list(range(8))
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0, correct_data)
def test_can_29():
pass