Tutorial multiple loops async

 1'''
 2Tutorial - multiple_loops_async.py with asynchronous mode.
 3
 4This example project demonstrates how to use two async thread to get RTC & print string from WifiDAQE3A.
 5
 6-------------------------------------------------------------------------------------
 7Please change correct serial number or IP and port number BEFORE you run example code.
 8
 9For other examples please check:
10    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
11See README.md file to get detailed usage of this example.
12
13Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
14'''
15
16## WPC
17from wpcsys import pywpc
18
19## Python
20import asyncio
21import sys
22sys.path.insert(0, 'src/')
23
24
25async def readRTC_loop(handle, delay=1):
26    while True:
27        rtc = await handle.Sys_getRTC_async()
28        print(f"RTC Time: {rtc}")
29        await asyncio.sleep(delay)  ## delay [sec]
30
31
32async def printString_loop(handle, delay=1):
33    while True:
34        print("WPC Systems Ltd")
35        await asyncio.sleep(delay)  ## delay [sec]
36
37
38async def main():
39    ## Get Python driver version
40    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
41
42    ## Create device handle
43    dev = pywpc.WifiDAQE3A()
44
45    ## Connect to device
46    try:
47        dev.connect("192.168.5.38")  ## Depend on your device
48    except Exception as err:
49        pywpc.printGenericError(err)
50        ## Release device handle
51        dev.close()
52        return
53
54    try:
55        ## Get firmware model & version
56        driver_info = await dev.Sys_getDriverInfo_async()
57        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
58
59        await asyncio.gather(readRTC_loop(dev, delay=1), printString_loop(dev, delay=2))  ## delay [sec]
60    except Exception as err:
61        pywpc.printGenericError(err)
62
63    ## This part never execute because the async thread.
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