1'''
2AI - AI_on_demand_in_loop.py with synchronous mode.
3
4This example demonstrates the process of obtaining AI data in on demand mode.
5Additionally, it utilizes a loop to retrieve AI data with 5 times 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 AI on demand 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-2025 WPC Systems Ltd. All rights reserved.
33'''
34
35## WPC
36from wpcsys import pywpc
37
38
39def main():
40 ## Get Python driver version
41 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
42
43 ## Create device handle
44 dev = pywpc.STEM()
45
46 ## Connect to device
47 try:
48 dev.connect("192.168.1.110") ## Depend on your device
49 except Exception as err:
50 pywpc.printGenericError(err)
51 ## Release device handle
52 dev.close()
53 return
54
55 try:
56 ## Parameters setting
57 slot = 1 ## Connect AIO module to slot
58 mode = 0
59 timeout = 3 ## [sec]
60 chip_select = [0, 1]
61
62 ## Get firmware model & version
63 driver_info = dev.Sys_getDriverInfo(timeout)
64 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
65
66 ## Get slot mode
67 slot_mode = dev.Sys_getMode(slot, timeout)
68 print("Slot mode:", slot_mode)
69
70 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
71 if slot_mode != "AIO":
72 err = dev.Sys_setAIOMode(slot, timeout)
73 print(f"Sys_setAIOMode in slot {slot}, status: {err}")
74
75 ## Get slot mode
76 slot_mode = dev.Sys_getMode(slot, timeout)
77 print("Slot mode:", slot_mode)
78
79 ## Open AI
80 err = dev.AI_open(slot, timeout)
81 print(f"AI_open in slot {slot}, status: {err}")
82
83 ## Enable CS
84 err = dev.AI_enableCS(slot, chip_select, timeout)
85 print(f"AI_enableCS in slot {slot}, status: {err}")
86
87 ## Set AI acquisition mode to on demand mode (0)
88 err = dev.AI_setMode(slot, mode, timeout)
89 print(f"AI_setMode {mode} in slot {slot}, status: {err}")
90
91 ## Read AI
92 for i in range(5):
93 ai_list = dev.AI_readOnDemand(slot)
94 print(f"data in slot {slot}: {ai_list}")
95
96 ## Close AI
97 err = dev.AI_close(slot, timeout)
98 print(f"AI_close in slot {slot}, status: {err}")
99 except Exception as err:
100 pywpc.printGenericError(err)
101
102 finally:
103 ## Disconnect device
104 dev.disconnect()
105
106 ## Release device handle
107 dev.close()
108
109
110if __name__ == '__main__':
111 main()