22
33from protocol import Protocol
44from frame import Frame
5+ from obd .utils import ascii_to_bytes
56
67
78class CANProtocol (Protocol ):
8- def __init__ (self , baud , id_bits ):
9- Protocol .__init__ (baud )
10- self .id_bits = id_bits
11-
12-
13- def create_frame (self , raw_bytes ):
14- frame = Frame (self , raw_bytes )
15- return frame
16-
17-
18- def parse_frames (self , frames ):
9+ def __init__ (self , baud , id_bits ):
10+ Protocol .__init__ (self , baud )
11+ self .id_bits = id_bits
12+
13+
14+ def parse_frame (self , frame ):
15+
16+ # pad 11-bit CAN headers out to 32 bits for consistency,
17+ # since ELM already does this for 29-bit CAN headers
18+ if self .id_bits == 11 :
19+ frame .raw = "00000" + frame .raw
20+
21+ raw_bytes = ascii_to_bytes (frame .raw )
22+
23+ # read header information
24+ if self .id_bits == 11 :
25+ frame .priority = raw_bytes [2 ] & 0x0F # always 7
26+ frame .addr_mode = raw_bytes [3 ] & 0xF0 # 0xD0 = functional, 0xE0 = physical
27+
28+ if frame .addr_mode == 0xD0 :
29+ #untested("11-bit functional request from tester")
30+ frame .rx_id = raw_bytes [3 ] & 0x0F # usually (always?) 0x0F for broadcast
31+ frame .tx_id = 0xF1 # made-up to mimic all other protocols
32+ elif raw_bytes [3 ] & 0x08 :
33+ frame .rx_id = 0xF1 # made-up to mimic all other protocols
34+ frame .tx_id = raw_bytes [3 ] & 0x07
35+ else :
36+ #untested("11-bit message header from tester (functional or physical)")
37+ frame .tx_id = 0xF1 # made-up to mimic all other protocols
38+ frame .rx_id = raw_bytes [3 ] & 0x07
39+
40+ else : # self.id_bits == 29:
41+ frame .priority = raw_bytes [0 ] # usually (always?) 0x18
42+ frame .addr_mode = raw_bytes [1 ] # DB = functional, DA = physical
43+ frame .rx_id = raw_bytes [2 ] # 0x33 = broadcast (functional)
44+ frame .tx_id = raw_bytes [3 ] # 0xF1 = tester ID
45+
46+ frame .data_bytes = raw_bytes [5 :]
47+
48+
49+ def parse_message (self , message ):
1950 pass
2051
2152
@@ -27,26 +58,27 @@ def parse_frames(self, frames):
2758##############################################
2859
2960
61+
3062class ISO_15765_4_11bit_500k (CANProtocol ):
31- def __init__ (self ):
32- CANProtocol .__init__ (baud = 500000 , id_bits = 11 )
63+ def __init__ (self ):
64+ CANProtocol .__init__ (self , baud = 500000 , id_bits = 11 )
3365
3466
3567class ISO_15765_4_29bit_500k (CANProtocol ):
36- def __init__ (self ):
37- CANProtocol .__init__ (baud = 500000 , id_bits = 29 )
68+ def __init__ (self ):
69+ CANProtocol .__init__ (self , baud = 500000 , id_bits = 29 )
3870
3971
4072class ISO_15765_4_11bit_250k (CANProtocol ):
41- def __init__ (self ):
42- CANProtocol .__init__ (baud = 250000 , id_bits = 11 )
73+ def __init__ (self ):
74+ CANProtocol .__init__ (self , baud = 250000 , id_bits = 11 )
4375
4476
4577class ISO_15765_4_29bit_250k (CANProtocol ):
46- def __init__ (self ):
47- CANProtocol .__init__ (baud = 250000 , id_bits = 29 )
78+ def __init__ (self ):
79+ CANProtocol .__init__ (self , baud = 250000 , id_bits = 29 )
4880
4981
5082class SAE_J1939 (CANProtocol ):
51- def __init__ (self ):
52- CANProtocol .__init__ (baud = 250000 , id_bits = 29 )
83+ def __init__ (self ):
84+ CANProtocol .__init__ (self , baud = 250000 , id_bits = 29 )
0 commit comments