forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_OBD.py
More file actions
101 lines (79 loc) · 2.98 KB
/
test_OBD.py
File metadata and controls
101 lines (79 loc) · 2.98 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
import obd
from obd.utils import Response
from obd.commands import OBDCommand
from obd.decoders import noop
from obd.protocols import SAE_J1850_PWM
def test_is_connected():
o = obd.OBD("/dev/null")
assert not o.is_connected()
# todo
# TODO: rewrite for new protocol architecture
def test_query():
# we don't need an actual serial connection
o = obd.OBD("/dev/null")
# forge our own command, to control the output
cmd = OBDCommand("TEST", "Test command", "01", "23", 2, noop, False)
# forge IO from the car by overwriting the read/write functions
# buffers
toCar = [""] # needs to be inside mutable object to allow assignment in closure
fromCar = ""
def write(cmd):
toCar[0] = cmd
o.is_connected = lambda *args: True
o.port.is_connected = lambda *args: True
o.port._ELM327__protocol = SAE_J1850_PWM()
o.port._ELM327__primary_ecu = 0x10
o.port._ELM327__write = write
o.port._ELM327__read = lambda *args: fromCar
# make sure unsupported commands don't write ------------------------------
fromCar = ["48 6B 10 41 23 AB CD 10"]
r = o.query(cmd)
assert toCar[0] == ""
assert r.is_null()
# a correct command transaction -------------------------------------------
fromCar = ["48 6B 10 41 23 AB CD 10"] # preset the response
r = o.query(cmd, force=True) # run
assert toCar[0] == "0123" # verify that the command was sent correctly
assert not r.is_null()
assert r.value == "ABCD" # verify that the response was parsed correctly
# response of greater length ----------------------------------------------
fromCar = ["48 6B 10 41 23 AB CD EF 10"]
r = o.query(cmd, force=True)
assert toCar[0] == "0123"
assert r.value == "ABCD"
# response of lesser length -----------------------------------------------
fromCar = ["48 6B 10 41 23 AB 10"]
r = o.query(cmd, force=True)
assert toCar[0] == "0123"
assert r.value == "AB00"
# NO DATA response --------------------------------------------------------
fromCar = ["NO DATA"]
r = o.query(cmd, force=True)
assert r.is_null()
# malformed response ------------------------------------------------------
fromCar = ["totaly not hex!@#$"]
r = o.query(cmd, force=True)
assert r.is_null()
# no response -------------------------------------------------------------
fromCar = [""]
r = o.query(cmd, force=True)
assert r.is_null()
# reject responses from other ECUs ---------------------------------------
fromCar = ["48 6B 12 41 23 AB CD 10"]
r = o.query(cmd, force=True)
assert toCar[0] == "0123"
assert r.is_null()
# filter for primary ECU --------------------------------------------------
fromCar = ["48 6B 12 41 23 AB CD 10", "48 6B 10 41 23 AB CD 10"]
r = o.query(cmd, force=True)
assert toCar[0] == "0123"
assert r.value == "ABCD"
'''
# ignore multiline responses ----------------------------------------------
fromCar = ["48 6B 10 41 23 AB CD 10", "48 6B 10 41 23 AB CD 10"]
r = o.query(cmd, force=True)
assert toCar[0] == "0123"
assert r.is_null()
'''
def test_load_commands():
pass