1'''
2Tutorial - single_loop_async.py with asynchronous mode.
3
4This example project demonstrates how to use async thread to get RTC from USBDAQF1AOD.
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
18
19## WPC
20
21from wpcsys import pywpc
22
23async def loop_func(handle, timeout=3, delay=1):
24 t = 0
25 while t < timeout:
26 data = await handle.Sys_getRTC_async()
27 print("RTC Time:" + str(data))
28 await asyncio.sleep(delay) ## delay [s]
29 t += delay
30
31async def main():
32 ## Get Python driver version
33 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
34
35 ## Create device handle
36 dev = pywpc.USBDAQF1AOD()
37
38 ## Connect to device
39 try:
40 dev.connect("default") ## Depend on your device
41 except Exception as err:
42 pywpc.printGenericError(err)
43 ## Release device handle
44 dev.close()
45 return
46
47 ## Perform async thread to query data
48 try:
49 ## Get firmware model & version
50 driver_info = await dev.Sys_getDriverInfo_async()
51 print("Model name: " + driver_info[0])
52 print("Firmware version: " + driver_info[-1])
53
54 await loop_func(dev, timeout=3, delay= 1)
55 except Exception as err:
56 pywpc.printGenericError(err)
57
58 ## This part never execute because the async thread
59 ## Disconnect device
60 dev.disconnect()
61
62 ## Release device handle
63 dev.close()
64
65 return
66
67def main_for_spyder(*args):
68 if asyncio.get_event_loop().is_running():
69 return asyncio.create_task(main(*args)).result()
70 else:
71 return asyncio.run(main(*args))
72
73if __name__ == '__main__':
74 asyncio.run(main()) ## Use terminal
75 # await main() ## Use Jupyter or IPython(>=7.0)
76 # main_for_spyder() ## Use Spyder