@@ -91,23 +91,24 @@ Example usage
91
91
# import the library
92
92
import can
93
93
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;
95
96
# many other interfaces are supported as well (see documentation)
96
- bus = can.Bus(interface = ' socketcan' ,
97
+ with can.Bus(interface = ' socketcan' ,
97
98
channel = ' vcan0' ,
98
- receive_own_messages = True )
99
+ receive_own_messages = True ) as bus:
99
100
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 )
104
105
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} " )
108
109
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()])
111
112
112
113
You can find more information in the documentation, online at
113
114
`python-can.readthedocs.org <https://python-can.readthedocs.org/en/stable/ >`__.
0 commit comments