-
Notifications
You must be signed in to change notification settings - Fork 33
/
J1939Cat
executable file
·66 lines (50 loc) · 2.18 KB
/
J1939Cat
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
#!/usr/bin/env python3
import sys
import readline
import rlcompleter
readline.parse_and_bind("tab: complete")
import os
import cancatlib.j1939stack
from cancatlib import *
intro = """'J1939Cat, bringing sniffing and interacting with J1939 devices to Python!'
currently your environment has an object called "c" for CanCat. this is how
you interact with the CanCat tool:
>>> c.ping()
>>> c.placeBookmark('')
>>> c.snapshotCanMsgs()
>>> c.printSessionStats()
>>> c.printCanMsgs()
>>> c.printCanSessions()
>>> c.CANxmit('message', )
>>> c.CANreplay()
>>> c.saveSessionToFile('file_to_save_session_to')
>>> help(c)
"""
if __name__ == "__main__":
import argparse
# Make it easy to find modules in CWD
sys.path.append('.')
interfaces = [iface[:-9] for iface in globals().keys() if iface.endswith('Interface')]
interface_names = ', '.join(interfaces)
bauds = [baud[4:-3] for baud in globals().keys() if baud.startswith('CAN_') and baud.endswith('BPS')]
baud_nums = ', '.join(bauds)
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', help='eg: /dev/ttyACM0')
parser.add_argument('-f', '--filename', help='Load file (does not require CanCat device)')
parser.add_argument('-I', '--interface', help='Use a predefined Interface (%s)' % interface_names)
parser.add_argument('-S', '--baud', help='Set the CAN Bus Speed (%s)' % (baud_nums))
ifo = parser.parse_args()
interface = j1939stack.J1939Interface
if ifo.interface:
ifacename = ifo.interface + "Interface"
interface = globals().get(ifacename)
if interface == None:
raise Exception("Invalid interface: %s. Must use one of the following: %s" % (ifo.interface, interface_names))
baud_val = CAN_250KBPS
if ifo.baud:
baud_val = globals().get("CAN_%sBPS" % ifo.baud)
if baud_val == None:
raise Exception("Invalid baud: %s. Must use one of the following: %s" % (ifo.baud, baud_nums))
results = interactive(ifo.port, intro=intro, InterfaceClass=interface, load_filename=ifo.filename, can_baud=baud_val)
if results == -1:
print("Error. Try '-h' from CLI for help.")