AI continuous multi slot

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