Skip to content

Commit 0809d06

Browse files
committed
move the main script functions
hardbyte#162
1 parent 0bba212 commit 0809d06

File tree

6 files changed

+241
-248
lines changed

6 files changed

+241
-248
lines changed

can/interfaces/remote/__main__.py

Lines changed: 0 additions & 64 deletions
This file was deleted.

can/io/logger.py

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
#!/usr/bin/env python
2-
"""
3-
logger.py logs CAN traffic to the terminal and to a file on disk.
4-
5-
logger.py can0
6-
7-
See candump in the can-utils package for a C implementation.
8-
Efficient filtering has been implemented for the socketcan backend.
9-
For example the command
10-
11-
logger.py can0 F03000:FFF000
12-
13-
Will filter for can frames with a can_id containing XXF03XXX.
14-
15-
Dynamic Controls 2010
16-
"""
17-
from __future__ import print_function
18-
import datetime
19-
import argparse
20-
import time
21-
import socket
22-
23-
import can
24-
251
from .asc import ASCWriter
262
from .blf import BLFWriter
273
from .csv import CSVWriter
@@ -59,75 +35,3 @@ def __new__(cls, other, filename):
5935
return SqliteWriter(filename)
6036
else:
6137
return Printer(filename)
62-
63-
64-
def main():
65-
parser = argparse.ArgumentParser(description="Log CAN traffic, printing messages to stdout or to a given file")
66-
67-
parser.add_argument("-f", "--file_name", dest="log_file",
68-
help="""Path and base log filename, extension can be .txt, .asc, .csv, .db, .npz""",
69-
default=None)
70-
71-
parser.add_argument("-v", action="count", dest="verbosity",
72-
help='''How much information do you want to see at the command line?
73-
You can add several of these e.g., -vv is DEBUG''', default=2)
74-
75-
parser.add_argument('-c', '--channel', help='''Most backend interfaces require some sort of channel.
76-
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
77-
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''')
78-
79-
parser.add_argument('-i', '--interface', dest="interface",
80-
help='''Specify the backend CAN interface to use. If left blank,
81-
fall back to reading from configuration files.''',
82-
choices=can.VALID_INTERFACES)
83-
84-
parser.add_argument('--filter', help='''Comma separated filters can be specified for the given CAN interface:
85-
<can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)
86-
<can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)
87-
''', nargs=argparse.REMAINDER, default='')
88-
89-
parser.add_argument('-b', '--bitrate', type=int,
90-
help='''Bitrate to use for the CAN bus.''')
91-
92-
results = parser.parse_args()
93-
94-
verbosity = results.verbosity
95-
96-
logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
97-
can.set_logging_level(logging_level_name)
98-
99-
can_filters = []
100-
if len(results.filter) > 0:
101-
print('Adding filter/s', results.filter)
102-
for filt in results.filter:
103-
if ':' in filt:
104-
_ = filt.split(":")
105-
can_id, can_mask = int(_[0], base=16), int(_[1], base=16)
106-
elif "~" in filt:
107-
can_id, can_mask = filt.split("~")
108-
can_id = int(can_id, base=16) | 0x20000000 # CAN_INV_FILTER
109-
can_mask = int(can_mask, base=16) & socket.CAN_ERR_FLAG
110-
can_filters.append({"can_id": can_id, "can_mask": can_mask})
111-
112-
config = {"can_filters": can_filters}
113-
if results.interface:
114-
config["bustype"] = results.interface
115-
if results.bitrate:
116-
config["bitrate"] = results.bitrate
117-
bus = can.interface.Bus(results.channel, **config)
118-
print('Can Logger (Started on {})\n'.format(datetime.datetime.now()))
119-
logger = Logger(results.log_file)
120-
121-
try:
122-
while True:
123-
msg = bus.recv(1)
124-
if msg is not None:
125-
logger(msg)
126-
except KeyboardInterrupt:
127-
pass
128-
finally:
129-
bus.shutdown()
130-
logger.stop()
131-
132-
if __name__ == "__main__":
133-
main()

can/io/player.py

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
#!/usr/bin/env python
2-
"""
3-
player.py replays CAN traffic saved with logger.py back
4-
to a CAN bus.
5-
6-
Similar to canplayer in the can-utils package.
7-
"""
81
from __future__ import print_function
9-
import datetime
102
import time
11-
import argparse
123
import logging
134

14-
import can
155
from .blf import BLFReader
166
from .sqlite import SqlReader
177

@@ -84,75 +74,3 @@ def __iter__(self):
8474

