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 EthanA2.
  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
 11-------------------------------------------------------------------------------------
 12Please change correct serial number or IP and port number BEFORE you run example code.
 13
 14For other examples please check:
 15    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 16See README.md file to get detailed usage of this example.
 17
 18Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
 19'''
 20
 21## Python
 22import time
 23
 24## WPC
 25
 26from wpcsys import pywpc
 27
 28
 29def main():
 30    ## Get Python driver version
 31    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 32
 33    ## Create device handle
 34    dev = pywpc.EthanA2()
 35
 36    ## Connect to device
 37    try:
 38        dev.connect("192.168.1.110") ## 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 ## second
 52        timeout = 3 ## second
 53        
 54
 55        ## Get firmware model & version
 56        driver_info = dev.Sys_getDriverInfo(timeout)
 57        print("Model name: " + driver_info[0])
 58        print("Firmware version: " + driver_info[-1])
 59
 60        ## Open file with WPC_test.csv
 61        err = dev.Logger_openFile("WPC_test.csv")
 62        print(f"Logger_openFile, status: {err}")
 63
 64        ## Write header into CSV file
 65        err = dev.Logger_writeHeader(["CH0","CH1","CH2","CH3","CH4","CH5","CH6","CH7"])
 66        print(f"Logger_writeHeader, status: {err}")
 67
 68        ## Open AI
 69        err = dev.AI_open(port, timeout)
 70        print(f"AI_open in port {port}, status: {err}")
 71        
 72
 73        ## Set AI acquisition mode to continuous mode (2)
 74        err = dev.AI_setMode(port, mode, timeout)
 75        print(f"AI_setMode {mode} in port {port}, status: {err}")
 76
 77        ## Set AI sampling rate
 78        err = dev.AI_setSamplingRate(port, sampling_rate, timeout)
 79        print(f"AI_setSamplingRate {sampling_rate} in port {port}, status: {err}")
 80
 81        ## Open AI streaming
 82        err = dev.AI_openStreaming(port, timeout)
 83        print(f"AI_openStreaming in port {port}, status: {err}")
 84
 85        ## Start AI streaming
 86        err = dev.AI_startStreaming(port, timeout)
 87        print(f"AI_startStreaming in port {port}, status: {err}")
 88
 89        ## Wait a while for data acquisition
 90        time.sleep(1) ## delay [s]
 91
 92        ## Close AI streaming
 93        err = dev.AI_closeStreaming(port, timeout)
 94        print(f"AI_closeStreaming in port {port}, status: {err}")
 95
 96        data_len = 1
 97        while data_len > 0:
 98            ## Read data acquisition
 99            ai_2Dlist = dev.AI_readStreaming(port, read_points, read_delay)
100            print(f"Number of samples: {len(ai_2Dlist)}" )
101
102            ## Write data into CSV file
103            dev.Logger_write2DList(ai_2Dlist)
104
105            ## Update data len
106            data_len = len(ai_2Dlist)
107
108        ## Close AI
109        err = dev.AI_close(port, timeout)
110        print(f"AI_close in port {port}, status: {err}")
111    except Exception as err:
112        pywpc.printGenericError(err)
113
114    ## Disconnect device
115    dev.disconnect()
116
117    ## Release device handle
118    dev.close()
119
120    return
121
122if __name__ == '__main__':
123    main()