Skip to content

Commit fab6f23

Browse files
committed
general cleanup of the examples
1 parent 78e4834 commit fab6f23

6 files changed

Lines changed: 58 additions & 19 deletions

File tree

examples/cyclic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
24
"""
35
This example exercises the periodic sending capabilities.
46
@@ -8,6 +10,8 @@
810
911
"""
1012

13+
from __future__ import print_function
14+
1115
import logging
1216
import time
1317

@@ -125,5 +129,4 @@ def test_periodic_send_with_modifying_data(bus):
125129

126130
bus.shutdown()
127131

128-
129132
time.sleep(2)

examples/send_one.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
"""
5+
This example shows how sending a single message works.
6+
"""
7+
18
from __future__ import print_function
2-
import can
39

10+
import can
411

512
def send_one():
613
bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=250000)
14+
15+
# Other buses work similar:
16+
#bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=250000)
717
#bus = can.interface.Bus(bustype='ixxat', channel=0, bitrate=250000)
818
#bus = can.interface.Bus(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000)
919

1020
msg = can.Message(arbitration_id=0xc0ffee,
1121
data=[0, 25, 0, 1, 3, 1, 4, 1],
1222
extended_id=True)
23+
1324
try:
1425
bus.send(msg)
1526
print("Message sent on {}".format(bus.channel_info))
1627
except can.CanError:
1728
print("Message NOT sent")
1829

19-
if __name__ == "__main__":
30+
if __name__ == '__main__':
2031
send_one()

examples/serial_com.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
24
"""
35
This example sends every second a messages over the serial interface and also
46
receives incoming messages.
@@ -17,10 +19,13 @@
1719
com0com: http://com0com.sourceforge.net/
1820
"""
1921

22+
from __future__ import print_function
23+
2024
import time
21-
import can
2225
import threading
2326

27+
import can
28+
2429

2530
def send_cyclic(bus, msg, stop_event):
2631
print("Start to send a message every 1s")

examples/simpleLogConvert.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/usr/bin/env python
2-
# use it to convert .can-log files
3-
# usage: simpleLogConvert.py sourceLog.asc targetLog.log
2+
# coding: utf-8
3+
4+
"""
5+
Use this to convert .can log files.
6+
7+
Usage: simpleLogConvert.py sourceLog.asc targetLog.log
8+
"""
49

510
import sys
11+
612
import can.io.logger
713
import can.io.player
814

examples/vcan_filtered.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
"""
5+
This shows how message filtering works.
6+
"""
7+
18
import time
9+
210
import can
311

4-
bus = can.interface.Bus(bustype='socketcan',
5-
channel='vcan0',
6-
receive_own_messages=True)
7-
8-
can_filters = [{"can_id": 1, "can_mask": 0xf, "extended": True}]
9-
bus.set_filters(can_filters)
10-
notifier = can.Notifier(bus, [can.Printer()])
11-
bus.send(can.Message(arbitration_id=1, extended_id=True))
12-
bus.send(can.Message(arbitration_id=2, extended_id=True))
13-
bus.send(can.Message(arbitration_id=1, extended_id=False))
14-
time.sleep(10)
12+
if __name__ == '__main__':
13+
bus = can.interface.Bus(bustype='socketcan',
14+
channel='vcan0',
15+
receive_own_messages=True)
16+
17+
can_filters = [{"can_id": 1, "can_mask": 0xf, "extended": True}]
18+
bus.set_filters(can_filters)
19+
notifier = can.Notifier(bus, [can.Printer()])
20+
bus.send(can.Message(arbitration_id=1, extended_id=True))
21+
bus.send(can.Message(arbitration_id=2, extended_id=True))
22+
bus.send(can.Message(arbitration_id=1, extended_id=False))
23+
24+
time.sleep(10)

examples/virtual_can_demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
14
"""
25
This demo creates multiple processes of Producers to spam a socketcan bus.
36
"""
@@ -16,6 +19,7 @@ def producer(id):
1619
for i in range(16):
1720
msg = can.Message(arbitration_id=0x0cf02200+id, data=[id, i, 0, 1, 3, 1, 4, 1])
1821
bus.send(msg)
22+
1923
# TODO Issue #3: Need to keep running to ensure the writing threads stay alive. ?
2024
time.sleep(2)
2125

0 commit comments

Comments
 (0)