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 STEM.
  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
 10If your product is "STEM", please invoke the function `Sys_setAIOMode_async`and `AI_enableCS_async`.
 11Example: AI_enableCS_async is {0, 2}
 12Subsequently, the returned value of AI_readOnDemand_async and AI_readStreaming_async will be displayed as follows.
 13data:
 14          CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7
 15          |                                     |                                      |
 16          |---------------- CS0-----------------|---------------- CS2------------------|
 17[sample0]
 18[sample1]
 19   .
 20   .
 21   .
 22[sampleN]
 23
 24-------------------------------------------------------------------------------------
 25Please change correct serial number or IP and port number BEFORE you run example code.
 26
 27For other examples please check:
 28    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 29See README.md file to get detailed usage of this example.
 30
 31Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 32'''
 33
 34## WPC
 35from wpcsys import pywpc
 36
 37## Python
 38import asyncio
 39import sys
 40sys.path.insert(0, 'src/')
 41
 42
 43async def main():
 44    ## Get Python driver version
 45    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 46
 47    ## Create device handle
 48    dev = pywpc.STEM()
 49
 50    ## Connect to device
 51    try:
 52        dev.connect("192.168.1.110")  ## Depend on your device
 53    except Exception as err:
 54        pywpc.printGenericError(err)
 55        ## Release device handle
 56        dev.close()
 57        return
 58
 59    try:
 60        ## Parameters setting
 61        slot = 1  ## Connect AIO module to slot
 62        mode = 2  ## 0: On demand, 1: N-samples, 2: Continuous
 63        sampling_rate = 200
 64        read_points = 200
 65        read_delay = 0.2  ## [sec]
 66        chip_select = [0, 1]
 67
 68        ## Get firmware model & version
 69        driver_info = await dev.Sys_getDriverInfo_async()
 70        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 71
 72        ## Get slot mode
 73        slot_mode = await dev.Sys_getMode_async(slot)
 74        print("Slot mode:", slot_mode)
 75
 76        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 77        if slot_mode != "AIO":
 78            err = await dev.Sys_setAIOMode_async(slot)
 79            print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
 80
 81        ## Get slot mode
 82        slot_mode = await dev.Sys_getMode_async(slot)
 83        print("Slot mode:", slot_mode)
 84
 85        ## Open AI
 86        err = await dev.AI_open_async(slot)
 87        print(f"AI_open_async in slot {slot}, status: {err}")
 88
 89        ## Enable CS
 90        err = await dev.AI_enableCS_async(slot, chip_select)
 91        print(f"AI_enableCS_async in slot {slot}, status: {err}")
 92
 93        ## Set AI acquisition mode to continuous mode (2)
 94        err = await dev.AI_setMode_async(slot, mode)
 95        print(f"AI_setMode_async {mode} in slot {slot}, status: {err}")
 96
 97        ## Set AI sampling rate
 98        err = await dev.AI_setSamplingRate_async(slot, sampling_rate)
 99        print(f"AI_setSamplingRate_async {sampling_rate} in slot {slot}, status: {err}")
100
101        ## Open AI streaming
102        err = await dev.AI_openStreaming_async(slot)
103        print(f"AI_openStreaming_async in slot {slot}, status: {err}")
104
105        ## Start AI streaming
106        err = await dev.AI_startStreaming_async(slot)
107        print(f"AI_startStreaming_async in slot {slot}, status: {err}")
108
109        ## Wait for acquisition
110        await asyncio.sleep(1)  ## delay [sec]
111
112        ## Close AI streaming
113        err = await dev.AI_closeStreaming_async(slot)
114        print(f"AI_closeStreaming_async in slot {slot}, status: {err}")
115
116        data_len = 1
117        while data_len > 0:
118            ## Read data acquisition
119            ai_2Dlist = await dev.AI_readStreaming_async(slot, read_points, read_delay)
120            print(f"number of samples = {len(ai_2Dlist)}")
121
122            ## Update data len
123            data_len = len(ai_2Dlist)
124
125        ## Close AI
126        err = await dev.AI_close_async(slot)
127        print(f"AI_close_async in slot {slot}, status: {err}")
128    except Exception as err:
129        pywpc.printGenericError(err)
130
131    finally:
132        ## Disconnect device
133        dev.disconnect()
134
135        ## Release device handle
136        dev.close()
137
138
139def main_for_spyder(*args):
140    if asyncio.get_event_loop().is_running():
141        return asyncio.create_task(main(*args)).result()
142    else:
143        return asyncio.run(main(*args))
144
145
146if __name__ == '__main__':
147    asyncio.run(main())  ## Use terminal
148    # await main()  ## Use Jupyter or IPython(>=7.0)
149    # main_for_spyder()  ## Use Spyder