Counter read

 1'''
 2Counter - Counter_read.py with synchronous mode.
 3
 4This example demonstrates how to read counter with USBDAQF1AOD.
 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.USBDAQF1AOD()
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 = 1  ## Depend on your device
39        timeout = 3  ## [sec]
40        edge = 0  ##  0: Falling edge, 1: Rising edge
41        window_size = 100
42        position = 0
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 counter
49        err = dev.Counter_open(channel, timeout)
50        print(f"Counter_open in channel {channel}, status: {err}")
51
52        ## Set counter edge
53        err = dev.Counter_setEdge(channel, edge, timeout)
54        print(f"Counter_setEdge in channel {channel}, status: {err}")
55
56        ## Set counter frequency window size
57        err = dev.Counter_setFreqWindow(channel, window_size, timeout)
58        print(f"Counter_setFreqWindow in channel {channel}, status: {err}")
59
60        ## Set counter position
61        err = dev.Counter_setPosition(channel, position, timeout)
62        print(f"Counter_setPosition in channel {channel}, status: {err}")
63
64        ## Start counter
65        err = dev.Counter_start(channel, timeout)
66        print(f"Counter_start in channel {channel}, status: {err}")
67
68        ## Read counter position
69        while True:
70            posi = dev.Counter_readPosition(channel, timeout)
71            print(f"Read counter position in channel {channel}: {posi}")
72    except KeyboardInterrupt:
73        print("Press keyboard")
74
75    except Exception as err:
76        pywpc.printGenericError(err)
77
78    finally:
79        ## Stop counter
80        err = dev.Counter_stop(channel, timeout)
81        print(f"Counter_stop in channel {channel}, status: {err}")
82
83        ## Close counter
84        err = dev.Counter_close(channel, timeout)
85        print(f"Counter_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()