1'''
2Encoder - Encoder_read.py with synchronous mode.
3
4This example demonstrates how to read encoder with USBDAQF1D.
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 time
18
19## WPC
20
21from wpcsys import pywpc
22
23def main():
24 ## Get Python driver version
25 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
26
27 ## Create device handle
28 dev = pywpc.USBDAQF1D()
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 timeout = 3 ## second
43 position = 0
44 direction = 1 ## 1 : Forward, -1 : Reverse
45 window_size = 100
46
47 ## Get firmware model & version
48 driver_info = dev.Sys_getDriverInfo(timeout)
49 print("Model name: " + driver_info[0])
50 print("Firmware version: " + driver_info[-1])
51
52 ## Open encoder
53 err = dev.Encoder_open(channel, timeout)
54 print(f"Encoder_open in channel {channel}, status: {err}")
55
56 ## Set encoder direction
57 err = dev.Encoder_setDirection(channel, direction, timeout)
58 print(f"Encoder_setDirection in channel {channel}, status: {err}")
59
60 ## Set encoder position
61 err = dev.Encoder_setPosition(channel, position, timeout)
62 print(f"Encoder_setPosition in channel {channel}, status: {err}")
63
64 ## Set encoder frequency window size
65 err = dev.Encoder_setFreqWindow(channel, window_size, timeout)
66 print(f"Encoder_setFreqWindow in channel {channel}, status: {err}")
67
68 ## Start encoder
69 err = dev.Encoder_start(channel, timeout)
70 print(f"Encoder_start in channel {channel}, status: {err}")
71
72 ## Read encoder position
73 while True:
74 position = dev.Encoder_readPosition(channel, timeout)
75 print(f"Encoder position in channel {channel}: {position}")
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 = dev.Encoder_stop(channel, timeout)
85 print(f"Encoder_stop in channel {channel}, status: {err}")
86
87 ## Close encoder
88 err = dev.Encoder_close(channel, timeout)
89 print(f"Encoder_close in channel {channel}, status: {err}")
90
91 ## Disconnect device
92 dev.disconnect()
93
94 ## Release device handle
95 dev.close()
96 return
97
98if __name__ == '__main__':
99 main()