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 USBDAQF1AD.
  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.USBDAQF1AD()
 35
 36    ## Connect to device
 37    try:
 38        dev.connect("default") ## 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 channel
 67        err = dev.AI_enableChannel(port, channel, timeout)
 68        print(f"AI_enableChannel in port {port}, status: {err}")
 69
 70        ## Set AI acquisition mode to N-samples mode (1)
 71        err = dev.AI_setMode(port, mode, timeout)
 72        print(f"AI_setMode {mode} in port {port}, status: {err}")
 73
 74        ## Set AI sampling rate
 75        err = dev.AI_setSamplingRate(port, sampling_rate, timeout)
 76        print(f"AI_setSamplingRate {sampling_rate} in port {port}, status: {err}")
 77
 78        ## Set AI # of samples
 79        err = dev.AI_setNumSamples(port, samples, timeout)
 80        print(f"AI_setNumSamples {samples} in port {port}, status: {err}")
 81
 82        ## Open AI streaming
 83        err = dev.AI_openStreaming(port, timeout)
 84        print(f"AI_openStreaming in port {port}, status: {err}")
 85
 86        ## Start AI streaming
 87        err = dev.AI_startStreaming(port, timeout)
 88        print(f"AI_startStreaming in port {port}, status: {err}")
 89
 90        ## Read AI
 91        ai_2Dlist = dev.AI_readStreaming(port, read_points, read_delay)
 92        print(f"Number of samples: {len(ai_2Dlist)}" )
 93
 94        ok = True
 95        for i, ai_list in enumerate(ai_2Dlist):
 96            ## Check for any missing data
 97            if len(ai_list) != channel:
 98                print(i, ai_list)
 99                ok = False
100        if ok:
101            print('Sample length OK')
102        else:
103            print('Error: at least 1 sample has wrong length')
104
105        ## Close AI streaming
106        err = dev.AI_closeStreaming(port, timeout)
107        print(f"AI_closeStreaming in port {port}, status: {err}")
108
109        ## Close AI
110        err = dev.AI_close(port, timeout)
111        print(f"AI_close in port {port}, status: {err}")
112    except Exception as err:
113        pywpc.printGenericError(err)
114
115    ## Disconnect device
116    dev.disconnect()
117
118    ## Release device handle
119    dev.close()
120
121    return
122
123if __name__ == '__main__':
124    main()