Skip to content

Commit 2dafa99

Browse files
author
Andrew Beal
committed
Change default bitrate to 500K.Fix bus to handle proper channel pass in. Add interface option to logger tool.
0 parents  commit 2dafa99

63 files changed

Lines changed: 6057 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.hgflow

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[branchname]
2+
master = default
3+
develop = develop
4+
feature = feature/
5+
release = release/
6+
hotfix = hotfix/
7+
support = support/
8+

.hgignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax: glob
2+
build/
3+
dist/
4+
.idea/
5+
pycanlib.egg-info/
6+
version.txt
7+
*.pyc

CONTRIBUTORS.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Ben Powell
2+
Brian Thorne <bthorne@dynamiccontrols.com>
3+
Geert Linders <glinders@dynamiccontrols.com>
4+
Mark Catley <mcatley@dynamiccontrols.com>
5+
Phillip Dixon <pdixon@dynamiccontrols.com>
6+
Rose Lu <rlu@dynamiccontrols.com>
7+
Albert Bloomfield <albert.bloomfield@ampeletricvehicles.com>

LICENSE.txt

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
GNU LESSER GENERAL PUBLIC LICENSE
2+
Version 3, 29 June 2007
3+
4+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5+
Everyone is permitted to copy and distribute verbatim copies
6+
of this license document, but changing it is not allowed.
7+
8+
9+
This version of the GNU Lesser General Public License incorporates
10+
the terms and conditions of version 3 of the GNU General Public
11+
License, supplemented by the additional permissions listed below.
12+
13+
0. Additional Definitions.
14+
15+
As used herein, "this License" refers to version 3 of the GNU Lesser
16+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
17+
General Public License.
18+
19+
"The Library" refers to a covered work governed by this License,
20+
other than an Application or a Combined Work as defined below.
21+
22+
An "Application" is any work that makes use of an interface provided
23+
by the Library, but which is not otherwise based on the Library.
24+
Defining a subclass of a class defined by the Library is deemed a mode
25+
of using an interface provided by the Library.
26+
27+
A "Combined Work" is a work produced by combining or linking an
28+
Application with the Library. The particular version of the Library
29+
with which the Combined Work was made is also called the "Linked
30+
Version".
31+
32+
The "Minimal Corresponding Source" for a Combined Work means the
33+
Corresponding Source for the Combined Work, excluding any source code
34+
for portions of the Combined Work that, considered in isolation, are
35+
based on the Application, and not on the Linked Version.
36+
37+
The "Corresponding Application Code" for a Combined Work means the
38+
object code and/or source code for the Application, including any data
39+
and utility programs needed for reproducing the Combined Work from the
40+
Application, but excluding the System Libraries of the Combined Work.
41+
42+
1. Exception to Section 3 of the GNU GPL.
43+
44+
You may convey a covered work under sections 3 and 4 of this License
45+
without being bound by section 3 of the GNU GPL.
46+
47+
2. Conveying Modified Versions.
48+
49+
If you modify a copy of the Library, and, in your modifications, a
50+
facility refers to a function or data to be supplied by an Application
51+
that uses the facility (other than as an argument passed when the
52+
facility is invoked), then you may convey a copy of the modified
53+
version:
54+
55+
a) under this License, provided that you make a good faith effort to
56+
ensure that, in the event an Application does not supply the
57+
function or data, the facility still operates, and performs
58+
whatever part of its purpose remains meaningful, or
59+
60+
b) under the GNU GPL, with none of the additional permissions of
61+
this License applicable to that copy.
62+
63+
3. Object Code Incorporating Material from Library Header Files.
64+
65+
The object code form of an Application may incorporate material from
66+
a header file that is part of the Library. You may convey such object
67+
code under terms of your choice, provided that, if the incorporated
68+
material is not limited to numerical parameters, data structure
69+
layouts and accessors, or small macros, inline functions and templates
70+
(ten or fewer lines in length), you do both of the following:
71+
72+
a) Give prominent notice with each copy of the object code that the
73+
Library is used in it and that the Library and its use are
74+
covered by this License.
75+
76+
b) Accompany the object code with a copy of the GNU GPL and this license
77+
document.
78+
79+
4. Combined Works.
80+
81+
You may convey a Combined Work under terms of your choice that,
82+
taken together, effectively do not restrict modification of the
83+
portions of the Library contained in the Combined Work and reverse
84+
engineering for debugging such modifications, if you also do each of
85+
the following:
86+
87+
a) Give prominent notice with each copy of the Combined Work that
88+
the Library is used in it and that the Library and its use are
89+
covered by this License.
90+
91+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
92+
document.
93+
94+
c) For a Combined Work that displays copyright notices during
95+
execution, include the copyright notice for the Library among
96+
these notices, as well as a reference directing the user to the
97+
copies of the GNU GPL and this license document.
98+
99+
d) Do one of the following:
100+
101+
0) Convey the Minimal Corresponding Source under the terms of this
102+
License, and the Corresponding Application Code in a form
103+
suitable for, and under terms that permit, the user to
104+
recombine or relink the Application with a modified version of
105+
the Linked Version to produce a modified Combined Work, in the
106+
manner specified by section 6 of the GNU GPL for conveying
107+
Corresponding Source.
108+
109+
1) Use a suitable shared library mechanism for linking with the
110+
Library. A suitable mechanism is one that (a) uses at run time
111+
a copy of the Library already present on the user's computer
112+
system, and (b) will operate properly with a modified version
113+
of the Library that is interface-compatible with the Linked
114+
Version.
115+
116+
e) Provide Installation Information, but only if you would otherwise
117+
be required to provide such information under section 6 of the
118+
GNU GPL, and only to the extent that such information is
119+
necessary to install and execute a modified version of the
120+
Combined Work produced by recombining or relinking the
121+
Application with a modified version of the Linked Version. (If
122+
you use option 4d0, the Installation Information must accompany
123+
the Minimal Corresponding Source and Corresponding Application
124+
Code. If you use option 4d1, you must provide the Installation
125+
Information in the manner specified by section 6 of the GNU GPL
126+
for conveying Corresponding Source.)
127+
128+
5. Combined Libraries.
129+
130+
You may place library facilities that are a work based on the
131+
Library side by side in a single library together with other library
132+
facilities that are not Applications and are not covered by this
133+
License, and convey such a combined library under terms of your
134+
choice, if you do both of the following:
135+
136+
a) Accompany the combined library with a copy of the same work based
137+
on the Library, uncombined with any other library facilities,
138+
conveyed under the terms of this License.
139+
140+
b) Give prominent notice with the combined library that part of it
141+
is a work based on the Library, and explaining where to find the
142+
accompanying uncombined form of the same work.
143+
144+
6. Revised Versions of the GNU Lesser General Public License.
145+
146+
The Free Software Foundation may publish revised and/or new versions
147+
of the GNU Lesser General Public License from time to time. Such new
148+
versions will be similar in spirit to the present version, but may
149+
differ in detail to address new problems or concerns.
150+
151+
Each version is given a distinguishing version number. If the
152+
Library as you received it specifies that a certain numbered version
153+
of the GNU Lesser General Public License "or any later version"
154+
applies to it, you have the option of following the terms and
155+
conditions either of that published version or of any later version
156+
published by the Free Software Foundation. If the Library as you
157+
received it does not specify a version number of the GNU Lesser
158+
General Public License, you may choose any version of the GNU Lesser
159+
General Public License ever published by the Free Software Foundation.
160+
161+
If the Library as you received it specifies that a proxy can decide
162+
whether future versions of the GNU Lesser General Public License shall
163+
apply, that proxy's public statement of acceptance of any version is
164+
permanent authorization for you to choose that version for the
165+
Library.

