#================================================================
#================================================================
# API-AIO(WDM)
# Infinite sampling using device buffer sample
# Condition setting file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import ctypes
import ctypes.wintypes
import caio
from CommonFunction import *


#========================================
# Function name    SamplingSetting
# Parameter        id    Id get by AioInit
# Details          Function that displays and processes the condition set menu
# Return value     None
#========================================
def SamplingSetting(id):
    error_buf = ctypes.create_string_buffer(256)    # Error code string
    lret = ctypes.c_long()                          # For return code
    ai_range = ctypes.c_short()                     # Range
    ai_start_trigger = ctypes.c_short()             # Conversion start condition
    ai_start_level_channel = ctypes.c_short()       # Start Level comparison channel
    ai_start_level_value = ctypes.c_float()         # Start Level comparison data
    ai_start_level_direction = ctypes.c_short()     # Start Level comparison direction
    ai_start_inrange_channel = ctypes.c_short()     # Start in range comparison channel
    ai_start_inrange_upper = ctypes.c_float()       # Start in range comparison upper limit value
    ai_start_inrange_lower = ctypes.c_float()       # Start in range comparison lower limit value
    ai_start_inrange_state = ctypes.c_long()        # The number of samplings within the state-held period after the condition of start in range comparison is satisfied
    ai_start_outrange_channel = ctypes.c_short()    # Start out range comparison channel
    ai_start_outrange_upper = ctypes.c_float()      # Start out range comparison upper limit value
    ai_start_outrange_lower = ctypes.c_float()      # Start out range comparison lower limit value
    ai_start_outrange_state = ctypes.c_long()       # The number of samplings within the state-held period after the condition of start out range comparison is satisfied
    ai_clock_type = ctypes.c_short()                # Clock type
    ai_sampling_clock = ctypes.c_float()            # Internal clock(usec)
    ai_clock_edge = ctypes.c_short()                # External clock input timing
    ai_scan_clock = ctypes.c_float()                # Conversion speed
    ai_stop_trigger = ctypes.c_short()              # Conversion stop condition
    ai_stop_level_channel = ctypes.c_short()        # Stop Level comparison channel
    ai_stop_level_value = ctypes.c_float()          # Stop Level comparison data
    ai_stop_level_direction = ctypes.c_short()      # Stop Level comparison direction
    ai_stop_inrange_channel = ctypes.c_short()      # Stop in range comparison channel
    ai_stop_inrange_upper = ctypes.c_float()        # Stop in range comparison upper limit value
    ai_stop_inrange_lower = ctypes.c_float()        # Stop in range comparison lower limit value
    ai_stop_inrange_state = ctypes.c_long()         # The number of samplings within the state-held period after the condition of stop in range comparison is satisfied
    ai_stop_outrange_channel = ctypes.c_short()     # Stop out range comparison channel
    ai_stop_outrange_upper = ctypes.c_float()       # Stop out range comparison upper limit value
    ai_stop_outrange_lower = ctypes.c_float()       # Stop out range comparison lower limit value
    ai_stop_outrange_state = ctypes.c_long()        # The number of samplings within the state-held period after the condition of stop out range comparison is satisfied
    global g_lock                                   # Mutex
    global g_info_flag                              # A flag showing the total sampling count, sampling count, status in the callback

    #----------------------------------------
    # Initialize the variable
    #----------------------------------------
    ai_start_level_channel.value = 0
    ai_start_inrange_channel.value = 0
    ai_start_outrange_channel.value = 0
    ai_stop_level_channel.value = 0
    ai_stop_inrange_channel.value = 0
    ai_stop_outrange_channel.value = 0
    #----------------------------------------
    # Loop that wait input
    #----------------------------------------
    while True:
        #----------------------------------------
        # Get setting parameters
        #----------------------------------------
        #--------------------
        # Get the range value
        #--------------------
        lret.value = caio.AioGetAiRange(id, 0, ctypes.byref(ai_range))
        #--------------------
        # Get the conversion start condition
        #--------------------
        lret.value = caio.AioGetAiStartTrigger(id, ctypes.byref(ai_start_trigger))
        if lret.value != 0:
            ai_start_trigger_string = "-"
        else:
            ai_start_trigger_string = GetStartTriggerString(ai_start_trigger)
        #--------------------
        # For start condition is Level comparison
        #--------------------
        if ai_start_trigger.value == 3:
            lret.value = caio.AioGetAiStartLevelEx(id, ai_start_level_channel, ctypes.byref(ai_start_level_value), ctypes.byref(ai_start_level_direction))
            if lret.value != 0:
                ai_start_level_channel_string = "-"
                ai_start_level_value_string = "-"
                ai_start_level_direction_string = "-"
            else:
                ai_start_level_channel_string = f"{ai_start_level_channel.value}"
                if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                    ai_start_level_value_string = f"{ai_start_level_value.value}[mA]"
                else:
                    ai_start_level_value_string = f"{ai_start_level_value.value}[V]"
                ai_start_level_direction_string = GetStartLevelDirectionString(ai_start_level_direction)
        #--------------------
        # For start condition is In Range comparison
        #--------------------
        elif ai_start_trigger.value == 4:
            lret.value = caio.AioGetAiStartInRangeEx(id, ai_start_inrange_channel, ctypes.byref(ai_start_inrange_upper), ctypes.byref(ai_start_inrange_lower), ctypes.byref(ai_start_inrange_state))
            if lret.value != 0:
                ai_start_inrange_channel_string = "-"
                ai_start_inrange_upper_string = "-"
                ai_start_inrange_lower_string = "-"
                ai_start_inrange_state_string = "-"
            else:
                ai_start_inrange_channel_string = f"{ai_start_inrange_channel.value}"
                if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                    ai_start_inrange_upper_string = f"{ai_start_inrange_upper.value}[mA]"
                    ai_start_inrange_lower_string = f"{ai_start_inrange_lower.value}[mA]"
                else:
                    ai_start_inrange_upper_string = f"{ai_start_inrange_upper.value}[V]"
                    ai_start_inrange_lower_string = f"{ai_start_inrange_lower.value}[V]"
                ai_start_inrange_state_string = f"{ai_start_inrange_state.value}"
        #--------------------
        # For start condition is Out Range comparison
        #--------------------
        elif ai_start_trigger.value == 5:
            lret.value = caio.AioGetAiStartOutRangeEx(id, ai_start_outrange_channel, ctypes.byref(ai_start_outrange_upper), ctypes.byref(ai_start_outrange_lower), ctypes.byref(ai_start_outrange_state))
            if lret.value != 0:
                ai_start_outrange_channel_string = "-"
                ai_start_outrange_upper_string = "-"
                ai_start_outrange_lower_string = "-"
                ai_start_outrange_state_string = "-"
            else:
                ai_start_outrange_channel_string = f"{ai_start_outrange_channel.value}"
                if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                    ai_start_outrange_upper_string = f"{ai_start_outrange_upper.value}[mA]"
                    ai_start_outrange_lower_string = f"{ai_start_outrange_lower.value}[mA]"
                else:
                    ai_start_outrange_upper_string = f"{ai_start_outrange_upper.value}[V]"
                    ai_start_outrange_lower_string = f"{ai_start_outrange_lower.value}[V]"
                ai_start_outrange_state_string = f"{ai_start_outrange_state.value}"
        #--------------------
        # Get the sampling clock condition
        #--------------------
        lret.value = caio.AioGetAiClockType(id, ctypes.byref(ai_clock_type))
        if lret.value != 0:
            ai_clock_type_string = "-"
        else:
            ai_clock_type_string = GetClockTypeString(ai_clock_type)
        #--------------------
        # For sampling clock condition is Internal clock
        #--------------------
        if ai_clock_type.value == 0:
            #--------------------
            # Get the sampling clock period
            #--------------------
            lret.value = caio.AioGetAiSamplingClock(id, ctypes.byref(ai_sampling_clock))
            if lret.value != 0:
                ai_sampling_clock_string = "-"
            else:
                ai_sampling_clock_string = f"{ai_sampling_clock.value}[usec]"
        #--------------------
        # For sampling clock condition is External clock
        #--------------------
        elif ai_clock_type.value == 1:
            #--------------------
            # Get the timing of external clock input
            #--------------------
            lret.value = caio.AioGetAiClockEdge(id, ctypes.byref(ai_clock_edge))
            if lret.value != 0:
                ai_clock_edge_string = "-"
            else:
                ai_clock_edge_string = GetClockEdgeString(ai_clock_edge)
        #--------------------
        # Get the conversion speed
        #--------------------
        lret.value = caio.AioGetAiScanClock(id, ctypes.byref(ai_scan_clock))
        if lret.value != 0:
            ai_scan_clock_string = "-"
        else:
            ai_scan_clock_string = f"{ai_scan_clock.value}[usec]"
        #--------------------
        # Get the conversion stop condition
        #--------------------
        lret.value = caio.AioGetAiStopTrigger(id, ctypes.byref(ai_stop_trigger))
        if lret.value != 0:
            ai_stop_trigger_string = "-"
        else:
            ai_stop_trigger_string = GetStopTriggerString(ai_stop_trigger)
        #--------------------
        # For stop condition is 'Stop conversion by the specified times', set 'Command' as the conversion stop condition and retrieve it
        #--------------------
        if ai_stop_trigger.value == 0:
            lret.value = caio.AioSetAiStopTrigger(id, 4)
            if lret.value == 0:
                #--------------------
                # Get the conversion stop condition
                #--------------------
                lret.value = caio.AioGetAiStopTrigger(id, ctypes.byref(ai_stop_trigger))
                if lret.value != 0:
                    ai_stop_trigger_string = "-"
                else:
                    ai_stop_trigger_string = GetStopTriggerString(ai_stop_trigger)
        #--------------------
        # For stop condition is Level comparison
        #--------------------
        if ai_stop_trigger.value == 3:
            lret.value = caio.AioGetAiStopLevelEx(id, ai_stop_level_channel, ctypes.byref(ai_stop_level_value), ctypes.byref(ai_stop_level_direction))
            if lret.value != 0:
                ai_stop_level_channel_string = "-"
                ai_stop_level_value_string = "-"
                ai_stop_level_direction_string = "-"
            else:
                ai_stop_level_channel_string = f"{ai_stop_level_channel.value}"
                if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                    ai_stop_level_value_string = f"{ai_stop_level_value.value}[mA]"
                else:
                    ai_stop_level_value_string = f"{ai_stop_level_value.value}[V]"
                ai_stop_level_direction_string = GetStopLevelDirectionString(ai_stop_level_direction)
        #--------------------
        # For stop condition is In Range comparison
        #--------------------
        elif ai_stop_trigger.value == 5:
            lret.value = caio.AioGetAiStopInRangeEx(id, ai_stop_inrange_channel, ctypes.byref(ai_stop_inrange_upper), ctypes.byref(ai_stop_inrange_lower), ctypes.byref(ai_stop_inrange_state))
            if lret.value != 0:
                ai_stop_inrange_channel_string = "-"
                ai_stop_inrange_upper_string = "-"
                ai_stop_inrange_lower_string = "-"
                ai_stop_inrange_state_string = "-"
            else:
                ai_stop_inrange_channel_string = f"{ai_stop_inrange_channel.value}"
                if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                    ai_stop_inrange_upper_string = f"{ai_stop_inrange_upper.value}[mA]"
                    ai_stop_inrange_lower_string = f"{ai_stop_inrange_lower.value}[mA]"
                else:
                    ai_stop_inrange_upper_string = f"{ai_stop_inrange_upper.value}[V]"
                    ai_stop_inrange_lower_string = f"{ai_stop_inrange_lower.value}[V]"
                ai_stop_inrange_state_string = f"{ai_stop_inrange_state.value}"
        #--------------------
        # For stop condition is Out Range comparison
        #--------------------
        elif ai_stop_trigger.value == 6:
            lret.value = caio.AioGetAiStopOutRangeEx(id, ai_stop_outrange_channel, ctypes.byref(ai_stop_outrange_upper), ctypes.byref(ai_stop_outrange_lower), ctypes.byref(ai_stop_outrange_state))
            if lret.value != 0:
                ai_stop_outrange_channel_string = "-"
                ai_stop_outrange_upper_string = "-"
                ai_stop_outrange_lower_string = "-"
                ai_stop_outrange_state_string = "-"
            else:
                ai_stop_outrange_channel_string = f"{ai_stop_outrange_channel.value}"
                if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                    ai_stop_outrange_upper_string = f"{ai_stop_outrange_upper.value}[mA]"
                    ai_stop_outrange_lower_string = f"{ai_stop_outrange_lower.value}[mA]"
                else:
                    ai_stop_outrange_upper_string = f"{ai_stop_outrange_upper.value}[V]"
                    ai_stop_outrange_lower_string = f"{ai_stop_outrange_lower.value}[V]"
                ai_stop_outrange_state_string = f"{ai_stop_outrange_state.value}"
        #----------------------------------------
        # Display the setting parameter
        #----------------------------------------
        print("========================================")
        print("Setting Parameter")
        print("========================================")
        print(f"Start Condition                : {ai_start_trigger_string}")
        if ai_start_trigger.value == 3:
            print(f"Comparison Channel             : {ai_start_level_channel_string}")
            print(f"Comparison Level               : {ai_start_level_value_string}")
            print(f"Comparison Direction           : {ai_start_level_direction_string}")
        elif ai_start_trigger.value == 4:
            print(f"Comparison Channel             : {ai_start_inrange_channel_string}")
            print(f"Comparison Upper Limit         : {ai_start_inrange_upper_string}")
            print(f"Comparison Lower Limit         : {ai_start_inrange_lower_string}")
            print(f"State-holding Times            : {ai_start_inrange_state_string}")
        elif ai_start_trigger.value == 5:
            print(f"Comparison Channel             : {ai_start_outrange_channel_string}")
            print(f"Comparison Upper Limit         : {ai_start_outrange_upper_string}")
            print(f"Comparison Lower Limit         : {ai_start_outrange_lower_string}")
            print(f"State-holding Times            : {ai_start_outrange_state_string}")
        print(f"Sampling Clock Condition       : {ai_clock_type_string}")
        if ai_clock_type.value == 0:
            print(f"Sampling Clock Period          : {ai_sampling_clock_string}")
        elif ai_clock_type.value == 1:
            print(f"Timing of External Clock Input : {ai_clock_edge_string}")
        print(f"Conversion Speed               : {ai_scan_clock_string}")
        print(f"Stop Condition                 : {ai_stop_trigger_string}")
        if ai_stop_trigger.value == 3:
            print(f"Comparison Channel             : {ai_stop_level_channel_string}")
            print(f"Comparison Level               : {ai_stop_level_value_string}")
            print(f"Comparison Direction           : {ai_stop_level_direction_string}")
        elif ai_stop_trigger.value == 5:
            print(f"Comparison Channel             : {ai_stop_inrange_channel_string}")
            print(f"Comparison Upper Limit         : {ai_stop_inrange_upper_string}")
            print(f"Comparison Lower Limit         : {ai_stop_inrange_lower_string}")
            print(f"State-holding Times            : {ai_stop_inrange_state_string}")
        elif ai_stop_trigger.value == 6:
            print(f"Comparison Channel             : {ai_stop_outrange_channel_string}")
            print(f"Comparison Upper Limit         : {ai_stop_outrange_upper_string}")
            print(f"Comparison Lower Limit         : {ai_stop_outrange_lower_string}")
            print(f"State-holding Times            : {ai_stop_outrange_state_string}")
        #----------------------------------------
        # Display the command
        #----------------------------------------
        print("========================================")
        print("Menu(Top > Condition Set)")
        print("========================================")
        print("1 : Start Condition")
        print("2 : Sampling Clock Condition")
        print("3 : Conversion Speed Condition")
        print("4 : Stop Condition")
        print("b : Back")
        print("Please input the command : ", end='', flush=True)
        #----------------------------------------
        # Unlock
        #----------------------------------------
        g_lock.release()
        #----------------------------------------
        # Waiting for input
        #----------------------------------------
        buf = input()
        #----------------------------------------
        # Lock
        #----------------------------------------
        g_lock.acquire()
        #----------------------------------------
        # If the total sampling count, sampling count, status are displayed in the callback, output a new line
        #----------------------------------------
        if g_info_flag.value == 1:
            print("")
            g_info_flag.value = 0
        #----------------------------------------
        # Distinguish the command
        #----------------------------------------
        #--------------------
        # Start Condition
        #--------------------
        if buf == "1":
            print("\n----------------------------------------")
            print("Start the start condition setting")
            print("----------------------------------------")
            #----------------------------------------
            # Input the start condition
            #----------------------------------------
            print("----------------------------------------")
            print("Parameter(Start condition)")
            print("----------------------------------------")
            print("0 : Software")
            print("1 : External trigger rising edge")
            print("2 : External trigger falling edge")
            print("3 : Level comparison")
            print("4 : In Range comparison")
            print("5 : Out Range comparison")
            while True:
                print("Please input the start condition : ", end='', flush=True)
                #----------------------------------------
                # Unlock
                #----------------------------------------
                g_lock.release()
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Lock
                #----------------------------------------
                g_lock.acquire()
                #----------------------------------------
                # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                #----------------------------------------
                if g_info_flag.value == 1:
                    print("")
                    g_info_flag.value = 0
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            ai_start_trigger = ctypes.c_short(int(buf))
            #----------------------------------------
            # Set the start condition
            #----------------------------------------
            lret.value = caio.AioSetAiStartTrigger(id, ai_start_trigger)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_buf)
                print("\nStart Condition Error")
                print(f"AioSetAiStartTrigger Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                continue
            #----------------------------------------
            # For start condition is Level comparison
            #----------------------------------------
            if ai_start_trigger.value == 3:
                #----------------------------------------
                # Input the comparison channel
                #----------------------------------------
                print("")
                while True:
                    print("Please input the comparison channel : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_start_level_channel = ctypes.c_short(int(buf))
                #----------------------------------------
                # Input the comparison level
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison level[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison level[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_start_level_value = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the comparison direction
                #----------------------------------------
                print("\n----------------------------------------")
                print("Parameter(Comparison direction)")
                print("----------------------------------------")
                print("0 : Both edges")
                print("1 : Rising edge")
                print("2 : Falling edge")
                while True:
                    print("Please input the comparison direction : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_start_level_direction = ctypes.c_short(int(buf))
                #----------------------------------------
                # Set the level comparison
                #----------------------------------------
                lret.value = caio.AioSetAiStartLevelEx(id, ai_start_level_channel, ai_start_level_value, ai_start_level_direction)
                if lret.value != 0:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nStart Condition Error")
                    print(f"AioSetAiStartLevelEx Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            #----------------------------------------
            # For start condition is In Range comparison
            #----------------------------------------
            elif ai_start_trigger.value == 4:
                #----------------------------------------
                # Input the comparison channel
                #----------------------------------------
                print("")
                while True:
                    print("Please input the comparison channel : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_start_inrange_channel = ctypes.c_short(int(buf))
                #----------------------------------------
                # Input the comparison upper limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison upper limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison upper limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_start_inrange_upper = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the comparison lower limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison lower limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison lower limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_start_inrange_lower = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the state-holding times
                #----------------------------------------
                print("")
                while True:
                    print("Please input the state-holding times : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_start_inrange_state = ctypes.c_long(int(buf))
                #----------------------------------------
                # Set the in range comparison
                #----------------------------------------
                lret.value = caio.AioSetAiStartInRangeEx(id, ai_start_inrange_channel, ai_start_inrange_upper, ai_start_inrange_lower, ai_start_inrange_state)
                if lret.value != 0:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nStart Condition Error")
                    print(f"AioSetAiStartInRangeEx Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            #----------------------------------------
            # For start condition is Out Range comparison
            #----------------------------------------
            elif ai_start_trigger.value == 5:
                #----------------------------------------
                # Input the comparison channel
                #----------------------------------------
                print("")
                while True:
                    print("Please input the comparison channel : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_start_outrange_channel = ctypes.c_short(int(buf))
                #----------------------------------------
                # Input the comparison upper limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison upper limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison upper limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_start_outrange_upper = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the comparison lower limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison lower limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison lower limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_start_outrange_lower = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the state-holding times
                #----------------------------------------
                print("")
                while True:
                    print("Please input the state-holding times : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_start_outrange_state = ctypes.c_long(int(buf))
                #----------------------------------------
                # Set the out range comparison
                #----------------------------------------
                lret.value = caio.AioSetAiStartOutRangeEx(id, ai_start_outrange_channel, ai_start_outrange_upper, ai_start_outrange_lower, ai_start_outrange_state)
                if lret.value != 0:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nStart Condition Error")
                    print(f"AioSetAiStartOutRangeEx Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            print("\nStart Condition : Successful completion\n")
        #--------------------
        # Sampling Clock Condition
        #--------------------
        elif buf == "2":
            print("\n----------------------------------------")
            print("Start the sampling clock condition setting")
            print("----------------------------------------")
            #----------------------------------------
            # Input the sampling clock type
            #----------------------------------------
            print("----------------------------------------")
            print("Parameter(Sampling clock type)")
            print("----------------------------------------")
            print("0 : Internal clock")
            print("1 : External clock")
            while True:
                print("Please input the sampling clock type : ", end='', flush=True)
                #----------------------------------------
                # Unlock
                #----------------------------------------
                g_lock.release()
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Lock
                #----------------------------------------
                g_lock.acquire()
                #----------------------------------------
                # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                #----------------------------------------
                if g_info_flag.value == 1:
                    print("")
                    g_info_flag.value = 0
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            ai_clock_type = ctypes.c_short(int(buf))
            #----------------------------------------
            # Set the sampling clock type
            #----------------------------------------
            lret.value = caio.AioSetAiClockType(id, ai_clock_type)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_buf)
                print("\nSampling Clock Condition Error")
                print(f"AioSetAiClockType Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                continue
            #----------------------------------------
            # For sampling clock type is Internal clock
            #----------------------------------------
            if ai_clock_type.value == 0:
                #----------------------------------------
                # Input the sampling clock period
                #----------------------------------------
                print("")
                while True:
                    print("Please input the sampling clock period[usec] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_sampling_clock = ctypes.c_float(float(buf))
                #----------------------------------------
                # Set the sampling clock period
                #----------------------------------------
                lret.value = caio.AioSetAiSamplingClock(id, ai_sampling_clock)
                if lret.value != 0:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nSampling Clock Condition Error")
                    print(f"AioSetAiSamplingClock Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            #----------------------------------------
            # For sampling clock type is External clock
            #----------------------------------------
            elif ai_clock_type.value == 1:
                #----------------------------------------
                # Input the timing of external clock input
                #----------------------------------------
                print("\n----------------------------------------")
                print("Parameter(Timing of external clock input)")
                print("----------------------------------------")
                print("0 : External input falling edge")
                print("1 : External input rising edge")
                while True:
                    print("Please input the timing of external clock input : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_clock_edge = ctypes.c_short(int(buf))
                #----------------------------------------
                # Set the timing of external clock input
                #----------------------------------------
                lret.value = caio.AioSetAiClockEdge(id, ai_clock_edge)
                #--------------------
                # Exit the process if not completed successfully
                #--------------------
                if lret.value != 0 and lret.value != 20001:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nSampling Clock Condition Error")
                    print(f"AioSetAiClockEdge Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            print("\nSampling Clock Condition : Successful completion\n")
        #--------------------
        # Conversion Speed Condition
        #--------------------
        elif buf == "3":
            #----------------------------------------
            # Input the conversion speed
            #----------------------------------------
            print("")
            while True:
                print("Please input the conversion speed[usec] : ", end='', flush=True)
                #----------------------------------------
                # Unlock
                #----------------------------------------
                g_lock.release()
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Lock
                #----------------------------------------
                g_lock.acquire()
                #----------------------------------------
                # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                #----------------------------------------
                if g_info_flag.value == 1:
                    print("")
                    g_info_flag.value = 0
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            ai_scan_clock = ctypes.c_float(float(buf))
            #----------------------------------------
            # Set the conversion speed
            #----------------------------------------
            lret.value = caio.AioSetAiScanClock(id, ai_scan_clock)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_buf)
                print("\nConversion Speed Condition Error")
                print(f"AioSetAiScanClock Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                continue
            print("\nConversion Speed Condition : Successful completion\n")
        #--------------------
        # Stop Condition
        #--------------------
        elif buf == "4":
            print("\n----------------------------------------")
            print("Start the stop condition setting")
            print("----------------------------------------")
            #----------------------------------------
            # Input the stop condition
            #----------------------------------------
            print("----------------------------------------")
            print("Parameter(Stop condition)")
            print("----------------------------------------")
            print("1 : External trigger rising edge")
            print("2 : External trigger falling edge")
            print("3 : Level comparison")
            print("4 : Command")
            print("5 : In Range comparison")
            print("6 : Out Range comparison")
            while True:
                print("Please input the stop condition : ", end='', flush=True)
                #----------------------------------------
                # Unlock
                #----------------------------------------
                g_lock.release()
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Lock
                #----------------------------------------
                g_lock.acquire()
                #----------------------------------------
                # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                #----------------------------------------
                if g_info_flag.value == 1:
                    print("")
                    g_info_flag.value = 0
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            ai_stop_trigger = ctypes.c_short(int(buf))
            #----------------------------------------
            # Set the stop condition
            #----------------------------------------
            lret.value = caio.AioSetAiStopTrigger(id, ai_stop_trigger)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_buf)
                print("\nStop Condition Error")
                print(f"AioSetAiStopTrigger Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                continue
            #----------------------------------------
            # For stop condition is Level comparison
            #----------------------------------------
            if ai_stop_trigger.value == 3:
                #----------------------------------------
                # Input the comparison channel
                #----------------------------------------
                print("")
                while True:
                    print("Please input the comparison channel : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_stop_level_channel = ctypes.c_short(int(buf))
                #----------------------------------------
                # Input the comparison level
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison level[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison level[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_stop_level_value = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the comparison direction
                #----------------------------------------
                print("\n----------------------------------------")
                print("Parameter(Comparison direction)")
                print("----------------------------------------")
                print("0 : Both edges")
                print("1 : Rising edge")
                print("2 : Falling edge")
                while True:
                    print("Please input the comparison direction : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_stop_level_direction = ctypes.c_short(int(buf))
                #----------------------------------------
                # Set the level comparison
                #----------------------------------------
                lret.value = caio.AioSetAiStopLevelEx(id, ai_stop_level_channel, ai_stop_level_value, ai_stop_level_direction)
                if lret.value != 0:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nStop Condition Error")
                    print(f"AioSetAiStopLevelEx Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            #----------------------------------------
            # For stop condition is In Range comparison
            #----------------------------------------
            elif ai_stop_trigger.value == 5:
                #----------------------------------------
                # Input the comparison channel
                #----------------------------------------
                print("")
                while True:
                    print("Please input the comparison channel : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_stop_inrange_channel = ctypes.c_short(int(buf))
                #----------------------------------------
                # Input the comparison upper limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison upper limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison upper limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_stop_inrange_upper = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the comparison lower limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison lower limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison lower limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_stop_inrange_lower = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the state-holding times
                #----------------------------------------
                print("")
                while True:
                    print("Please input the state-holding times : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_stop_inrange_state = ctypes.c_long(int(buf))
                #----------------------------------------
                # Set the in range comparison
                #----------------------------------------
                lret.value = caio.AioSetAiStopInRangeEx(id, ai_stop_inrange_channel, ai_stop_inrange_upper, ai_stop_inrange_lower, ai_stop_inrange_state)
                if lret.value != 0:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nStop Condition Error")
                    print(f"AioSetAiStopInRangeEx Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            #----------------------------------------
            # For stop condition is Out Range comparison
            #----------------------------------------
            elif ai_stop_trigger.value == 6:
                #----------------------------------------
                # Input the comparison channel
                #----------------------------------------
                print("")
                while True:
                    print("Please input the comparison channel : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_stop_outrange_channel = ctypes.c_short(int(buf))
                #----------------------------------------
                # Input the comparison upper limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison upper limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison upper limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_stop_outrange_upper = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the comparison lower limit value
                #----------------------------------------
                print("")
                while True:
                    if ai_range.value == caio.P20MA or ai_range.value == caio.P4TO20MA or ai_range.value == caio.PM20MA:
                        print("Please input the comparison lower limit value[mA] : ", end='', flush=True)
                    else:
                        print("Please input the comparison lower limit value[V] : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 0):
                        continue
                    break
                ai_stop_outrange_lower = ctypes.c_float(float(buf))
                #----------------------------------------
                # Input the state-holding times
                #----------------------------------------
                print("")
                while True:
                    print("Please input the state-holding times : ", end='', flush=True)
                    #----------------------------------------
                    # Unlock
                    #----------------------------------------
                    g_lock.release()
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Lock
                    #----------------------------------------
                    g_lock.acquire()
                    #----------------------------------------
                    # If the total sampling count, sampling count, status are displayed in the callback, output a new line
                    #----------------------------------------
                    if g_info_flag.value == 1:
                        print("")
                        g_info_flag.value = 0
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                ai_stop_outrange_state = ctypes.c_long(int(buf))
                #----------------------------------------
                # Set the out range comparison
                #----------------------------------------
                lret.value = caio.AioSetAiStopOutRangeEx(id, ai_stop_outrange_channel, ai_stop_outrange_upper, ai_stop_outrange_lower, ai_stop_outrange_state)
                if lret.value != 0:
                    caio.AioGetErrorString(lret, error_buf)
                    print("\nStop Condition Error")
                    print(f"AioSetAiStopOutRangeEx Ret = {lret.value} {error_buf.value.decode('sjis')}\n")
                    continue
            print("\nStop Condition : Successful completion\n")
        #--------------------
        # Back
        #--------------------
        elif buf == "b":
            break
        #--------------------
        # Error Number
        #--------------------
        else:
            print("\nThe number is out of range.\nPlease try again.\n")
            continue