1'''
2AI - AI_on_demand_once.py with asynchronous mode.
3
4This example demonstrates the process of obtaining AI data in on demand mode.
5Additionally, it retrieve AI data 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_async`and `AI_enableCS_async`.
12Example: AI_enableCS_async is {0, 2}
13Subsequently, the returned value of AI_readOnDemand_async and AI_readStreaming_async 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 asyncio
37
38## WPC
39
40from wpcsys import pywpc
41
42
43async def 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 = 0
63 chip_select = [0, 1]
64
65 ## Get firmware model & version
66 driver_info = await dev.Sys_getDriverInfo_async()
67 print("Model name: " + driver_info[0])
68 print("Firmware version: " + driver_info[-1])
69
70 ## Get slot mode
71 slot_mode = await dev.Sys_getMode_async(slot)
72 print("Slot mode:", slot_mode)
73
74 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
75 if slot_mode != "AIO":
76 err = await dev.Sys_setAIOMode_async(slot)
77 print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
78
79 ## Get slot mode
80 slot_mode = await dev.Sys_getMode_async(slot)
81 print("Slot mode:", slot_mode)
82
83 ## Open AI
84 err = await dev.AI_open_async(slot)
85 print(f"AI_open_async in slot {slot}, status: {err}")
86
87 ## Enable CS
88 err = await dev.AI_enableCS_async(slot, chip_select)
89 print(f"AI_enableCS_async in slot {slot}, status: {err}")
90
91 ## Set AI acquisition mode to on demand mode (0)
92 err = await dev.AI_setMode_async(slot, mode)
93 print(f"AI_setMode_async {mode} in slot {slot}, status: {err}")
94
95 ## Read AI
96 ai_list = await dev.AI_readOnDemand_async(slot)
97 print(f"data in slot {slot}: {ai_list}")
98
99 ## Close AI
100 err = await dev.AI_close_async(slot)
101 print(f"AI_close_async in slot {slot}, status: {err}")
102 except Exception as err:
103 pywpc.printGenericError(err)
104
105 ## Disconnect device
106 dev.disconnect()
107
108 ## Release device handle
109 dev.close()
110
111 return
112
113def main_for_spyder(*args):
114 if asyncio.get_event_loop().is_running():
115 return asyncio.create_task(main(*args)).result()
116 else:
117 return asyncio.run(main(*args))
118if __name__ == '__main__':
119 asyncio.run(main()) ## Use terminal
120 # await main() ## Use Jupyter or IPython(>=7.0)
121 # main_for_spyder() ## Use Spyder