README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
The **C**ontroller **A**rea **N**etwork is a bus standard designed to
2+
allow microcontrollers and devices to communicate with each other. It
3+
has priority based bus arbitration, reliable deterministic
4+
communication. It is used in cars, trucks, wheelchairs and more. See
5+
[wikipedia][1] for more info.
6+
7+
# python-can
8+
9+
This module provides controller area network support for [Python][4].
10+
11+
## Configuration File
12+
13+
In order to use this library a CAN interface needs to be specified.
14+
A method to do this is to create a configuration file called `can.conf`,
15+
`~/.canrc` or `can.ini`.
16+
17+
The configuration file sets the default interface and channel:
18+
19+
[default]
20+
interface = <the name of the interface to use>
21+
channel = <the channel to use by default>
22+
23+
24+
## Interfaces
25+
26+
The interface available are:
27+
28+
### kvaser
29+
30+
[Kvaser][2]'s CANLib SDK for Windows (also available on Linux)
31+
32+
### socketcan
33+
34+
On linux the socketcan interface is exposed via either:
35+
36+
- socketcan_ctypes
37+
- socketcan_native
38+
39+
### serial
40+
41+
A text based interface. For example use over bluetooth with `/dev/rfcomm0`
42+
43+
### pcan
44+
45+
[Peak-System][8]'s PCAN-Basic API.
46+
47+
48+
## Installation
49+
50+
## GNU/Linux dependencies
51+
52+
Reasonably modern Linux Kernels (2.6.25 or newer) have an implementation of
53+
``socketcan``. This version of python-can will directly use socketcan
54+
if called with Python 3.3 or greater, otherwise that interface is
55+
used via ctypes.
56+
57+
## Windows dependencies
58+
59+
### Kvaser
60+
61+
To install `python-can` using the Kvaser CANLib SDK as the backend:
62+
63+
1. Install the [latest stable release of Python][4].
64+
65+
2. Install [Kvaser's latest Windows CANLib drivers][5].
66+
67+
3. Test that Kvaser's own tools work to ensure the driver is properly
68+
installed and that the hardware is working.
69+
70+
71+
### PCAN
72+
73+
To use the PCAN-Basic API as the backend (which has only been tested with
74+
Python 2.7):
75+
76+
1. Download the latest version of the [PCAN-Basic API][9].
77+
78+
2. Extract `PCANBasic.dll` from the Win32 subfolder of the archive or the x64
79+
subfolder depending on whether you have a 32-bit or 64-bit installation of
80+
Python.
81+
82+
3. Copy `PCANBasic.dll` into the working directory where you will be running
83+
your python script. There is probably a way to install the dll properly,
84+
but I'm not certain how to do that.
85+
86+
Note that PCANBasic API timestamps count seconds from system startup. To
87+
convert these to epoch times, the uptime library is used. If it is not
88+
available, the times are returned as number of seconds from system startup.
89+
To install the uptime library, run `pip install uptime`.
90+
91+
92+
## Install python-can
93+
94+
You may need to install [pip][7] and [setuptools][10] first.
95+
96+
Two options, install normally with:
97+
98+
python setup.py install
99+
100+
Or to do a "development" install of this package to your machine (this allows
101+
you to make changes locally or pull updates from the Mercurial repository and
102+
use them without having to reinstall):
103+
104+
python setup.py develop
105+
106+
On linux you will need `sudo` rights.
107+
108+
109+
## Documentation
110+
111+
The documentation for python-can has been generated with Sphinx they can be viewed online at
112+
[python-can.readthedocs.org][6]
113+
114+
115+
### Generation
116+
117+
With sphinx installed the documentation can be generated locally with:
118+
119+
python setup.py build_sphinx
120+
121+
122+
[1]: http://en.wikipedia.org/wiki/CAN_bus
123+
[2]: http://www.kvaser.com
124+
[3]: http://www.brownhat.org/docs/socketcan/llcf-api.html
125+
[4]: http://python.org/download/
126+
[5]: http://www.kvaser.com/en/downloads.html
127+
[6]: https://python-can.readthedocs.org/en/latest/
128+
[7]: http://www.pip-installer.org/en/latest/installing.html
129+
[8]: http://www.peak-system.com/
130+
[9]: http://www.peak-system.com/Downloads.76.0.html?
131+
[10]: https://pypi.python.org/pypi/setuptools

bin/can_logger.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
"""
3+
can_logger.py logs CAN traffic to the terminal and to a file on disk.
4+
5+
can_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+
can_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+
22+
import can
23+
24+
if __name__ == "__main__":
25+
26+
parser = argparse.ArgumentParser(description="Log CAN traffic, printing messages to stdout or to a given file")
27+
28+
parser.add_argument("-f", "--file_name", dest="log_file",
29+
help="""Path and base log filename, extension can be .txt, .csv, .db, .npz""",
30+
default=None)
31+
32+
parser.add_argument("-v", action="count", dest="verbosity",
33+
help='''How much information do you want to see at the command line?
34+
You can add several of these e.g., -vv is DEBUG''', default=2)
35+
36+
parser.add_argument('-c', '--channel', help='''Most backend interfaces require some sort of channel.
37+
For example with the serial interface the channel might be a rfcomm device: /dev/rfcomm0
38+
Other channel examples are: can0, vcan0''', default=can.rc['channel'])
39+
40+
parser.add_argument('-i', '--interface', dest="interface",help='''Which backend do you want to use?''',default='kvaser',choices=('kvaser','socketscan','pcan','serial'))
41+
42+
parser.add_argument('--filter', help='''Comma separated filters can be specified for the given CAN interface:
43+
<can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)
44+
<can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)
45+
''', nargs=argparse.REMAINDER, default='')
46+
47+
results = parser.parse_args()
48+
49+
verbosity = results.verbosity
50+
51+
logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
52+
can.set_logging_level(logging_level_name)
53+
54+
can_filters = []
55+
if len(results.filter) > 0:
56+
print('we have filter/s', results.filter)
57+
for filt in results.filter:
58+
if ':' in filt:
59+
_ = filt.split(":")
60+
can_id, can_mask = int(_[0], base=16), int(_[1], base=16)
61+
elif "~" in filt:
62+
can_id, can_mask = filt.split("~")
63+
can_id = int(can_id, base=16) | 0x20000000 # CAN_INV_FILTER
64+
can_mask = int(can_mask, base=16) & socket.CAN_ERR_FLAG
65+
can_filters.append({"can_id": can_id, "can_mask": can_mask})
66+
67+
bus = can.interface.Bus(results.channel, bustype=results.interface, can_filters=can_filters)
68+
print('Can Logger (Started on {})\n'.format(datetime.datetime.now()))
69+
notifier = can.Notifier(bus, [can.Printer(results.log_file)])
70+
71+
try:
72+
while True:
73+
time.sleep(1)
74+
except KeyboardInterrupt:
75+
bus.shutdown()

0 commit comments

Comments
 (0)