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 WifiDAQF4A.
  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-2025 WPC Systems Ltd. All rights reserved.
 18'''
 19
 20## WPC
 21from wpcsys import pywpc
 22
 23## Python
 24import asyncio
 25import sys
 26sys.path.insert(0, 'src/')
 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.WifiDAQF4A()
 35
 36    ## Connect to device
 37    try:
 38        dev.connect("192.168.5.38")  ## 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 = 2  ## 0: On demand, 1: N-samples, 2: Continuous
 49        sampling_rate = 200
 50        read_points = 200
 51        read_delay = 0.2  ## [sec]
 52        
 53
 54        ## Get firmware model & version
 55        driver_info = await dev.Sys_getDriverInfo_async()
 56        print(f"Model name: {driver_info[0]}, 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
 63        ## Set AI acquisition mode to continuous mode (2)
 64        err = await dev.AI_setMode_async(port, mode)
 65        print(f"AI_setMode_async {mode} in port {port}, status: {err}")
 66
 67        ## Set AI sampling rate
 68        err = await dev.AI_setSamplingRate_async(port, sampling_rate)
 69        print(f"AI_setSamplingRate_async {sampling_rate} in port {port}, status: {err}")
 70
 71        ## Open AI streaming
 72        err = await dev.AI_openStreaming_async(port)
 73        print(f"AI_openStreaming_async in port {port}, status: {err}")
 74
 75        ## Start AI streaming
 76        err = await dev.AI_startStreaming_async(port)
 77        print(f"AI_startStreaming_async in port {port}, status: {err}")
 78
 79        ## Wait for acquisition
 80        await asyncio.sleep(1)  ## delay [sec]
 81
 82        ## Close AI streaming
 83        err = await dev.AI_closeStreaming_async(port)
 84        print(f"AI_closeStreaming_async in port {port}, status: {err}")
 85
 86        data_len = 1
 87        while data_len > 0:
 88            ## Read data acquisition
 89            ai_2Dlist = await dev.AI_readStreaming_async(port, read_points, read_delay)
 90            print(f"number of samples = {len(ai_2Dlist)}")
 91
 92            ## Update data len
 93            data_len = len(ai_2Dlist)
 94
 95        ## Close AI
 96        err = await dev.AI_close_async(port)
 97        print(f"AI_close_async in port {port}, status: {err}")
 98    except Exception as err:
 99        pywpc.printGenericError(err)
100
101    finally:
102        ## Disconnect device
103        dev.disconnect()
104
105        ## Release device handle
106        dev.close()
107
108
109def main_for_spyder(*args):
110    if asyncio.get_event_loop().is_running():
111        return asyncio.create_task(main(*args)).result()
112    else:
113        return asyncio.run(main(*args))
114
115
116if __name__ == '__main__':
117    asyncio.run(main())  ## Use terminal
118    # await main()  ## Use Jupyter or IPython(>=7.0)
119    # main_for_spyder()  ## Use Spyder