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-2025 WPC Systems Ltd. All rights reserved.
32'''
33
34## WPC
35from wpcsys import pywpc
36
37## Python
38import random
39
40
41def main():
42 ## Get Python driver version
43 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
44
45 ## Create device handle
46 dev = pywpc.STEM()
47
48 ## Connect to device
49 try:
50 dev.connect("192.168.1.110") ## Depend on your device
51 except Exception as err:
52 pywpc.printGenericError(err)
53 ## Release device handle
54 dev.close()
55 return
56
57 try:
58 ## Parameters setting
59 slot = 1 ## Connect AIO module to slot
60 chip_select = [0, 1]
61 mode = 2 ## 0: On demand, 1: N-samples, 2: Continuous
62 sampling_rate = 200
63 read_points = 200
64 read_delay = 2 ## [sec]
65 timeout = 3 ## [sec]
66
67 ## Get firmware model & version
68 driver_info = dev.Sys_getDriverInfo(timeout)
69 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
70
71 ## Get slot mode
72 slot_mode = dev.Sys_getMode(slot, timeout)
73 print("Slot mode:", slot_mode)
74
75 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
76 if slot_mode != "AIO":
77 err = dev.Sys_setAIOMode(slot, timeout)
78 print(f"Sys_setAIOMode in slot {slot}, status: {err}")
79
80 ## Get slot mode
81 slot_mode = dev.Sys_getMode(slot, timeout)
82 print("Slot mode:", slot_mode)
83
84 ## Open AO
85 err = dev.AO_open(slot, timeout)
86 print(f"AO_open in slot {slot}, status: {err}")
87
88 ## Open AI
89 err = dev.AI_open(slot, timeout)
90 print(f"AI_open in slot {slot}, status: {err}")
91
92 ## Set AI acquisition mode to continuous mode (2)
93 err = dev.AI_setMode(slot, mode, timeout)
94 print(f"AI_setMode {mode} in slot {slot}, status: {err}")
95
96 ## Set AI sampling rate
97 err = dev.AI_setSamplingRate(slot, sampling_rate, timeout)
98 print(f"AI_setSamplingRate {sampling_rate} in slot {slot}, status: {err}")
99
100 ## Enable CS
101 err = dev.AI_enableCS(slot, chip_select, timeout)
102 print(f"AI_enableCS in slot {slot}, status: {err}")
103
104 ## Open AI streaming
105 err = dev.AI_openStreaming(slot, timeout)
106 print(f"AI_openStreaming in slot {slot}, status: {err}")
107
108 ## Start AI streaming
109 err = dev.AI_startStreaming(slot, timeout)
110 print(f"AI_startStreaming in slot {slot}, status: {err}")
111
112 counter = 0
113 data_len = 1
114 ao_list = [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
115 while data_len > 0:
116 ## Read data acquisition
117 ai_2Dlist = dev.AI_readStreaming(slot, read_points, read_delay)
118 # print(ai_2Dlist)
119 print(f"Data len = {len(ai_2Dlist)}")
120
121 ## Update data len and counter
122 data_len = len(ai_2Dlist)
123 counter += 1
124
125 if counter % 10 == 0:
126 ## Select AO random value from AO list
127 ao_value = random.choice(ao_list)
128
129 ## Write AO vaule in channel 0
130 err = dev.AO_writeOneChannel(slot, 0, ao_value, timeout)
131 print(f"In slot {slot} channel 0, the AO value is {ao_value}, status: {err}")
132
133 except Exception as err:
134 pywpc.printGenericError(err)
135 except KeyboardInterrupt:
136 print("Press keyboard")
137 finally:
138 ## Close AI streaming
139 err = dev.AI_closeStreaming(slot, timeout)
140 print(f"AI_closeStreaming in slot {slot}, status: {err}")
141
142 ## Close AI
143 err = dev.AI_close(slot, timeout)
144 print(f"AI_close in slot {slot}, status: {err}")
145
146 ## Close AO
147 err = dev.AO_close(slot, timeout)
148 print(f"AO_close in slot {slot}, status: {err}")
149
150 ## Disconnect device
151 dev.disconnect()
152
153 ## Release device handle
154 dev.close()
155
156
157if __name__ == '__main__':
158 main()