1'''
2Counter - Counter_read.py with asynchronous mode.
3
4This example demonstrates how to read counter with USBDAQF1TD.
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 main():
24 ## Get Python driver version
25 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
26
27 ## Create device handle
28 dev = pywpc.USBDAQF1TD()
29
30 ## Connect to device
31 try:
32 dev.connect("default") ## Depend on your device
33 except Exception as err:
34 pywpc.printGenericError(err)
35 ## Release device handle
36 dev.close()
37 return
38
39 try:
40 ## Parameters setting
41 channel = 1 ## Depend on your device
42 edge = 0 ## 0: Falling edge, 1: Rising edge
43 window_size = 100
44 position = 0
45
46 ## Get firmware model & version
47 driver_info = await dev.Sys_getDriverInfo_async()
48 print("Model name: " + driver_info[0])
49 print("Firmware version: " + driver_info[-1])
50
51 ## Open counter
52 err = await dev.Counter_open_async(channel)
53 print(f"Counter_open_async in channel {channel}, status: {err}")
54
55 ## Set counter edge
56 err = await dev.Counter_setEdge_async(channel, edge)
57 print(f"Counter_setEdge in channel {channel}, status: {err}")
58
59 ## Set counter frequency window size
60 err = await dev.Counter_setFreqWindow_async(channel, window_size)
61 print(f"Counter_setFreqWindow in channel {channel}, status: {err}")
62
63 ## Set counter position
64 err = await dev.Counter_setPosition_async(channel, position)
65 print(f"Counter_setPosition_async in channel {channel}, status: {err}")
66
67 ## Start counter
68 err = await dev.Counter_start_async(channel)
69 print(f"Counter_start_async in channel {channel}, status: {err}")
70
71 ## Read counter position
72 while True:
73 posi = await dev.Counter_readPosition_async(channel)
74 print(f"Read counter position in channel {channel}: {posi}")
75 except KeyboardInterrupt:
76 print("Press keyboard")
77
78 except Exception as err:
79 pywpc.printGenericError(err)
80
81 finally:
82 ## Stop counter
83 err = await dev.Counter_stop_async(channel)
84 print(f"Counter_stop_async in channel {channel}, status: {err}")
85
86 ## Close counter
87 err = await dev.Counter_close_async(channel)
88 print(f"Counter_close_async in channel {channel}, status: {err}")
89
90 ## Disconnect device
91 dev.disconnect()
92
93 ## Release device handle
94 dev.close()
95
96 return
97
98def main_for_spyder(*args):
99 if asyncio.get_event_loop().is_running():
100 return asyncio.create_task(main(*args)).result()
101 else:
102 return asyncio.run(main(*args))
103
104if __name__ == '__main__':
105 asyncio.run(main()) ## Use terminal
106 # await main() ## Use Jupyter or IPython(>=7.0)
107 # main_for_spyder() ## Use Spyder