1'''
2AI - AI_continuous.py with synchronous mode.
3
4This example demonstrates the process of obtaining AI data in continuous mode with 8 channels from STEM.
5
6To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
7Next, it outlines the procedure for reading the streaming AI data.
8Finally, it concludes by explaining how to close the AI.
9
10If your product is "STEM", please invoke the function `Sys_setAIOMode`and `AI_enableCS`.
11Example: AI_enableCS is {0, 2}
12Subsequently, the returned value of AI_readOnDemand and AI_readStreaming 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 time
36
37## WPC
38
39from wpcsys import pywpc
40
41
42def 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 mode = 2 ## 0 : On demand, 1 : N-samples, 2 : Continuous
62 sampling_rate = 200
63 read_points = 200
64 read_delay = 0.2 ## second
65 timeout = 3 ## second
66 chip_select = [0, 1]
67
68 ## Get firmware model & version
69 driver_info = dev.Sys_getDriverInfo(timeout)
70 print("Model name: " + driver_info[0])
71 print("Firmware version: " + driver_info[-1])
72
73 ## Get slot mode
74 slot_mode = dev.Sys_getMode(slot, timeout)
75 print("Slot mode:", slot_mode)
76
77 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
78 if slot_mode != "AIO":
79 err = dev.Sys_setAIOMode(slot, timeout)
80 print(f"Sys_setAIOMode in slot {slot}, status: {err}")
81
82 ## Get slot mode
83 slot_mode = dev.Sys_getMode(slot, timeout)
84 print("Slot mode:", slot_mode)
85
86 ## Open AI
87 err = dev.AI_open(slot, timeout)
88 print(f"AI_open in slot {slot}, status: {err}")
89
90 ## Enable CS
91 err = dev.AI_enableCS(slot, chip_select, timeout)
92 print(f"AI_enableCS in slot {slot}, status: {err}")
93
94 ## Set AI acquisition mode to continuous mode (2)
95 err = dev.AI_setMode(slot, mode, timeout)
96 print(f"AI_setMode {mode} in slot {slot}, status: {err}")
97
98 ## Set AI sampling rate
99 err = dev.AI_setSamplingRate(slot, sampling_rate, timeout)
100 print(f"AI_setSamplingRate {sampling_rate} in slot {slot}, status: {err}")
101
102 ## Open AI streaming
103 err = dev.AI_openStreaming(slot, timeout)
104 print(f"AI_openStreaming in slot {slot}, status: {err}")
105
106 ## Start AI streaming
107 err = dev.AI_startStreaming(slot, timeout)
108 print(f"AI_startStreaming in slot {slot}, status: {err}")
109
110 ## Wait a while for data acquisition
111 time.sleep(1) ## delay [s]
112
113 ## Stop AI
114 err = dev.AI_stop(slot, timeout)
115 print(f"AI_stop in slot {slot}, status: {err}")
116
117 data_len = 1
118 while data_len > 0:
119 ## Read data acquisition
120 ai_2Dlist = dev.AI_readStreaming(slot, read_points, read_delay)
121 print(f"number of samples = {len(ai_2Dlist)}" )
122
123 ## Update data len
124 data_len = len(ai_2Dlist)
125
126 ## Close AI streaming
127 err = dev.AI_closeStreaming(slot, timeout)
128 print(f"AI_closeStreaming in slot {slot}, status: {err}")
129 except Exception as err:
130 pywpc.printGenericError(err)
131
132 ## Disconnect device
133 dev.disconnect()
134
135 ## Release device handle
136 dev.close()
137 return
138
139if __name__ == '__main__':
140 main()