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 EthanIA.
 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-2024 WPC Systems Ltd. All rights reserved.
15'''
16
17## Python
18import asyncio
19
20## WPC
21
22from wpcsys import pywpc
23
24
25async def main():
26    ## Get Python driver version
27    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
28
29    ## Create device handle
30    dev = pywpc.EthanIA()
31
32    ## Connect to device
33    try:
34        dev.connect("192.168.1.110") ## 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
45        ## Get firmware model & version
46        driver_info = await dev.Sys_getDriverInfo_async()
47        print("Model name: " + driver_info[0])
48        print("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    ## Disconnect device
66    dev.disconnect()
67
68    ## Release device handle
69    dev.close()
70
71    return
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
79if __name__ == '__main__':
80    asyncio.run(main()) ## Use terminal
81    # await main() ## Use Jupyter or IPython(>=7.0)
82    # main_for_spyder() ## Use Spyder