AI continuous

  1'''
  2AI - AI_continuous.py with synchronous 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`and `AI_enableCS`.
 11Example: AI_enableCS is {0, 2}
 12Subsequently, the returned value of AI_readOnDemand and AI_readStreaming 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 time
 39
 40
 41def main():
 42    ## Get Python driver version
 43    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 44
 45    ## Create device handle
 46    dev = pywpc.STEM()
 47
 48    ## Connect to device
 49    try:
 50        dev.connect("192.168.1.110")  ## Depend on your device
 51    except Exception as err:
 52        pywpc.printGenericError(err)
 53        ## Release device handle
 54        dev.close()
 55        return
 56
 57    try:
 58        ## Parameters setting
 59        slot = 1  ## Connect AIO module to slot
 60        mode = 2  ## 0: On demand, 1: N-samples, 2: Continuous
 61        sampling_rate = 200
 62        read_points = 200
 63        read_delay = 0.2  ## [sec]
 64        timeout = 3  ## [sec]
 65        chip_select = [0, 1]
 66
 67        ## Get firmware model & version
 68        driver_info = dev.Sys_getDriverInfo(timeout)
 69        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 70
 71        ## Get slot mode
 72        slot_mode = dev.Sys_getMode(slot, timeout)
 73        print("Slot mode:", slot_mode)
 74
 75        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 76        if slot_mode != "AIO":
 77            err = dev.Sys_setAIOMode(slot, timeout)
 78            print(f"Sys_setAIOMode in slot {slot}, status: {err}")
 79
 80        ## Get slot mode
 81        slot_mode = dev.Sys_getMode(slot, timeout)
 82        print("Slot mode:", slot_mode)
 83
 84        ## Open AI
 85        err = dev.AI_open(slot, timeout)
 86        print(f"AI_open in slot {slot}, status: {err}")
 87
 88        ## Enable CS
 89        err = dev.AI_enableCS(slot, chip_select, timeout)
 90        print(f"AI_enableCS in slot {slot}, status: {err}")
 91
 92        ## Set AI acquisition mode to continuous mode (2)
 93        err = dev.AI_setMode(slot, mode, timeout)
 94        print(f"AI_setMode {mode} in slot {slot}, status: {err}")
 95
 96        ## Set AI sampling rate
 97        err = dev.AI_setSamplingRate(slot, sampling_rate, timeout)
 98        print(f"AI_setSamplingRate {sampling_rate} in slot {slot}, status: {err}")
 99
100        ## Open AI streaming
101        err = dev.AI_openStreaming(slot, timeout)
102        print(f"AI_openStreaming in slot {slot}, status: {err}")
103
104        ## Start AI streaming
105        err = dev.AI_startStreaming(slot, timeout)
106        print(f"AI_startStreaming in slot {slot}, status: {err}")
107
108        ## Wait a while for data acquisition
109        time.sleep(1)  ## delay [sec]
110
111        ## Stop AI
112        err = dev.AI_stop(slot, timeout)
113        print(f"AI_stop in slot {slot}, status: {err}")
114
115        data_len = 1
116        while data_len > 0:
117            ## Read data acquisition
118            ai_2Dlist = dev.AI_readStreaming(slot, read_points, read_delay)
119            print(f"number of samples = {len(ai_2Dlist)}")
120
121            ## Update data len
122            data_len = len(ai_2Dlist)
123
124        ## Close AI streaming
125        err = dev.AI_closeStreaming(slot, timeout)
126        print(f"AI_closeStreaming in slot {slot}, status: {err}")
127    except Exception as err:
128        pywpc.printGenericError(err)
129
130    finally:
131        ## Disconnect device
132        dev.disconnect()
133
134        ## Release device handle
135        dev.close()
136
137
138if __name__ == '__main__':
139    main()