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 USBDAQF1AOD.
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-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.USBDAQF1AOD()
36
37 ## Connect to device
38 try:
39 dev.connect("default") ## 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 = 2 ## 0: On demand, 1: N-samples, 2: Continuous
50 sampling_rate = 200
51 read_points = 200
52 read_delay = 0.2 ## [sec]
53 channel = 8
54
55 ## Get firmware model & version
56 driver_info = await dev.Sys_getDriverInfo_async()
57 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
58 ## Open file with WPC_test.csv
59 err = dev.Logger_openFile("WPC_test.csv")
60 print(f"Logger_openFile, status: {err}")
61
62 ## Write header into CSV file
63 err = dev.Logger_writeHeader(["CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7"])
64 print(f"Logger_writeHeader, status: {err}")
65
66 ## Open AI
67 err = await dev.AI_open_async(port)
68 print(f"AI_open_async in port {port}, status: {err}")
69
70 ## Set AI channel
71 err = await dev.AI_enableChannel_async(port, channel)
72 print(f"AI_enableChannel_async in port {port}, status: {err}")
73
74 ## Set AI acquisition mode to continuous mode (2)
75 err = await dev.AI_setMode_async(port, mode)
76 print(f"AI_setMode_async {mode} in port {port}, status: {err}")
77
78 ## Set AI sampling rate
79 err = await dev.AI_setSamplingRate_async(port, sampling_rate)
80 print(f"AI_setSamplingRate_async {sampling_rate} in port {port}, status: {err}")
81
82 ## Open AI streaming
83 err = await dev.AI_openStreaming_async(port)
84 print(f"AI_openStreaming_async in port {port}, status: {err}")
85
86 ## Start AI streaming
87 err = await dev.AI_startStreaming_async(port)
88 print(f"AI_startStreaming_async in port {port}, status: {err}")
89
90 ## Wait for acquisition
91 await asyncio.sleep(1) ## delay [sec]
92
93 ## Close AI streaming
94 err = await dev.AI_closeStreaming_async(port)
95 print(f"AI_closeStreaming_async in port {port}, status: {err}")
96
97 data_len = 1
98 while data_len > 0:
99 ## Read data acquisition
100 ai_2Dlist = await dev.AI_readStreaming_async(port, read_points, read_delay)
101 print(f"number of samples = {len(ai_2Dlist)}")
102
103 ## Write data into CSV file
104 dev.Logger_write2DList(ai_2Dlist)
105
106 ## Update data len
107 data_len = len(ai_2Dlist)
108
109 ## Close AI
110 err = await dev.AI_close_async(port)
111 print(f"AI_close_async in port {port}, status: {err}")
112 except Exception as err:
113 pywpc.printGenericError(err)
114
115 finally:
116 ## Disconnect device
117 dev.disconnect()
118
119 ## Release device handle
120 dev.close()
121
122
123def main_for_spyder(*args):
124 if asyncio.get_event_loop().is_running():
125 return asyncio.create_task(main(*args)).result()
126 else:
127 return asyncio.run(main(*args))
128
129
130if __name__ == '__main__':
131 asyncio.run(main()) ## Use terminal
132 # await main() ## Use Jupyter or IPython(>=7.0)
133 # main_for_spyder() ## Use Spyder