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