@@ -58,13 +58,14 @@ def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector):
5858 print ("Press 1 or 2 to stimulate green or yellow connector" )
5959 print ("Press + or - to increase or decrease current" )
6060 print ("Press q to quit" )
61- # create keyboard input thread for non blocking console input
62- keyboard_input_thread = KeyboardInputThread (input_callback )
6361
6462 # get comport from command line argument
6563 com_port = ExampleUtils .get_comport_from_commandline_argument ()
6664
6765 connection = None
66+ # created only once low_level_layer is assigned below, because input_callback
67+ # references it and a key press before that would raise an exception
68+ keyboard_input_thread = None
6869 try :
6970 # create serial port connection
7071 connection = SerialPortConnection (com_port )
@@ -83,6 +84,10 @@ def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector):
8384 # call init low level
8485 await low_level_layer .init (LowLevelMode .NO_MEASUREMENT , LowLevelHighVoltageSource .STANDARD )
8586
87+ # only create the keyboard input thread once low_level_layer is assigned, since
88+ # input_callback references it and a key press before that would raise an exception
89+ keyboard_input_thread = KeyboardInputThread (input_callback )
90+
8691 # now we can start stimulation
8792 while keyboard_input_thread .is_alive ():
8893 # get new packets from connection
@@ -102,7 +107,8 @@ def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector):
102107 finally :
103108 # request the keyboard thread to stop even on an exception above, so it
104109 # doesn't keep waiting for input that no longer matters
105- keyboard_input_thread .stop ()
110+ if keyboard_input_thread is not None :
111+ keyboard_input_thread .stop ()
106112 # always close the serial port connection, even if an exception occurred above,
107113 # otherwise the COM port stays locked for subsequent runs
108114 if connection is not None :
0 commit comments