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 USBDAQF1AD.
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
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 time
23
24## WPC
25
26from wpcsys import pywpc
27
28
29def main():
30 ## Get Python driver version
31 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
32
33 ## Create device handle
34 dev = pywpc.USBDAQF1AD()
35
36 ## Connect to device
37 try:
38 dev.connect("default") ## 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 = 0
49 timeout = 3 ## second
50 channel = 8
51
52 ## Get firmware model & version
53 driver_info = dev.Sys_getDriverInfo(timeout)
54 print("Model name: " + driver_info[0])
55 print("Firmware version: " + driver_info[-1])
56
57 ## Open AI
58 err = dev.AI_open(port, timeout)
59 print(f"AI_open in port {port}, status: {err}")
60
61 ## Set AI channel
62 err = dev.AI_enableChannel(port, channel, timeout)
63 print(f"AI_enableChannel in port {port}, status: {err}")
64
65 ## Set AI acquisition mode to on demand mode (0)
66 err = dev.AI_setMode(port, mode, timeout)
67 print(f"AI_setMode {mode} in port {port}, status: {err}")
68
69 ## Read AI
70 for i in range(5):
71 ai_list = dev.AI_readOnDemand(port)
72 print(f"Data in port {port}: {ai_list}")
73
74 ## Close AI
75 err = dev.AI_close(port, timeout)
76 print(f"AI_close in port {port}, status: {err}")
77 except Exception as err:
78 pywpc.printGenericError(err)
79
80 ## Disconnect device
81 dev.disconnect()
82
83 ## Release device handle
84 dev.close()
85
86 return
87
88if __name__ == '__main__':
89 main()