Tutorial single loop thread

 1'''
 2Tutorial - single_loop_thread.py with asynchronous mode.
 3
 4This example project demonstrates how to use thread to get RTC from USBDAQF1AD.
 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-2024 WPC Systems Ltd. All rights reserved.
14'''
15
16## Python
17import asyncio
18import threading
19import time
20## WPC
21
22from wpcsys import pywpc
23
24async def getRTC(handle, delay=1):
25    data = await handle.Sys_getRTC_async()
26    print("RTC Time:" + str(data))
27    await asyncio.sleep(delay) ## delay [s]
28
29def RTC_thread(handle, delay=1):
30    while True:
31        asyncio.run(getRTC(handle, delay))
32        time.sleep(delay) ## delay [s]
33
34async def main():
35    ## Get Python driver version
36    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
37
38    ## Create device handle
39    dev = pywpc.USBDAQF1AD()
40
41    ## Connect to device
42    try:
43        dev.connect("default") ## Depend on your device
44    except Exception as err:
45        pywpc.printGenericError(err)
46        ## Release device handle
47        dev.close()
48        return
49
50    ## Perform two sync thread to query data
51    try:
52        ## Get firmware model & version
53        driver_info = await dev.Sys_getDriverInfo_async()
54        print("Model name: " + driver_info[0])
55        print("Firmware version: " + driver_info[-1])
56
57        _threadRTC = threading.Thread(target = RTC_thread, args = [dev, 1])
58        _threadRTC.start()
59    except Exception as err:
60        pywpc.printGenericError(err)
61
62    ## This part will execute immediately because the sync thread is running in parallel.
63    '''
64    # Disconnect device
65    dev.disconnect()
66
67    # Release device handle
68    dev.close()
69    '''
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