@@ -34,82 +34,87 @@ async def device_communication() -> int:
3434
3535 # get comport from command line argument
3636 com_port = ExampleUtils .get_comport_from_commandline_argument ()
37- # create serial port connection
38- connection = SerialPortConnection (com_port )
39- # open connection, now we can read and write data
40- connection .open ()
41-
42- # create science mode device
43- device = DeviceI24 (connection )
44- # call initialize to get basic information (serial, versions) and stop any active stimulation/measurement
45- # to have a defined state
46- await device .initialize ()
47-
48- # get dyscom layer to call dyscom level commands
49- dyscom = device .get_layer_dyscom ()
50-
51- # call enable measurement power module for measurement
52- await dyscom .power_module (DyscomPowerModuleType .MEASUREMENT , DyscomPowerModulePowerType .SWITCH_ON )
53- # call init with lowest sample rate and enable signal types
54- init_params = DyscomInitParams ()
55- init_params .signal_type = [DyscomSignalType .BI , DyscomSignalType .EMG_1 ,\
56- DyscomSignalType .EMG_2 , DyscomSignalType .BREATHING ]
57- init_params .register_map_ads129x .config_register_1 .output_data_rate = Ads129xOutputDataRate .HR_MODE_500_SPS__LP_MODE_250_SPS
58- init_params .register_map_ads129x .config_register_1 .power_mode = Ads129xPowerMode .LOW_POWER
59- await dyscom .init (init_params )
60-
61- # start dyscom measurement
62- await dyscom .start ()
63-
64- # loop for some time
65- for x in range (5000 ):
66- # check if we closed window
67- if not is_window_open :
68- break
69-
70- # check operation mode from time to time, this function is not waiting for response
71- # so we have to handle it by ourself later
72- if x % 500 == 0 :
73- dyscom .send_get_operation_mode ()
74-
75- live_data_counter = 0
76- while True :
77- # process all available packages
78- ack = dyscom .packet_buffer .get_packet_from_buffer (live_data_counter == 0 )
79- if ack :
80- # because there are multiple get commands, we need to additionally check kind,
81- # which is always associated DyscomGetType
82- if ack .command == Commands .DL_GET_ACK and ack .kind == DyscomGetType .OPERATION_MODE :
83- om_ack : PacketDyscomGetAckOperationMode = ack
84- print (f"Operation mode { om_ack .operation_mode .name } " )
85- # check if measurement is still active
86- if om_ack .result_error != ResultAndError .NO_ERROR :
87- break
88- elif ack .command == Commands .DL_SEND_LIVE_DATA :
89- live_data_counter += 1
90-
91- sld : PacketDyscomSendLiveData = ack
92- if sld .status_error :
93- print (f"SendLiveData status error { sld .samples } " )
94- break
95-
96- for x in range (4 ):
97- plot_helper .append_value (x , sld .samples [x ].value )
98-
99- plot_helper .update ()
100- else :
101- # print(f"Live data acknowledges per iteration {live_data_counter}")
102- break
103-
104- await asyncio .sleep (0.001 )
10537
106- # stop measurement
107- await dyscom .stop ()
108- # turn power module off
109- await dyscom .power_module (DyscomPowerModuleType .MEASUREMENT , DyscomPowerModulePowerType .SWITCH_OFF )
38+ connection = None
39+ try : # pylint:disable=too-many-nested-blocks
40+ # create serial port connection
41+ connection = SerialPortConnection (com_port )
42+ # open connection, now we can read and write data
43+ connection .open ()
44+
45+ # create science mode device
46+ device = DeviceI24 (connection )
47+ # call initialize to get basic information (serial, versions) and stop any active stimulation/measurement
48+ # to have a defined state
49+ await device .initialize ()
50+
51+ # get dyscom layer to call dyscom level commands
52+ dyscom = device .get_layer_dyscom ()
53+
54+ # call enable measurement power module for measurement
55+ await dyscom .power_module (DyscomPowerModuleType .MEASUREMENT , DyscomPowerModulePowerType .SWITCH_ON )
56+ # call init with lowest sample rate and enable signal types
57+ init_params = DyscomInitParams ()
58+ init_params .signal_type = [DyscomSignalType .BI , DyscomSignalType .EMG_1 ,\
59+ DyscomSignalType .EMG_2 , DyscomSignalType .BREATHING ]
60+ init_params .register_map_ads129x .config_register_1 .output_data_rate = Ads129xOutputDataRate .HR_MODE_500_SPS__LP_MODE_250_SPS
61+ init_params .register_map_ads129x .config_register_1 .power_mode = Ads129xPowerMode .LOW_POWER
62+ await dyscom .init (init_params )
63+
64+ # start dyscom measurement
65+ await dyscom .start ()
66+
67+ # loop for some time
68+ for x in range (5000 ):
69+ # check if we closed window
70+ if not is_window_open :
71+ break
11072
111- # close serial port connection
112- connection .close ()
73+ # check operation mode from time to time, this function is not waiting for response
74+ # so we have to handle it by ourself later
75+ if x % 500 == 0 :
76+ dyscom .send_get_operation_mode ()
77+
78+ live_data_counter = 0
79+ while True :
80+ # process all available packages
81+ ack = dyscom .packet_buffer .get_packet_from_buffer (live_data_counter == 0 )
82+ if ack :
83+ # because there are multiple get commands, we need to additionally check kind,
84+ # which is always associated DyscomGetType
85+ if ack .command == Commands .DL_GET_ACK and ack .kind == DyscomGetType .OPERATION_MODE :
86+ om_ack : PacketDyscomGetAckOperationMode = ack
87+ print (f"Operation mode { om_ack .operation_mode .name } " )
88+ # check if measurement is still active
89+ if om_ack .result_error != ResultAndError .NO_ERROR :
90+ break
91+ elif ack .command == Commands .DL_SEND_LIVE_DATA :
92+ live_data_counter += 1
93+
94+ sld : PacketDyscomSendLiveData = ack
95+ if sld .status_error :
96+ print (f"SendLiveData status error { sld .samples } " )
97+ break
98+
99+ for x in range (4 ):
100+ plot_helper .append_value (x , sld .samples [x ].value )
101+
102+ plot_helper .update ()
103+ else :
104+ # print(f"Live data acknowledges per iteration {live_data_counter}")
105+ break
106+
107+ await asyncio .sleep (0.001 )
108+
109+ # stop measurement
110+ await dyscom .stop ()
111+ # turn power module off
112+ await dyscom .power_module (DyscomPowerModuleType .MEASUREMENT , DyscomPowerModulePowerType .SWITCH_OFF )
113+ finally :
114+ # always close the serial port connection, even if an exception occurred above,
115+ # otherwise the COM port stays locked for subsequent runs
116+ if connection is not None :
117+ connection .close ()
113118
114119 print ("Close plot window to end example" )
115120
0 commit comments