8575
time.sleep(sleep_period)
8676
yield m
87-
88-
89-
def main():
90-
parser = argparse.ArgumentParser(description="Replay CAN traffic")
91-
92-
parser.add_argument("-f", "--file_name", dest="log_file",
93-
help="""Path and base log filename, extension can be .txt, .asc, .csv, .db, .npz""",
94-
default=None)
95-
96-
parser.add_argument("-v", action="count", dest="verbosity",
97-
help='''Also print can frames to stdout.
98-
You can add several of these to enable debugging''', default=2)
99-
100-
parser.add_argument('-c', '--channel',
101-
help='''Most backend interfaces require some sort of channel.
102-
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
103-
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''')
104-
105-
parser.add_argument('-i', '--interface', dest="interface",
106-
help='''Specify the backend CAN interface to use. If left blank,
107-
fall back to reading from configuration files.''',
108-
choices=can.VALID_INTERFACES)
109-
110-
parser.add_argument('-b', '--bitrate', type=int,
111-
help='''Bitrate to use for the CAN bus.''')
112-
113-
parser.add_argument('--ignore-timestamps', dest='timestamps',
114-
help='''Ignore timestamps (send all frames immediately with minimum gap between
115-
frames)''', action='store_false')
116-
117-
parser.add_argument('-g', '--gap', type=float, help='''<ms> minimum time between replayed frames''')
118-
parser.add_argument('-s', '--skip', type=float, default=60*60*24,
119-
help='''<s> skip gaps greater than 's' seconds''')
120-
121-
parser.add_argument('infile', metavar='input-file', type=str,
122-
help='The file to replay. Supported types: .db, .blf')
123-
124-
results = parser.parse_args()
125-
126-
verbosity = results.verbosity
127-
gap = 0.0001 if results.gap is None else results.gap
128-
129-
logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
130-
can.set_logging_level(logging_level_name)
131-
132-
config = {}
133-
if results.interface:
134-
config["bustype"] = results.interface
135-
if results.bitrate:
136-
config["bitrate"] = results.bitrate
137-
bus = can.interface.Bus(results.channel, **config)
138-
139-
player = LogReader(results.infile)
140-
141-
in_sync = MessageSync(player, timestamps=True, skip=results.skip)
142-
143-
print('Can LogReader (Started on {})'.format(
144-
datetime.datetime.now()))
145-
146-
try:
147-
for m in in_sync:
148-
if verbosity >= 3:
149-
print(m)
150-
bus.send(m)
151-
except KeyboardInterrupt:
152-
pass
153-
finally:
154-
bus.shutdown()
155-
156-
157-
if __name__ == "__main__":
158-
main()

can/logger.py

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,95 @@
1-
from can.io import logger
1+
#!/usr/bin/env python
2+
"""
3+
logger.py logs CAN traffic to the terminal and to a file on disk.
24
3-
logger.main()
5+
logger.py can0
6+
7+
See candump in the can-utils package for a C implementation.
8+
Efficient filtering has been implemented for the socketcan backend.
9+
For example the command
10+
11+
logger.py can0 F03000:FFF000
12+
13+
Will filter for can frames with a can_id containing XXF03XXX.
14+
15+
Dynamic Controls 2010
16+
"""
17+
from __future__ import print_function
18+
import datetime
19+
import argparse
20+
import socket
21+
22+
import can
23+
from can.io.logger import Logger
24+
25+
26+
def main():
27+
parser = argparse.ArgumentParser(description="Log CAN traffic, printing messages to stdout or to a given file")
28+
29+
parser.add_argument("-f", "--file_name", dest="log_file",
30+
help="""Path and base log filename, extension can be .txt, .asc, .csv, .db, .npz""",
31+
default=None)
32+
33+
parser.add_argument("-v", action="count", dest="verbosity",
34+
help='''How much information do you want to see at the command line?
35+
You can add several of these e.g., -vv is DEBUG''', default=2)
36+
37+
parser.add_argument('-c', '--channel', help='''Most backend interfaces require some sort of channel.
38+
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
39+
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''')
40+
41+
parser.add_argument('-i', '--interface', dest="interface",
42+
help='''Specify the backend CAN interface to use. If left blank,
43+
fall back to reading from configuration files.''',
44+
choices=can.VALID_INTERFACES)
45+
46+
parser.add_argument('--filter', help='''Comma separated filters can be specified for the given CAN interface:
47+
<can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)
48+
<can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)
49+
''', nargs=argparse.REMAINDER, default='')
50+
51+
parser.add_argument('-b', '--bitrate', type=int,
52+
help='''Bitrate to use for the CAN bus.''')
53+
54+
results = parser.parse_args()
55+
56+
verbosity = results.verbosity
57+
58+
logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
59+
can.set_logging_level(logging_level_name)
60+
61+
can_filters = []
62+
if len(results.filter) > 0:
63+
print('Adding filter/s', results.filter)
64+
for filt in results.filter:
65+
if ':' in filt:
66+
_ = filt.split(":")
67+
can_id, can_mask = int(_[0], base=16), int(_[1], base=16)
68+
elif "~" in filt:
69+
can_id, can_mask = filt.split("~")
70+
can_id = int(can_id, base=16) | 0x20000000 # CAN_INV_FILTER
71+
can_mask = int(can_mask, base=16) & socket.CAN_ERR_FLAG
72+
can_filters.append({"can_id": can_id, "can_mask": can_mask})
73+
74+
config = {"can_filters": can_filters}
75+
if results.interface:
76+
config["bustype"] = results.interface
77+
if results.bitrate:
78+
config["bitrate"] = results.bitrate
79+
bus = can.interface.Bus(results.channel, **config)
80+
print('Can Logger (Started on {})\n'.format(datetime.datetime.now()))
81+
logger = Logger(results.log_file)
82+
83+
try:
84+
while True:
85+
msg = bus.recv(1)
86+
if msg is not None:
87+
logger(msg)
88+
except KeyboardInterrupt:
89+
pass
90+
finally:
91+
bus.shutdown()
92+
logger.stop()
93+
94+
if __name__ == "__main__":
95+
main()

0 commit comments

Comments
 (0)