1'''
2Encoder - Encoder_read.py with asynchronous mode.
3
4This example demonstrates how to read encoder with USBDAQF1RD.
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.USBDAQF1RD()
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 = 0 ## Depend on your device
44 direction = 1 ## 1 : Forward, -1 : Reverse
45 position = 0
46 window_size = 100
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 encoder
53 err = await dev.Encoder_open_async(channel)
54 print(f"Encoder_open_async in channel {channel}, status: {err}")
55
56 ## Set encoder direction
57 err = await dev.Encoder_setDirection_async(channel, direction)
58 print(f"Encoder_setDirection_async in channel {channel}, status: {err}")
59
60 ## Set encoder position
61 err = await dev.Encoder_setPosition_async(channel, position)
62 print(f"Encoder_setPosition_async in channel {channel}, status: {err}")
63
64 ## Set encoder frequency window size
65 err = await dev.Encoder_setFreqWindow_async(channel, window_size)
66 print(f"Encoder_setFreqWindow_async in channel {channel}, status: {err}")
67
68 ## Start encoder
69 err = await dev.Encoder_start_async(channel)
70 print(f"Encoder_start_async in channel {channel}, status: {err}")
71
72 ## Read encoder position
73 while True:
74 posi = await dev.Encoder_readPosition_async(channel)
75 print(f"Encoder 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 encoder
84 err = await dev.Encoder_stop_async(channel)
85 print(f"Encoder_stop_async in channel {channel}, status: {err}")
86
87 ## Close encoder
88 err = await dev.Encoder_close_async(channel)
89 print(f"Encoder_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