RTC trigger AI continuous

  1
  2'''
  3RTC_AI - RTC_trigger_AI_continuous.py with synchronous mode.
  4
  5This example demonstrates how to use RTC to trigger AI with continuous mode from EthanA2.
  6
  7-------------------------------------------------------------------------------------
  8Please change correct serial number or IP and port number BEFORE you run example code.
  9
 10For other examples please check:
 11    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 12See README.md file to get detailed usage of this example.
 13
 14Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
 15'''
 16## WPC
 17
 18from wpcsys import pywpc
 19import time
 20
 21def main():
 22    ## Get Python driver version
 23    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 24
 25    ## Create device handle
 26    dev = pywpc.EthanA2()
 27
 28    ## Connect to device
 29    try:
 30        dev.connect("192.168.1.110") ## Depend on your device
 31    except Exception as err:
 32        pywpc.printGenericError(err)
 33        ## Release device handle
 34        dev.close()
 35        return
 36
 37    try:
 38        ## Parameters setting
 39        port = 0 ## Depend on your device
 40        mode = 2 ## 1 : N-samples, 2 : Continuous
 41        trigger_mode = 1 ## 1 : Use RTC to start AI streaming
 42        sampling_rate = 1000
 43        read_points = 200
 44        read_delay = 0.5 ## second
 45        timeout = 3 ## second
 46        mode_alarm = 0
 47        year = 2024
 48        month = 4
 49        day = 2
 50        hour = 15
 51        minute = 8
 52        second = 50
 53
 54        ## Get firmware model & version
 55        driver_info = dev.Sys_getDriverInfo(timeout)
 56        print("Model name: " + driver_info[0])
 57        print("Firmware version: " + driver_info[-1])
 58
 59        ## Open AI
 60        err = dev.AI_open(port, timeout)
 61        print(f"AI_open in port {port}, status: {err}")
 62
 63        ## Set AI mode
 64        err = dev.AI_setMode(port, mode, timeout)
 65        print(f"AI_setMode {mode} in port {port}, status: {err}")
 66
 67        ## Set AI trigger mode
 68        err = dev.AI_setTriggerMode(port, trigger_mode, timeout)
 69        print(f"AI_setTriggerMode {trigger_mode} in port {port}, status: {err}")
 70
 71        ## Set AI sampling rate
 72        err = dev.AI_setSamplingRate(port, sampling_rate, timeout)
 73        print(f"AI_setSamplingRate {sampling_rate} in port {port}, status: {err}")
 74
 75        ## Set RTC
 76        err = dev.Sys_setRTC(year, month, day, hour, minute, second-10, timeout)
 77        print(f"Set RTC to {year}-{month}-{day}, {hour}:{minute}:{second-10}, status: {err}")
 78
 79        ## Open AI streaming
 80        err = dev.AI_openStreaming(port, timeout)
 81        print(f"AI_openStreaming in port {port}, status: {err}")
 82
 83        ## Start RTC alarm after 10 seconds
 84        err = dev.Sys_startRTCAlarm(mode_alarm, day, hour, minute, second, timeout)
 85        print(f"Alarm RTC to {year}-{month}-{day}, {hour}:{minute}:{second}, status: {err}")
 86
 87        stop_flag = 1
 88        for i in range(15):
 89            ## Read data acquisition
 90            ai_2Dlist = dev.AI_readStreaming(port, read_points, read_delay)
 91            print(f"len: {len(ai_2Dlist)}, {dev.Sys_getRTC()}" )
 92            if len(ai_2Dlist)> 0 and stop_flag == 1 :
 93                ## Close AI streaming
 94                err = dev.AI_closeStreaming(port, timeout)
 95                print(f"AI_closeStreaming in port {port}, status: {err}")
 96                stop_flag = 0
 97            time.sleep(1)
 98
 99        ## Close AI
100        err = dev.AI_close(port, timeout)
101        print(f"AI_close in port {port}, status: {err}")
102    except Exception as err:
103        pywpc.printGenericError(err)
104
105    ## Disconnect device
106    dev.disconnect()
107
108    ## Release device handle
109    dev.close()
110
111    return
112if __name__ == '__main__':
113    main()