File: socketcan.py

package info (click to toggle)
pyroute2 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,700 kB
  • sloc: python: 50,245; makefile: 280; javascript: 183; ansic: 81; sh: 44; awk: 17
file content (21 lines) | stat: -rw-r--r-- 614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'''
Simplest example to set CAN bitrate.
'''

from pyroute2 import IPRoute

with IPRoute() as ip_route:
    # loolkup can0 interface
    idx = ip_route.link_lookup(ifname='can0')[0]
    link = ip_route.link('get', index=idx)

    # bring can0 interface down. CAN settings can be set only
    # if the interface is down
    if 'state' in link[0] and link[0]['state'] == 'up':
        ip_route.link('set', index=idx, state='down')

    # set CAN birate
    ip_route.link('set', index=idx, kind='can', can_bittiming={'bitrate': 250000 })

    # bring can0 interface up
    ip_route.link('set', index=idx, state='up')