AI on demand once

 1'''
 2AI24Bit - AI_on_demand_once.py with asynchronous mode.
 3
 4This example demonstrates the process of obtaining AI data in on demand mode.
 5Additionally, it retrieve AI data from EthanA2.
 6
 7-------------------------------------------------------------------------------------
 8Please change correct serial number or IP and port number BEFORE you run example code.
 9
10For other examples please check:
11    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
12See README.md file to get detailed usage of this example.
13
14Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
15'''
16
17## WPC
18from wpcsys import pywpc
19
20## Python
21import asyncio
22import sys
23sys.path.insert(0, 'src/')
24
25
26async def main():
27    ## Get Python driver version
28    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
29
30    ## Create device handle
31    dev = pywpc.EthanA2()
32
33    ## Connect to device
34    try:
35        dev.connect("192.168.1.110")  ## Depend on your device
36    except Exception as err:
37        pywpc.printGenericError(err)
38        ## Release device handle
39        dev.close()
40        return
41
42    try:
43        ## Parameters setting
44        port = 0  ## Depend on your device
45
46        ## Get firmware model & version
47        driver_info = await dev.Sys_getDriverInfo_async()
48        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
49
50        ## Open AI
51        err = await dev.AI_open_async(port)
52        print(f"AI_open_async in port {port}, status: {err}")
53
54        ## Data acquisition
55        data = await dev.AI_readOnDemand_async(port)
56        print(f"data in port {port}: ")
57        print(f"{data}")
58
59        ## Close AI
60        err = await dev.AI_close_async(port)
61        print(f"AI_close_async in port {port}, status: {err}")
62    except Exception as err:
63        pywpc.printGenericError(err)
64
65    finally:
66        ## Disconnect device
67        dev.disconnect()
68
69        ## Release device handle
70        dev.close()
71
72
73def main_for_spyder(*args):
74    if asyncio.get_event_loop().is_running():
75        return asyncio.create_task(main(*args)).result()
76    else:
77        return asyncio.run(main(*args))
78
79
80if __name__ == '__main__':
81    asyncio.run(main())  ## Use terminal
82    # await main()  ## Use Jupyter or IPython(>=7.0)
83    # main_for_spyder()  ## Use Spyder