AI on demand in loop

 1'''
 2AI - AI_on_demand_in_loop.py with asynchronous 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-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.USBDAQF1AOD()
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        channel = 8
50
51        ## Get firmware model & version
52        driver_info = await dev.Sys_getDriverInfo_async()
53        print("Model name: " + driver_info[0])
54        print("Firmware version: " + driver_info[-1])
55
56        ## Open AI
57        err = await dev.AI_open_async(port)
58        print(f"AI_open_async in port {port}, status: {err}")
59        
60        ## Set AI channel
61        err = await dev.AI_enableChannel_async(port, channel)
62        print(f"AI_enableChannel_async in port {port}, status: {err}")
63
64        ## Set AI acquisition mode to on demand mode (0)
65        err = await dev.AI_setMode_async(port, mode)
66        print(f"AI_setMode_async {mode} in port {port}, status: {err}")
67
68        ## Read AI
69        for i in range(5):
70            ai_list = await dev.AI_readOnDemand_async(port)
71            print(f"data in port {port}: {ai_list}")
72
73        ## Close AI
74        err = await dev.AI_close_async(port)
75        print(f"AI_close_async in port {port}, status: {err}")
76    except Exception as err:
77        pywpc.printGenericError(err)
78
79    ## Disconnect device
80    dev.disconnect()
81
82    ## Release device handle
83    dev.close()
84
85    return
86
87def main_for_spyder(*args):
88    if asyncio.get_event_loop().is_running():
89        return asyncio.create_task(main(*args)).result()
90    else:
91        return asyncio.run(main(*args))
92if __name__ == '__main__':
93    asyncio.run(main()) ## Use terminal
94    # await main() ## Use Jupyter or IPython(>=7.0)
95    # main_for_spyder() ## Use Spyder