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