@@ -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 = [0x 11 , 0x 22 , 0x 33 ])
103- bus.send(message, timeout = 0.2 )
101+ # send a message
102+ message = can.Message(arbitration_id = 123 , is_extended_id = True ,
103+ data = [0x 11 , 0x 22 , 0x 33 ])
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