AI continuous with logger

  1'''
  2AI - AI_continuous_with_logger.py with synchronous mode.
  3
  4This example demonstrates the process of obtaining AI data in continuous mode with 8 channels from STEM.
  5Then, save data into CSV file.
  6
  7To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
  8Next, it outlines the procedure for reading and saving the streaming AI data.
  9Finally, it concludes by explaining how to close the AI.
 10
 11If your product is "STEM", please invoke the function `Sys_setAIOMode`and `AI_enableCS`.
 12Example: AI_enableCS is {0, 2}
 13Subsequently, the returned value of AI_readOnDemand and AI_readStreaming 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-2025 WPC Systems Ltd. All rights reserved.
 33'''
 34
 35## WPC
 36from wpcsys import pywpc
 37
 38## Python
 39import time
 40
 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 = 1  ## Connect AIO module to slot
 61        mode = 2  ## 0: On demand, 1: N-samples, 2: Continuous
 62        sampling_rate = 200
 63        read_points = 200
 64        read_delay = 0.2  ## [sec]
 65        timeout = 3  ## [sec]
 66        chip_select = [0, 1]
 67
 68        ## Get firmware model & version
 69        driver_info = dev.Sys_getDriverInfo(timeout)
 70        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 71
 72        ## Open file with WPC_test.csv
 73        err = dev.Logger_openFile("WPC_test.csv")
 74        print(f"Logger_openFile, status: {err}")
 75
 76        ## Write header into CSV file
 77        err = dev.Logger_writeHeader(["CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7"])
 78        print(f"Logger_writeHeader, status: {err}")
 79
 80        ## Get slot mode
 81        slot_mode = dev.Sys_getMode(slot, timeout)
 82        print("Slot mode:", slot_mode)
 83
 84        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 85        if slot_mode != "AIO":
 86            err = dev.Sys_setAIOMode(slot, timeout)
 87            print(f"Sys_setAIOMode in slot {slot}, status: {err}")
 88
 89        ## Get slot mode
 90        slot_mode = dev.Sys_getMode(slot, timeout)
 91        print("Slot mode:", slot_mode)
 92
 93        ## Open AI
 94        err = dev.AI_open(slot, timeout)
 95        print(f"AI_open in slot {slot}, status: {err}")
 96
 97        ## Enable CS
 98        err = dev.AI_enableCS(slot, chip_select, timeout)
 99        print(f"AI_enableCS in slot {slot}, status: {err}")
100
101        ## Set AI acquisition mode to continuous mode (2)
102        err = dev.AI_setMode(slot, mode, timeout)
103        print(f"AI_setMode {mode} in slot {slot}, status: {err}")
104
105        ## Set AI sampling rate
106        err = dev.AI_setSamplingRate(slot, sampling_rate, timeout)
107        print(f"AI_setSamplingRate {sampling_rate} in slot {slot}, status: {err}")
108
109        ## Open AI streaming
110        err = dev.AI_openStreaming(slot, timeout)
111        print(f"AI_openStreaming in slot {slot}, status: {err}")
112
113        ## Start AI streaming
114        err = dev.AI_startStreaming(slot, timeout)
115        print(f"AI_startStreaming in slot {slot}, status: {err}")
116
117        ## Wait a while for data acquisition
118        time.sleep(1)  ## delay [sec]
119
120        ## Close AI streaming
121        err = dev.AI_closeStreaming(slot, timeout)
122        print(f"AI_closeStreaming in slot {slot}, status: {err}")
123
124        data_len = 1
125        while data_len > 0:
126            ## Read data acquisition
127            ai_2Dlist = dev.AI_readStreaming(slot, read_points, read_delay)
128            print(f"number of samples = {len(ai_2Dlist)}")
129
130            ## Write data into CSV file
131            dev.Logger_write2DList(ai_2Dlist)
132
133            ## Update data len
134            data_len = len(ai_2Dlist)
135
136        ## Close AI
137        err = dev.AI_close(slot, timeout)
138        print(f"AI_close in slot {slot}, status: {err}")
139    except Exception as err:
140        pywpc.printGenericError(err)
141
142    finally:
143        ## Disconnect device
144        dev.disconnect()
145
146        ## Release device handle
147        dev.close()
148
149
150if __name__ == '__main__':
151    main()