1'''
2AI - AI_N_samples_once.py with synchronous mode.
3
4This example demonstrates the process of obtaining AI data in N-sample mode.
5Additionally, it gets AI data with points in once from STEM.
6
7To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
8Next, it outlines the procedure for reading the streaming AI data.
9Finally, it concludes by explaining how to close the AI.
10
11If your product is "STEM", please invoke the function `Sys_setAIOMode`and `AI_enableCS`.
12Example: AI_enableCS is {0, 2}
13Subsequently, the returned value of AI_readOnDemand and AI_readStreaming will be displayed as follows.
14data:
15 CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7
16 | | |
17 |---------------- CS0-----------------|---------------- CS2------------------|
18[sample0]
19[sample1]
20 .
21 .
22 .
23[sampleN]
24
25-------------------------------------------------------------------------------------
26Please change correct serial number or IP and port number BEFORE you run example code.
27
28For other examples please check:
29 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
30See README.md file to get detailed usage of this example.
31
32Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
33'''
34
35## Python
36import time
37
38## WPC
39
40from wpcsys import pywpc
41
42
43def main():
44 ## Get Python driver version
45 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
46
47 ## Create device handle
48 dev = pywpc.STEM()
49
50 ## Connect to device
51 try:
52 dev.connect("192.168.1.110") ## Depend on your device
53 except Exception as err:
54 pywpc.printGenericError(err)
55 ## Release device handle
56 dev.close()
57 return
58
59 try:
60 ## Parameters setting
61 slot = 1 ## Connect AIO module to slot
62 mode = 1 ## 0 : On demand, 1 : N-samples, 2 : Continuous
63 sampling_rate = 1000
64 samples = 200
65 read_points = 200
66 read_delay = 0.5 ## second
67 timeout = 3 ## second
68 chip_select = [0, 1]
69
70 ## Get firmware model & version
71 driver_info = dev.Sys_getDriverInfo(timeout)
72 print("Model name: " + driver_info[0])
73 print("Firmware version: " + driver_info[-1])
74
75 ## Get slot mode
76 slot_mode = dev.Sys_getMode(slot, timeout)
77 print("Slot mode:", slot_mode)
78
79 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
80 if slot_mode != "AIO":
81 err = dev.Sys_setAIOMode(slot, timeout)
82 print(f"Sys_setAIOMode in slot {slot}, status: {err}")
83
84 ## Get slot mode
85 slot_mode = dev.Sys_getMode(slot, timeout)
86 print("Slot mode:", slot_mode)
87
88 ## Open AI
89 err = dev.AI_open(slot, timeout)
90 print(f"AI_open in slot {slot}, status: {err}")
91
92 ## Enable CS
93 err = dev.AI_enableCS(slot, chip_select, timeout)
94 print(f"AI_enableCS in slot {slot}, status: {err}")
95
96 ## Set AI acquisition mode to N-samples mode (1)
97 err = dev.AI_setMode(slot, mode, timeout)
98 print(f"AI_setMode {mode} in slot {slot}, status: {err}")
99
100 ## Set AI sampling rate
101 err = dev.AI_setSamplingRate(slot, sampling_rate, timeout)
102 print(f"AI_setSamplingRate {sampling_rate} in slot {slot}, status: {err}")
103
104 ## Set AI # of samples
105 err = dev.AI_setNumSamples(slot, samples, timeout)
106 print(f"AI_setNumSamples {samples} in slot {slot}, status: {err}")
107
108 ## Open AI streaming
109 err = dev.AI_openStreaming(slot, timeout)
110 print(f"AI_openStreaming in slot {slot}, status: {err}")
111
112 ## Start AI streaming
113 err = dev.AI_startStreaming(slot, timeout)
114 print(f"AI_startStreaming in slot {slot}, status: {err}")
115
116 ## Read AI data
117 ai_2Dlist = dev.AI_readStreaming(slot, read_points, read_delay)
118 print(f"number of samples = {len(ai_2Dlist)}" )
119
120 ok = True
121 for i, ai_list in enumerate(ai_2Dlist):
122 ## Check for any missing data
123 if len(ai_list) != len(chip_select)*8:
124 print(i, ai_list)
125 ok = False
126 if ok:
127 print('OK')
128 else:
129 print('NG')
130
131 ## Close AI streaming
132 err = dev.AI_closeStreaming(slot, timeout)
133 print(f"AI_closeStreaming in slot {slot}, status: {err}")
134
135 ## Close AI
136 err = dev.AI_close(slot, timeout)
137 print(f"AI_close in slot {slot}, status: {err}")
138 except Exception as err:
139 pywpc.printGenericError(err)
140
141 ## Disconnect device
142 dev.disconnect()
143
144 ## Release device handle
145 dev.close()
146
147 return
148
149if __name__ == '__main__':
150 main()