Skip to content

Commit e7e127a

Browse files
authored
Merge pull request hardbyte#1435 from piotrszleg/readme-with
Add with statement to example in README.md
2 parents 8816061 + 71ba9dd commit e7e127a

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

README.rst

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,24 @@ Example usage
9191
# import the library
9292
import can
9393
94-
# create a bus instance
94+
# create a bus instance using 'with' statement,
95+
# this will cause bus.shutdown() to be called on the block exit;
9596
# many other interfaces are supported as well (see documentation)
96-
bus = can.Bus(interface='socketcan',
97+
with can.Bus(interface='socketcan',
9798
channel='vcan0',
98-
receive_own_messages=True)
99+
receive_own_messages=True) as bus:
99100
100-
# send a message
101-
message = can.Message(arbitration_id=123, is_extended_id=True,
102-
data=[0x11, 0x22, 0x33])
103-
bus.send(message, timeout=0.2)
101+
# send a message
102+
message = can.Message(arbitration_id=123, is_extended_id=True,
103+
data=[0x11, 0x22, 0x33])
104+
bus.send(message, timeout=0.2)
104105
105-
# iterate over received messages
106-
for msg in bus:
107-
print(f"{msg.arbitration_id:X}: {msg.data}")
106+
# iterate over received messages
107+
for msg in bus:
108+
print(f"{msg.arbitration_id:X}: {msg.data}")
108109
109-
# or use an asynchronous notifier
110-
notifier = can.Notifier(bus, [can.Logger("recorded.log"), can.Printer()])
110+
# or use an asynchronous notifier
111+
notifier = can.Notifier(bus, [can.Logger("recorded.log"), can.Printer()])
111112
112113
You can find more information in the documentation, online at
113114
`python-can.readthedocs.org <https://python-can.readthedocs.org/en/stable/>`__.

0 commit comments

Comments
 (0)