Repository navigation
Expand file tree
/
Copy pathprotocol.py
More file actions
148 lines (110 loc) · 5 KB
/
Copy pathprotocol.py
File metadata and controls
148 lines (110 loc) · 5 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
########################################################################
# #
# python-OBD: A python OBD-II serial module derived from pyobd #
# #
# Copyright 2004 Donour Sizemore (donour@uchicago.edu) #
# Copyright 2009 Secons Ltd. (www.obdtester.com) #
# Copyright 2009 Peter J. Creath #
# Copyright 2015 Brendan Whitfield (bcw7044@rit.edu) #
# #
########################################################################
# #
# protocols/protocol.py #
# #
# This file is part of python-OBD (a derivative of pyOBD) #
# #
# python-OBD is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 2 of the License, or #
# (at your option) any later version. #
# #
# python-OBD is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with python-OBD. If not, see <http://www.gnu.org/licenses/>. #
# #
########################################################################
from obd.utils import ascii_to_bytes, isHex
from obd.debug import debug
"""
Basic data models for all protocols to use
"""
class Frame(object):
def __init__(self, raw):
self.raw = raw
self.data_bytes = []
self.priority = None
self.addr_mode = None
self.rx_id = None
self.tx_id = None
self.type = None
self.seq_index = 0 # only used when type = CF
self.data_len = None
class Message(object):
def __init__(self, frames, tx_id):
self.frames = frames
self.tx_id = tx_id
self.data_bytes = []
def __eq__(self, other):
if isinstance(other, Message):
for attr in ["frames", "tx_id", "data_bytes"]:
if getattr(self, attr) != getattr(other, attr):
return False
return True
else:
return False
"""
Protocol objects are stateless factories for Frames and Messages.
They are __called__ with the raw string response, and return a
list of Messages.
"""
class Protocol(object):
PRIMARY_ECU = None
def __init__(self, baud=38400):
self.baud = baud
def __call__(self, lines):
# ditch spaces
lines = [line.replace(' ', '') for line in lines]
# ditch frames without valid hex (trashes "NO DATA", etc...)
lines = filter(isHex, lines)
frames = []
for line in lines:
# subclass function to parse the lines into Frames
frame = self.create_frame(line)
# drop frames that couldn't be parsed
if frame is not None:
frames.append(frame)
# group frames by transmitting ECU (tx_id)
ecus = {}
for frame in frames:
if frame.tx_id not in ecus:
ecus[frame.tx_id] = [frame]
else:
ecus[frame.tx_id].append(frame)
messages = []
for ecu in ecus:
# subclass function to assemble frames into Messages
message = self.create_message(ecus[ecu], ecu)
# drop messages that couldn't be assembled
if message is not None:
messages.append(message)
return messages
def create_frame(self, raw):
"""
override in subclass for each protocol
Function recieves a list of byte values for a frame.
Function should return a Frame instance. If fatal errors were
found, this function should return None (the Frame is dropped).
"""
raise NotImplementedError()
def create_message(self, frames):
"""
override in subclass for each protocol
Function recieves a list of Frame objects.
Function should return a Message instance. If fatal errors were
found, this function should return None (the Message is dropped).
"""
raise NotImplementedError()