AI continuous

  1'''
  2AI - AI_continuous.py with asynchronous mode.
  3
  4This example demonstrates the process of obtaining AI data in continuous mode with 8 channels from USBDAQF1AD.
  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 asyncio
 22
 23## WPC
 24
 25from wpcsys import pywpc
 26
 27
 28async def main():
 29    ## Get Python driver version
 30    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 31
 32    ## Create device handle
 33    dev = pywpc.USBDAQF1AD()
 34
 35    ## Connect to device
 36    try:
 37        dev.connect("default") ## 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        channel = 8
 52
 53        ## Get firmware model & version
 54        driver_info = await dev.Sys_getDriverInfo_async()
 55        print("Model name: " + driver_info[0])
 56        print("Firmware version: " + driver_info[-1])
 57
 58        ## Open AI
 59        err = await dev.AI_open_async(port)
 60        print(f"AI_open_async in port {port}, status: {err}")
 61        
 62        ## Set AI channel
 63        err = await dev.AI_enableChannel_async(port, channel)
 64        print(f"AI_enableChannel_async in port {port}, status: {err}")
 65
 66        ## Set AI acquisition mode to continuous mode (2)
 67        err = await dev.AI_setMode_async(port, mode)
 68        print(f"AI_setMode_async {mode} in port {port}, status: {err}")
 69
 70        ## Set AI sampling rate
 71        err = await dev.AI_setSamplingRate_async(port, sampling_rate)
 72        print(f"AI_setSamplingRate_async {sampling_rate} in port {port}, status: {err}")
 73
 74        ## Open AI streaming
 75        err = await dev.AI_openStreaming_async(port)
 76        print(f"AI_openStreaming_async in port {port}, status: {err}")
 77
 78        ## Start AI streaming
 79        err = await dev.AI_startStreaming_async(port)
 80        print(f"AI_startStreaming_async in port {port}, status: {err}")
 81
 82        ## Wait for acquisition
 83        await asyncio.sleep(1) ## delay [s]
 84
 85        ## Close AI streaming
 86        err = await dev.AI_closeStreaming_async(port)
 87        print(f"AI_closeStreaming_async in port {port}, status: {err}")
 88
 89        data_len = 1
 90        while data_len > 0:
 91            ## Read data acquisition
 92            ai_2Dlist = await dev.AI_readStreaming_async(port, read_points, read_delay)
 93            print(f"number of samples = {len(ai_2Dlist)}" )
 94
 95            ## Update data len
 96            data_len = len(ai_2Dlist)
 97
 98        ## Close AI
 99        err = await dev.AI_close_async(port)
100        print(f"AI_close_async in port {port}, status: {err}")
101    except Exception as err:
102        pywpc.printGenericError(err)
103
104    ## Disconnect device
105    dev.disconnect()
106
107    ## Release device handle
108    dev.close()
109
110    return
111
112def main_for_spyder(*args):
113    if asyncio.get_event_loop().is_running():
114        return asyncio.create_task(main(*args)).result()
115    else:
116        return asyncio.run(main(*args))
117if __name__ == '__main__':
118    asyncio.run(main()) ## Use terminal
119    # await main() ## Use Jupyter or IPython(>=7.0)
120    # main_for_spyder() ## Use Spyder