1
2'''
3AIO - AO_output_while_AI_streaming.py with asynchronous mode.
4
5This example demonstrates the process of AI streaming and AO output from STEM.
6Not all of sampling rate can alter the output values of AO.
7Its limitation is that the AI sampling rate and the number of CS must be less than or equal to 3000 Hz.
8
9Please invoke the function `Sys_setAIOMode_async` and `AI_enableCS_async`.
10
11Example: AI_enableCS_async is {0, 2}
12Subsequently, the returned value of AI_readOnDemand_async and AI_readStreaming_async will be displayed as follows.
13data:
14 CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7
15 | | |
16 |---------------- CS0-----------------|---------------- CS2------------------|
17[sample0]
18[sample1]
19 .
20 .
21 .
22[sampleN]
23
24-------------------------------------------------------------------------------------
25Please change correct serial number or IP and port number BEFORE you run example code.
26
27For other examples please check:
28 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
29See README.md file to get detailed usage of this example.
30
31Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
32'''
33
34## Python
35import asyncio
36import random
37
38## WPC
39
40from wpcsys import pywpc
41
42async def main():
43 ## Get Python driver version
44 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
45
46 ## Create device handle
47 dev = pywpc.STEM()
48
49 ## Connect to device
50 try:
51 dev.connect("192.168.1.110") ## Depend on your device
52 except Exception as err:
53 pywpc.printGenericError(err)
54 ## Release device handle
55 dev.close()
56 return
57
58 try:
59 ## Parameters setting
60 slot = 1 ## Connect AIO module to slot
61 chip_select = [0, 1]
62 mode = 2 ## 0 : On demand, 1 : N-samples, 2 : Continuous.
63 sampling_rate = 200
64 read_points = 200
65 read_delay = 2 ## second
66
67 ## Get firmware model & version
68 driver_info = await dev.Sys_getDriverInfo_async()
69 print("Model name: " + driver_info[0])
70 print("Firmware version: " + driver_info[-1])
71
72 ## Get slot mode
73 slot_mode = await dev.Sys_getMode_async(slot)
74 print("Slot mode:", slot_mode)
75
76 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
77 if slot_mode != "AIO":
78 err = await dev.Sys_setAIOMode_async(slot)
79 print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
80
81 ## Get slot mode
82 slot_mode = await dev.Sys_getMode_async(slot)
83 print("Slot mode:", slot_mode)
84
85 ## Open AO
86 err = await dev.AO_open_async(slot)
87 print(f"AO_open_async in slot {slot}, status: {err}")
88
89 ## Open AI
90 err = await dev.AI_open_async(slot)
91 print(f"AI_open_async in slot {slot}, status: {err}")
92
93 ## Set AI acquisition mode to continuous mode (2)
94 err = await dev.AI_setMode_async(slot, mode)
95 print(f"AI_setMode_async {mode} in slot {slot}, status: {err}")
96
97 ## Set AI sampling rate
98 err = await dev.AI_setSamplingRate_async(slot, sampling_rate)
99 print(f"AI_setSamplingRate_async {sampling_rate} in slot {slot}, status: {err}")
100
101 ## Enable CS
102 err = await dev.AI_enableCS_async(slot, chip_select)
103 print(f"AI_enableCS_async in slot {slot}, status: {err}")
104
105 ## Open AI streaming
106 err = await dev.AI_openStreaming_async(slot)
107 print(f"AI_openStreaming_async in slot {slot}, status: {err}")
108
109 ## Start AI streaming
110 err = await dev.AI_startStreaming_async(slot)
111 print(f"AI_startStreaming_async in slot {slot}, status: {err}")
112
113 counter = 0
114 data_len = 1
115 ao_list = [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
116 while data_len > 0:
117 ## Read data acquisition
118 ai_2Dlist = await dev.AI_readStreaming_async(slot, read_points, read_delay)
119 # print(ai_2Dlist)
120 print(f"Data len = {len(ai_2Dlist)}" )
121
122 ## Update data len and counter
123 data_len = len(ai_2Dlist)
124 counter+=1
125
126 if counter % 10 == 0:
127 ## Select AO random value from AO list
128 ao_value = random.choice(ao_list)
129
130 ## Write AO vaule in channel 0
131 err = await dev.AO_writeOneChannel_async(slot, 0, ao_value)
132 print(f"In slot {slot} channel 0, the AO value is {ao_value}, status: {err}")
133
134 except Exception as err:
135 pywpc.printGenericError(err)
136 except KeyboardInterrupt:
137 print("Press keyboard")
138 finally:
139 ## Close AI streaming
140 err = await dev.AI_closeStreaming_async(slot)
141 print(f"AI_closeStreaming_async in slot {slot}, status: {err}")
142
143 ## Close AI
144 err = await dev.AI_close_async(slot)
145 print(f"AI_close_async in slot {slot}, status: {err}")
146
147 ## Close AO
148 err = await dev.AO_close_async(slot)
149 print(f"AO_close_async in slot {slot}, status: {err}")
150
151 ## Disconnect device
152 dev.disconnect()
153
154 ## Release device handle
155 dev.close()
156
157 return
158def main_for_spyder(*args):
159 if asyncio.get_event_loop().is_running():
160 return asyncio.create_task(main(*args)).result()
161 else:
162 return asyncio.run(main(*args))
163if __name__ == '__main__':
164 asyncio.run(main()) ## Use terminal
165 # await main() ## Use Jupyter or IPython(>=7.0)
166 # main_for_spyder() ## Use Spyder