AI N samples once

  1'''
  2AI - AI_N_samples_once.py with asynchronous 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 asyncio
 23
 24## WPC
 25
 26from wpcsys import pywpc
 27
 28
 29async def 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 = 0.5 ## second
 54
 55        ## Get firmware model & version
 56        driver_info = await dev.Sys_getDriverInfo_async()
 57        print("Model name: " + driver_info[0])
 58        print("Firmware version: " + driver_info[-1])
 59
 60        ## Open AI
 61        err = await dev.AI_open_async(port)
 62        print(f"AI_open_async in port {port}, status: {err}")
 63        
 64        ## Set AI channel
 65        err = await dev.AI_enableChannel_async(port, channel)
 66        print(f"AI_enableChannel_async in port {port}, status: {err}")
 67        ## Set AI acquisition mode to N-samples mode (1)
 68        err = await dev.AI_setMode_async(port, mode)
 69        print(f"AI_setMode_async {mode} in port {port}, status: {err}")
 70
 71        ## Set AI sampling rate
 72        err = await dev.AI_setSamplingRate_async(port, sampling_rate)
 73        print(f"AI_setSamplingRate_async {sampling_rate} in port {port}, status: {err}")
 74
 75        ## Set AI # of samples
 76        err = await dev.AI_setNumSamples_async(port, samples)
 77        print(f"AI_setNumSamples_async {samples} in port {port}, status: {err}")
 78
 79        ## Open AI streaming
 80        err = await dev.AI_openStreaming_async(port)
 81        print(f"AI_openStreaming_async in port {port}, status: {err}")
 82
 83        ## Start AI streaming
 84        err = await dev.AI_startStreaming_async(port)
 85        print(f"AI_startStreaming_async in port {port}, status: {err}")
 86
 87        ## Read AI
 88        ai_2Dlist = await dev.AI_readStreaming_async(port, read_points, read_delay)
 89        print(f"number of samples = {len(ai_2Dlist)}" )
 90
 91        ok = True
 92        for i, ai_list in enumerate(ai_2Dlist):
 93            ## Check for any missing data
 94            if len(ai_list) != channel:
 95                print(i, ai_list)
 96                ok = False
 97        if ok:
 98            print('OK')
 99        else:
100            print('NG')
101
102        ## Close AI streaming
103        err = await dev.AI_closeStreaming_async(port)
104        print(f"AI_closeStreaming_async in port {port}, status: {err}")
105
106        ## Close AI
107        err = await dev.AI_close_async(port)
108        print(f"AI_close_async in port {port}, status: {err}")
109    except Exception as err:
110        pywpc.printGenericError(err)
111
112    ## Disconnect device
113    dev.disconnect()
114
115    ## Release device handle
116    dev.close()
117
118    return
119
120def main_for_spyder(*args):
121    if asyncio.get_event_loop().is_running():
122        return asyncio.create_task(main(*args)).result()
123    else:
124        return asyncio.run(main(*args))
125if __name__ == '__main__':
126    asyncio.run(main()) ## Use terminal
127    # await main() ## Use Jupyter or IPython(>=7.0)
128    # main_for_spyder() ## Use Spyder