1'''
2Encoder - Encoder_read.py with synchronous mode.
3
4This example demonstrates how to read encoder with USBDAQF1CD.
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
20def main():
21 ## Get Python driver version
22 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
23
24 ## Create device handle
25 dev = pywpc.USBDAQF1CD()
26
27 ## Connect to device
28 try:
29 dev.connect("default") ## Depend on your device
30 except Exception as err:
31 pywpc.printGenericError(err)
32 ## Release device handle
33 dev.close()
34 return
35
36 try:
37 ## Parameters setting
38 channel = 0 ## Depend on your device
39 timeout = 3 ## [sec]
40 position = 0
41 direction = 1 ## 1 : Forward, -1 : Reverse
42 window_size = 100
43
44 ## Get firmware model & version
45 driver_info = dev.Sys_getDriverInfo(timeout)
46 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
47
48 ## Open encoder
49 err = dev.Encoder_open(channel, timeout)
50 print(f"Encoder_open in channel {channel}, status: {err}")
51
52 ## Set encoder direction
53 err = dev.Encoder_setDirection(channel, direction, timeout)
54 print(f"Encoder_setDirection in channel {channel}, status: {err}")
55
56 ## Set encoder position
57 err = dev.Encoder_setPosition(channel, position, timeout)
58 print(f"Encoder_setPosition in channel {channel}, status: {err}")
59
60 ## Set encoder frequency window size
61 err = dev.Encoder_setFreqWindow(channel, window_size, timeout)
62 print(f"Encoder_setFreqWindow in channel {channel}, status: {err}")
63
64 ## Start encoder
65 err = dev.Encoder_start(channel, timeout)
66 print(f"Encoder_start in channel {channel}, status: {err}")
67
68 ## Read encoder position
69 while True:
70 position = dev.Encoder_readPosition(channel, timeout)
71 print(f"Encoder position in channel {channel}: {position}")
72 except KeyboardInterrupt:
73 print("Press keyboard")
74
75 except Exception as err:
76 pywpc.printGenericError(err)
77
78 finally:
79 ## Stop encoder
80 err = dev.Encoder_stop(channel, timeout)
81 print(f"Encoder_stop in channel {channel}, status: {err}")
82
83 ## Close encoder
84 err = dev.Encoder_close(channel, timeout)
85 print(f"Encoder_close in channel {channel}, status: {err}")
86
87 ## Disconnect device
88 dev.disconnect()
89
90 ## Release device handle
91 dev.close()
92
93
94if __name__ == '__main__':
95 main()