1'''
2AI - AI_continuous_with_logger.py with asynchronous mode.
3
4This example demonstrates the process of obtaining AI data in continuous mode with 8 channels from WifiDAQE3AOD.
5Then, save data into CSV file.
6
7To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
8Next, it outlines the procedure for reading and saving 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-2024 WPC Systems Ltd. All rights reserved.
19'''
20
21## Python
22import asyncio
23
24## WPC
25
26from wpcsys import pywpc
27
28
29async def main():
30 ## Get Python driver version
31 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
32
33 ## Create device handle
34 dev = pywpc.WifiDAQE3AOD()
35
36 ## Connect to device
37 try:
38 dev.connect("192.168.5.38") ## Depend on your device
39 except Exception as err:
40 pywpc.printGenericError(err)
41 ## Release device handle
42 dev.close()
43 return
44
45 try:
46 ## Parameters setting
47 port = 0 ## Depend on your device
48 mode = 2 ## 0 : On demand, 1 : N-samples, 2 : Continuous.
49 sampling_rate = 200
50 read_points = 200
51 read_delay = 0.2 ## second
52
53
54 ## Get firmware model & version
55 driver_info = await dev.Sys_getDriverInfo_async()
56 print("Model name: " + driver_info[0])
57 print("Firmware version: " + driver_info[-1])
58
59 ## Open file with WPC_test.csv
60 err = dev.Logger_openFile("WPC_test.csv")
61 print(f"Logger_openFile, status: {err}")
62
63 ## Write header into CSV file
64 err = dev.Logger_writeHeader(["CH0","CH1","CH2","CH3","CH4","CH5","CH6","CH7"])
65 print(f"Logger_writeHeader, status: {err}")
66
67 ## Open AI
68 err = await dev.AI_open_async(port)
69 print(f"AI_open_async in port {port}, status: {err}")
70
71
72 ## Set AI acquisition mode to continuous mode (2)
73 err = await dev.AI_setMode_async(port, mode)
74 print(f"AI_setMode_async {mode} in port {port}, status: {err}")
75
76 ## Set AI sampling rate
77 err = await dev.AI_setSamplingRate_async(port, sampling_rate)
78 print(f"AI_setSamplingRate_async {sampling_rate} in port {port}, status: {err}")
79
80 ## Open AI streaming
81 err = await dev.AI_openStreaming_async(port)
82 print(f"AI_openStreaming_async in port {port}, status: {err}")
83
84 ## Start AI streaming
85 err = await dev.AI_startStreaming_async(port)
86 print(f"AI_startStreaming_async in port {port}, status: {err}")
87
88 ## Wait for acquisition
89 await asyncio.sleep(1) ## delay [s]
90
91 ## Close AI streaming
92 err = await dev.AI_closeStreaming_async(port)
93 print(f"AI_closeStreaming_async in port {port}, status: {err}")
94
95 data_len = 1
96 while data_len > 0:
97 ## Read data acquisition
98 ai_2Dlist = await dev.AI_readStreaming_async(port, read_points, read_delay)
99 print(f"number of samples = {len(ai_2Dlist)}" )
100
101 ## Write data into CSV file
102 dev.Logger_write2DList(ai_2Dlist)
103
104 ## Update data len
105 data_len = len(ai_2Dlist)
106
107 ## Close AI
108 err = await dev.AI_close_async(port)
109 print(f"AI_close_async in port {port}, status: {err}")
110 except Exception as err:
111 pywpc.printGenericError(err)
112
113 ## Disconnect device
114 dev.disconnect()
115
116 ## Release device handle
117 dev.close()
118
119 return
120
121def main_for_spyder(*args):
122 if asyncio.get_event_loop().is_running():
123 return asyncio.create_task(main(*args)).result()
124 else:
125 return asyncio.run(main(*args))
126if __name__ == '__main__':
127 asyncio.run(main()) ## Use terminal
128 # await main() ## Use Jupyter or IPython(>=7.0)
129 # main_for_spyder() ## Use Spyder