Loading...
Searching...
No Matches
async.py
1"""! @example async.py """
2import sys
3import struct
4import asyncio
5from PySide6.QtWidgets import QApplication
6from SquidstatPyLibrary import AisDeviceTracker
7from SquidstatPyLibrary import AisCompRange
8from SquidstatPyLibrary import AisDCData
9from SquidstatPyLibrary import AisACData
10from SquidstatPyLibrary import AisExperimentNode
11from SquidstatPyLibrary import AisErrorCode
12from SquidstatPyLibrary import AisExperiment
13from SquidstatPyLibrary import AisInstrumentHandler
14from SquidstatPyLibrary import AisConstantPotElement
15from SquidstatPyLibrary import AisEISPotentiostaticElement
16from SquidstatPyLibrary import AisConstantCurrentElement
17
18# initialize the application
19app = QApplication([])
20
21# Add delay before quitting
22async def delayed_quit():
23 await asyncio.sleep(5)
24 app.quit()
25
26# function to print when experiment has completed
27def experiment_complete_handler(channel, reason):
28 print(f"Experiment Completed on channel {channel} , {reason}")
29 asyncio.run(delayed_quit())
30
31
32# main function setup as async
33async def main():
34 # initialize a device tracker
36 # connect to device associated with the tracker
37 # print device serial number
38 tracker.newDeviceConnected.connect(lambda deviceName: print(f"Connected Device: {deviceName}"))
39 # connect to device on com port 4
40 error = tracker.connectToDeviceOnComPort("COM4")
41 if error.value() != AisErrorCode.Success:
42 print(error.message())
43 app.quit()
44
45 # Add initial delay before asking for hanlder (5 s)
46 await asyncio.sleep(5)
47
48 # use serial number to get handler for instrument
49 handler = tracker.getInstrumentHandler("Cycler1518")
50 # manages DC data input and output
51 # add more variables if you want to print more data to the console
52 handler.activeDCDataReady.connect(lambda channel, data: print("timestamp:", "{:.9f}".format(data.timestamp), "workingElectrodeVoltage: ", "{:.9f}".format(data.workingElectrodeVoltage)))
53 # manages AC data input and output
54 # add more variables if you want to print more data to the console
55 handler.activeACDataReady.connect(lambda channel, data: print("frequency:", "{:.9f}".format(data.frequency), "absoluteImpedance: ", "{:.9f}".format(data.absoluteImpedance), "phaseAngle: ", "{:.9f}".format(data.phaseAngle)))
56 # print when a new node starts to the console
57 handler.experimentNewElementStarting.connect(lambda channel, data: print("New Node beginning:", data.stepName, "step number: ", data.stepNumber, " step sub : ", data.substepNumber))
58 # called when an experiment has completed
59 handler.experimentStopped.connect(experiment_complete_handler)
60
61 # initialize an experiment
62 experiment = AisExperiment()
63 # define a constant potential experiment at 0.2 V, with 1 s sampling time, and a duration of 10 s
64 cvElement = AisConstantPotElement(0.2, 1, 10)
65 # define a constant current experiment at 0.1 A, with 0.1 s sampling time, and a duration of 5 s
66 ccElement = AisConstantCurrentElement(0.1, 0.1, 5)
67
68 success = True
69
70 # initialize a sub experiment
71 subExperiment = AisExperiment()
72 # add constant current experiment to position 1 of the sub experiment
73 # this experiment will run 1 time
74 success &= subExperiment.appendElement(ccElement, 1)
75 # add constant potential experiment to positions 2 and 3 of the sub experiment
76 # this experiment will run 2 times
77 success &= subExperiment.appendElement(cvElement, 2)
78
79 # add constant current experiment to position 1 and 2 of the main experiment
80 # this experiment will run 2 times
81 success &= experiment.appendElement(ccElement, 2)
82 # add constant potential experiment to position 3 of the main experiment
83 # this experiment will run 1 time
84 success &= experiment.appendElement(cvElement, 1)
85
86 # add the sub experiment to the main experiment
87 # the sub experiment will run 2 times
88 success &= experiment.appendSubExperiment(subExperiment, 2)
89
90 if not success:
91 print("Error building experiment")
92 app.quit()
93
94 # upload experiment list to the given channel
95 error = handler.uploadExperimentToChannel(0, experiment)
96 if error.value() != AisErrorCode.Success:
97 print(error.message())
98 app.quit()
99
100 # start the expiment on channel
101 error = handler.startUploadedExperiment(0)
102 if error.value() != AisErrorCode.Success:
103 print(error.message())
104 app.quit()
105
106 # Add initial delay (5 s)
107 await asyncio.sleep(5)
108
109# setup main to be ran as async
110startFunc = main()
111asyncio.run(startFunc)
112
113# exit application
114sys.exit(app.exec()) # Start the event loop
115
an experiment that simulates a constant current flow with more advance options for stopping the exper...
Definition AisConstantCurrentElement.h:18
an experiment that simulates a constant applied voltage.
Definition AisConstantPotElement.h:18
static AisDeviceTracker * Instance()
get the instance of the device tracker.
this class is used to create custom experiments. A custom experiment contains one or more elements....
Definition AisExperiment.h:22