forked from EVNotify/PlugAndPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathELM327.py
60 lines (47 loc) · 1.73 KB
/
ELM327.py
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
from serial import Serial
import logging
from pexpect import fdpexpect
class ELM327:
def __init__(self, dongle):
self.serial = Serial(dongle['port'], baudrate=dongle['speed'], timeout=5)
self.exp = fdpexpect.fdspawn(self.serial.fd)
self.initDongle()
def sendAtCmd(self, cmd, expect='OK'):
expect = bytes(expect, 'utf-8')
cmd = bytes(cmd, 'utf-8')
logging.info('Write %s' % str(cmd))
self.exp.send(cmd + b'\r\n')
self.exp.expect('>')
ret = self.exp.before.strip(b'\r\n')
logging.debug('Got %s' % str(cmd))
if expect not in ret:
raise Exception('Expected %s, got %s' % (expect,ret))
def sendCommand(self, cmd):
cmd = bytes(cmd, 'utf-8')
logging.info('Write %s' % str(cmd))
self.exp.send(cmd + b'\r\n')
self.exp.expect('>')
return self.exp.before.strip(b'\r\n').split(b'\r\n')
def initDongle(self):
cmds = [['ATZ','OK'],
['ATE0','OK'],
['ATL1','OK'],
['ATS0','OK'],
['ATH1','OK'],
['ATSTFF','OK'],
['ATFE','OK']]
for c,r in cmds:
self.sendAtCmd(c, r)
def setAllowLongMessages(self, value):
self.sendAtCmd('ATAL%i' % value)
def setProtocol(self, prot):
if prot == 'CAN_11_500':
self.sendAtCmd('ATSP6','6 = ISO 15765-4, CAN (11/500)')
else:
raise Exception('Unsupported protocol %s' % prot)
def setIDFilter(self, filter):
self.sendAtCmd('ATCF' + str(filter))
def setCANRxMask(self, mask):
self.sendAtCmd('ATCM' + str(mask))
def setCANRxFilter(self, addr):
self.sendAtCmd('ATCRA' + str(addr))