Counter read

  1'''
  2Counter - Counter_read.py with synchronous mode.
  3
  4This example demonstrates how to read counter 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 = 1 ## Depend on your device
 42        timeout = 3 ## second
 43        edge = 0 ##  0: Falling edge, 1: Rising edge
 44        window_size = 100
 45        position = 0
 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 counter
 53        err = dev.Counter_open(channel, timeout)
 54        print(f"Counter_open in channel {channel}, status: {err}")
 55
 56        ## Set counter edge
 57        err = dev.Counter_setEdge(channel, edge, timeout)
 58        print(f"Counter_setEdge in channel {channel}, status: {err}")
 59
 60        ## Set counter frequency window size
 61        err = dev.Counter_setFreqWindow(channel, window_size, timeout)
 62        print(f"Counter_setFreqWindow in channel {channel}, status: {err}")
 63
 64        ## Set counter position
 65        err = dev.Counter_setPosition(channel, position, timeout)
 66        print(f"Counter_setPosition in channel {channel}, status: {err}")
 67
 68        ## Start counter
 69        err = dev.Counter_start(channel, timeout)
 70        print(f"Counter_start in channel {channel}, status: {err}")
 71
 72        ## Read counter position
 73        while True:
 74            posi = dev.Counter_readPosition(channel, timeout)
 75            print(f"Read counter 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 counter
 84        err = dev.Counter_stop(channel, timeout)
 85        print(f"Counter_stop in channel {channel}, status: {err}")
 86
 87        ## Close counter
 88        err = dev.Counter_close(channel, timeout)
 89        print(f"Counter_close in channel {channel}, status: {err}")
 90
 91        ## Disconnect device
 92        dev.disconnect()
 93
 94        ## Release device handle
 95        dev.close()
 96
 97    return
 98
 99if __name__ == '__main__':
100    main()