Skip to content

Commit 216447e

Browse files
added finally block to close connection in examples
1 parent 6eddd82 commit 216447e

11 files changed

Lines changed: 562 additions & 503 deletions

‎examples/dyscom/example_dyscom_fastplotlib.py‎

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎examples/dyscom/example_dyscom_get.py‎

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,62 @@ async def main() -> int:
1313

1414
# get comport from command line argument
1515
com_port = ExampleUtils.get_comport_from_commandline_argument()
16-
# create serial port connection
17-
connection = SerialPortConnection(com_port)
18-
# open connection, now we can read and write data
19-
connection.open()
20-
21-
# create science mode device
22-
device = DeviceI24(connection)
23-
# call initialize to get basic information (serial, versions) and stop any active stimulation/measurement
24-
# to have a defined state
25-
await device.initialize()
26-
27-
# get general layer to access general commands
28-
general = device.get_layer_general()
29-
# get dyscom layer to access dyscom commands
30-
dyscom = device.get_layer_dyscom()
31-
32-
#####
33-
# dyscom layer provides a get command for device id, it is redundant to general layer device id
34-
# both values should be identical
35-
device_id = await dyscom.get_device_id()
36-
print(f"Device id from general layer: {general.device_id}, from dyscom layer: {device_id}")
37-
38-
#####
39-
# dyscom layer provides a get command for firmware version, it is redundant to general layer firmware verion
40-
# both values should be identical
41-
firmware_version = await dyscom.get_firmware_version()
42-
print(f"Firmware version from general layer: {general.firmware_version}, from dyscom layer: {firmware_version}")
43-
44-
#####
45-
# get command for file system status
46-
file_system_status = await dyscom.get_file_system_status()
47-
print(file_system_status)
48-
49-
# this is the default calibration filename
50-
calibration_filename = f"rehaingest_{device_id}.cal"
51-
####
52-
# get calibration file content
53-
calibration_content = await dyscom.get_file_content(calibration_filename)
54-
print(f"Calibration content length: {len(calibration_content)}")
55-
# I24 devices calculates checksum from first 2 bytes of the file content
56-
calculated_checksum = (calibration_content[0] << 8) | calibration_content[1]
57-
print(f"Calculated calibration content checksum: {calculated_checksum}")
58-
59-
####
60-
# get file info for calibration file
61-
file_info = await dyscom.get_file_info(calibration_filename)
62-
print(f"Calibration file info checksum: {file_info.checksum}")
63-
64-
# close serial port connection
65-
connection.close()
16+
17+
connection = None
18+
try:
19+
# create serial port connection
20+
connection = SerialPortConnection(com_port)
21+
# open connection, now we can read and write data
22+
connection.open()
23+
24+
# create science mode device
25+
device = DeviceI24(connection)
26+
# call initialize to get basic information (serial, versions) and stop any active stimulation/measurement
27+
# to have a defined state
28+
await device.initialize()
29+
30+
# get general layer to access general commands
31+
general = device.get_layer_general()
32+
# get dyscom layer to access dyscom commands
33+
dyscom = device.get_layer_dyscom()
34+
35+
#####
36+
# dyscom layer provides a get command for device id, it is redundant to general layer device id
37+
# both values should be identical
38+
device_id = await dyscom.get_device_id()
39+
print(f"Device id from general layer: {general.device_id}, from dyscom layer: {device_id}")
40+
41+
#####
42+
# dyscom layer provides a get command for firmware version, it is redundant to general layer firmware verion
43+
# both values should be identical
44+
firmware_version = await dyscom.get_firmware_version()
45+
print(f"Firmware version from general layer: {general.firmware_version}, from dyscom layer: {firmware_version}")
46+
47+
#####
48+
# get command for file system status
49+
file_system_status = await dyscom.get_file_system_status()
50+
print(file_system_status)
51+
52+
# this is the default calibration filename
53+
calibration_filename = f"rehaingest_{device_id}.cal"
54+
####
55+
# get calibration file content
56+
calibration_content = await dyscom.get_file_content(calibration_filename)
57+
print(f"Calibration content length: {len(calibration_content)}")
58+
# I24 devices calculates checksum from first 2 bytes of the file content
59+
calculated_checksum = (calibration_content[0] << 8) | calibration_content[1]
60+
print(f"Calculated calibration content checksum: {calculated_checksum}")
61+
62+
####
63+
# get file info for calibration file
64+
file_info = await dyscom.get_file_info(calibration_filename)
65+
print(f"Calibration file info checksum: {file_info.checksum}")
66+
finally:
67+
# always close the serial port connection, even if an exception occurred above,
68+
# otherwise the COM port stays locked for subsequent runs
69+
if connection is not None:
70+
connection.close()
71+
6672
return 0
6773

6874

0 commit comments

Comments
 (0)