AI N samples once

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