AI on demand in loop

 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 USBDAQF1AOD.
 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-2025 WPC Systems Ltd. All rights reserved.
19'''
20
21## WPC
22from wpcsys import pywpc
23
24
25def main():
26    ## Get Python driver version
27    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
28
29    ## Create device handle
30    dev = pywpc.USBDAQF1AOD()
31
32    ## Connect to device
33    try:
34        dev.connect("default")  ## Depend on your device
35    except Exception as err:
36        pywpc.printGenericError(err)
37        ## Release device handle
38        dev.close()
39        return
40
41    try:
42        ## Parameters setting
43        port = 0  ## Depend on your device
44        mode = 0
45        timeout = 3  ## [sec]
46        channel = 8
47        ## Get firmware model & version
48        driver_info = dev.Sys_getDriverInfo(timeout)
49        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
50
51        ## Open AI
52        err = dev.AI_open(port, timeout)
53        print(f"AI_open in port {port}, status: {err}")
54        
55        ## Set AI channel
56        err = dev.AI_enableChannel(port, channel, timeout)
57        print(f"AI_enableChannel in port {port}, status: {err}")
58
59        ## Set AI acquisition mode to on demand mode (0)
60        err = dev.AI_setMode(port, mode, timeout)
61        print(f"AI_setMode {mode} in port {port}, status: {err}")
62
63        ## Read AI
64        for i in range(5):
65            ai_list = dev.AI_readOnDemand(port)
66            print(f"Data in port {port}: {ai_list}")
67
68        ## Close AI
69        err = dev.AI_close(port, timeout)
70        print(f"AI_close in port {port}, status: {err}")
71    except Exception as err:
72        pywpc.printGenericError(err)
73
74    finally:
75        ## Disconnect device
76        dev.disconnect()
77
78        ## Release device handle
79        dev.close()
80
81
82if __name__ == '__main__':
83    main()