1'''
2AI - AI_N_samples_once.py with asynchronous 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 EthanA.
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
11-------------------------------------------------------------------------------------
12Please change correct serial number or IP and port number BEFORE you run example code.
13
14For other examples please check:
15 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
16See README.md file to get detailed usage of this example.
17
18Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
19'''
20
21## WPC
22from wpcsys import pywpc
23
24## Python
25import asyncio
26import sys
27sys.path.insert(0, 'src/')
28
29
30async def main():
31 ## Get Python driver version
32 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
33
34 ## Create device handle
35 dev = pywpc.EthanA()
36
37 ## Connect to device
38 try:
39 dev.connect("192.168.1.110") ## Depend on your device
40 except Exception as err:
41 pywpc.printGenericError(err)
42 ## Release device handle
43 dev.close()
44 return
45
46 try:
47 ## Parameters setting
48 port = 0 ## Depend on your device
49 mode = 1 ## 0: On demand, 1: N-samples, 2: Continuous
50 channel = 8
51 sampling_rate = 1000
52 samples = 200
53 read_points = 200
54 read_delay = 0.5 ## [sec]
55
56 ## Get firmware model & version
57 driver_info = await dev.Sys_getDriverInfo_async()
58 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
59
60 ## Open AI
61 err = await dev.AI_open_async(port)
62 print(f"AI_open_async in port {port}, status: {err}")
63
64 ## Set AI acquisition mode to N-samples mode (1)
65 err = await dev.AI_setMode_async(port, mode)
66 print(f"AI_setMode_async {mode} in port {port}, status: {err}")
67
68 ## Set AI sampling rate
69 err = await dev.AI_setSamplingRate_async(port, sampling_rate)
70 print(f"AI_setSamplingRate_async {sampling_rate} in port {port}, status: {err}")
71
72 ## Set AI # of samples
73 err = await dev.AI_setNumSamples_async(port, samples)
74 print(f"AI_setNumSamples_async {samples} in port {port}, status: {err}")
75
76 ## Open AI streaming
77 err = await dev.AI_openStreaming_async(port)
78 print(f"AI_openStreaming_async in port {port}, status: {err}")
79
80 ## Start AI streaming
81 err = await dev.AI_startStreaming_async(port)
82 print(f"AI_startStreaming_async in port {port}, status: {err}")
83
84 ## Read AI
85 ai_2Dlist = await dev.AI_readStreaming_async(port, read_points, read_delay)
86 print(f"number of samples = {len(ai_2Dlist)}")
87
88 ok = True
89 for i, ai_list in enumerate(ai_2Dlist):
90 ## Check for any missing data
91 if len(ai_list) != channel:
92 print(i, ai_list)
93 ok = False
94 if ok:
95 print('OK')
96 else:
97 print('NG')
98
99 ## Close AI streaming
100 err = await dev.AI_closeStreaming_async(port)
101 print(f"AI_closeStreaming_async in port {port}, status: {err}")
102
103 ## Close AI
104 err = await dev.AI_close_async(port)
105 print(f"AI_close_async in port {port}, status: {err}")
106 except Exception as err:
107 pywpc.printGenericError(err)
108
109 finally:
110 ## Disconnect device
111 dev.disconnect()
112
113 ## Release device handle
114 dev.close()
115
116
117def main_for_spyder(*args):
118 if asyncio.get_event_loop().is_running():
119 return asyncio.create_task(main(*args)).result()
120 else:
121 return asyncio.run(main(*args))
122
123
124if __name__ == '__main__':
125 asyncio.run(main()) ## Use terminal
126 # await main() ## Use Jupyter or IPython(>=7.0)
127 # main_for_spyder() ## Use Spyder