forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobd2.py
More file actions
54 lines (48 loc) · 1.55 KB
/
obd2.py
File metadata and controls
54 lines (48 loc) · 1.55 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
import serial
import obd
import ast
import sys
import time
import argparse
import traceback
from logging import raiseExceptions
sys.path.append('/opt/iotistic-mnvr/pyscripts/')
from network_utils import run
from db_util import *
parser = argparse.ArgumentParser()
# parser.add_argument("-r", "--request", type=str, help="requested data")
args = parser.parse_args()
def connect_to_OBD():
while True:
try:
serialport = '/dev/ttyUSB0' if 'Future' in run('udevadm info /dev/ttyUSB0') else '/dev/ttyUSB4'
connection = obd.OBD(serialport)
return connection
except:
traceback.print_exc()
continue
def init_obd_db():
obd_db = Database('/home/mnvr/Data/obd.db') # GPS_DB SETUP
obd_db.create_table("obd_data")
obd_db.empty_table("obd_data")
return obd_db
if __name__=='__main__':
connection = connect_to_OBD()
obd_db = init_obd_db()
time.sleep(0.5)
while True:
try:
res = connection.query(obd.commands['GET_DTC'])
response = ast.literal_eval(str(res))
print(f'insert into obd_data (candtc) VALUES ("{response}")')
# print(response)
if (str(response)== 'None'):
raise Exception('OBD Connection is not available.')
obd_db.send_query(f'insert into obd_data (candtc) VALUES ("{response}")')
time.sleep(2)
except:
traceback.print_exc()
connection.close()
time.sleep(1)
connection = connect_to_OBD()
continue