AI continuous

  1'''
  2AI - AI_continuous.py with synchronous mode.
  3
  4This example demonstrates the process of obtaining AI data in continuous mode with 8 channels from EthanA2.
  5
  6To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
  7Next, it outlines the procedure for reading the streaming AI data.
  8Finally, it concludes by explaining how to close the AI.
  9
 10-------------------------------------------------------------------------------------
 11Please change correct serial number or IP and port number BEFORE you run example code.
 12
 13For other examples please check:
 14    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 15See README.md file to get detailed usage of this example.
 16
 17Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
 18'''
 19
 20## Python
 21import time
 22
 23## WPC
 24
 25from wpcsys import pywpc
 26
 27
 28def main():
 29    ## Get Python driver version
 30    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 31
 32    ## Create device handle
 33    dev = pywpc.EthanA2()
 34
 35    ## Connect to device
 36    try:
 37        dev.connect("192.168.1.110") ## Depend on your device
 38    except Exception as err:
 39        pywpc.printGenericError(err)
 40        ## Release device handle
 41        dev.close()
 42        return
 43
 44    try:
 45        ## Parameters setting
 46        port = 0 ## Depend on your device
 47        mode = 2 ## 0 : On demand, 1 : N-samples, 2 : Continuous.
 48        sampling_rate = 200
 49        read_points = 200
 50        read_delay = 0.2 ## second
 51        timeout = 3 ## second
 52        
 53
 54        # Get firmware model & version
 55        driver_info = dev.Sys_getDriverInfo(timeout)
 56        print("Model name: " + driver_info[0])
 57        print("Firmware version: " + driver_info[-1])
 58
 59        ## Open AI
 60        err = dev.AI_open(port, timeout)
 61        print(f"AI_open in port {port}, status: {err}")
 62        
 63
 64        ## Set AI acquisition mode to continuous mode (2)
 65        err = dev.AI_setMode(port, mode, timeout)
 66        print(f"AI_setMode {mode} in port {port}, status: {err}")
 67
 68        ## Set AI sampling rate
 69        err = dev.AI_setSamplingRate(port, sampling_rate, timeout)
 70        print(f"AI_setSamplingRate {sampling_rate} in port {port}, status: {err}")
 71
 72        ## Open AI streaming
 73        err = dev.AI_openStreaming(port, timeout)
 74        print(f"AI_openStreaming in port {port}, status: {err}")
 75
 76        ## Start AI streaming
 77        err = dev.AI_startStreaming(port, timeout)
 78        print(f"AI_startStreaming in port {port}, status: {err}")
 79
 80        ## Wait a while for data acquisition
 81        time.sleep(1) ## delay [s]
 82
 83        ## Close AI streaming
 84        err = dev.AI_closeStreaming(port, timeout)
 85        print(f"AI_closeStreaming in port {port}, status: {err}")
 86
 87        data_len = 1
 88        while data_len > 0:
 89            ## Read data acquisition
 90            ai_2Dlist = dev.AI_readStreaming(port, read_points, read_delay)
 91            print(f"Number of samples: {len(ai_2Dlist)}" )
 92
 93            ## Update data len
 94            data_len = len(ai_2Dlist)
 95
 96        ## Close AI
 97        err = dev.AI_close(port, timeout)
 98        print(f"AI_close in port {port}, status: {err}")
 99    except Exception as err:
100        pywpc.printGenericError(err)
101
102    ## Disconnect device
103    dev.disconnect()
104
105    ## Release device handle
106    dev.close()
107
108    return
109
110if __name__ == '__main__':
111    main()