#================================================================
#================================================================
# API-AIO(WDM)
# Infinite sampling using user buffer sample
# Condition setting file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import ctypes
import caio
from ConditionUI import ConditionWindowUI


#========================================
# ConditionWindow class
#========================================
class ConditionWindow:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Variable
        #----------------------------------------
        self.start_trigger = (0,    # Software
                              1,    # External trigger rising edge
                              2,    # External trigger falling edge
                              3,    # Level comparison
                              4,    # In Range comparison
                              5)    # Out Range comparison
        self.clock_type    = (0,    # Internal clock
                              1)    # External clock
        self.clock_edge    = (0,    # External input falling edge
                              1)    # External input rising edge
        self.stop_trigger  = (1,    # External trigger rising edge
                              2,    # External trigger falling edge
                              3,    # Level comparison
                              4,    # Command (AioStopAi)
                              5,    # In Range comparison
                              6)    # Out Range comparison
        #----------------------------------------
        # Set the parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self.ui = ConditionWindowUI(parent)
        #----------------------------------------
        # Set the callback function for the UI controls
        #----------------------------------------
        self.ui.setup_commands(
            command_start_condition=self.OnSelendokComboStartCondition,
            command_clock_type=self.OnSelendokComboClockType,
            command_stop_condition=self.OnSelendokComboStopCondition,
            command_ok=self.OnClickedButtonOk,
            command_cancel=self.OnClickedButtonCancel
        )
        #----------------------------------------
        # Initialize the display
        #----------------------------------------
        self._update_ui()
        #----------------------------------------
        # Wait until the modal dialog closes
        #----------------------------------------
        self.parent.ui.root.wait_window(self.ui.condition_window)

    #========================================
    # Initialize the display
    #========================================
    def _update_ui(self):
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code
        ai_start_trigger = ctypes.c_short()                # Conversion start condition
        ai_stop_trigger = ctypes.c_short()                 # Conversion stop condition
        ai_level = ctypes.c_float()                        # Level comparison data
        ai_direction = ctypes.c_short()                    # Level comparison direction
        ai_range_level1 = ctypes.c_float()                 # Upper limit data of In Range / Out Range comparison
        ai_range_level2 = ctypes.c_float()                 # Lower limit data of In Range / Out Range comparison
        ai_state_times = ctypes.c_long()                   # Sampling times of the state-holding
        ai_clock_type = ctypes.c_short()                   # Clock type
        ai_sampling_clock = ctypes.c_float()               # Sampling clock
        ai_auto_mode = ctypes.c_short()                    # Set mode of transfer size
        ai_transfer_size = ctypes.c_long()                 # Transfer size
        ai_clock_edge = ctypes.c_short()                   # Timing of the external clock input
        ai_scan_clock = ctypes.c_float()                   # Conversion speed
        #----------------------------------------
        # Initialize parameters
        #----------------------------------------
        err_flag = 0
        self.parent.condition_ok = False
        #----------------------------------------
        # Get the conversion start condition
        #----------------------------------------
        lret.value = caio.AioGetAiStartTrigger(self.parent.id, ctypes.byref(ai_start_trigger))
        if lret.value != 0:
            if lret.value != 20001:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Generate error message
                #--------------------
                message_string = f"AioGetAiStartTrigger = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                err_flag = 1
            else:
                #--------------------
                # Disable control
                #--------------------
                self.ui.combobox_start_condition.config(state='disabled')
            #--------------------
            # Change the selection of analog input conversion start condition combobox to 0
            #--------------------
            self.ui.combobox_start_condition.current(0)
            self.OnSelendokComboStartCondition()
        #----------------------------------------
        # Normal termination process
        #----------------------------------------
        else:
            #--------------------
            # Convert from conversion start condition to index
            #--------------------
            index = 0
            if ai_start_trigger.value == 0:
                index = 0
            elif ai_start_trigger.value == 1:
                index = 1
            elif ai_start_trigger.value == 2:
                index = 2
            elif ai_start_trigger.value == 3:
                index = 3
            elif ai_start_trigger.value == 4:
                index = 4
            elif ai_start_trigger.value == 5:
                index = 5
            else:
                index = 0
            #--------------------
            # Change the selection of the analog input conversion start condition combobox
            #--------------------
            self.ui.combobox_start_condition.current(index)
            self.OnSelendokComboStartCondition()
        #----------------------------------------
        # Update the initial display of channel No.
        #----------------------------------------
        self.ui.textbox_start_level_channel_no_var.set('0')
        #----------------------------------------
        # For Level comparison
        #----------------------------------------
        if ai_start_trigger.value == 3:
            #----------------------------------------
            # Get the level when the level comparison is started
            #----------------------------------------
            lret.value = caio.AioGetAiStartLevelEx(self.parent.id, 0, ctypes.byref(ai_level), ctypes.byref(ai_direction))
            if lret.value != 0:
                if lret.value != 20001:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Generate error message
                    #--------------------
                    message_string = f"AioGetAiStartLevelEx = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                    err_flag = 1
                else:
                    #--------------------
                    # Disable control
                    #--------------------
                    self.ui.textbox_start_level_channel_no.config(state='disabled')
                    self.ui.textbox_start_level_comparison_value.config(state='disabled')
                    self.ui.combobox_start_level_comparison_direction.config(state='disabled')
                #--------------------
                # Update the initial display of comparison value
                #--------------------
                self.ui.textbox_start_level_comparison_value_var.set('1.0')
                #--------------------
                # Change the selection of the level comparison direction combobox to 0
                #--------------------
                self.ui.combobox_start_level_comparison_direction.current(0)
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                #--------------------
                # Update comparison value
                #--------------------
                self.ui.textbox_start_level_comparison_value_var.set(f"{ai_level.value:.1f}")
                #--------------------
                # Convert from level comparison direction to index
                #--------------------
                index = 0
                if ai_direction.value == 0:
                    index = 0
                elif ai_direction.value == 1:
                    index = 1
                elif ai_direction.value == 2:
                    index = 2
                else:
                    index = 0
                #--------------------
                # Change the selection of the level comparison direction combobox
                #--------------------
                self.ui.combobox_start_level_comparison_direction.current(index)
        #----------------------------------------
        # Other than Level comparison
        #----------------------------------------
        else:
            #--------------------
            # Update the initial display of comparison value
            #--------------------
            self.ui.textbox_start_level_comparison_value_var.set('1.0')
            #--------------------
            # Change the selection of the level comparison direction combobox to 0
            #--------------------
            self.ui.combobox_start_level_comparison_direction.current(0)
        #----------------------------------------
        # Update the initial display of channel No.
        #----------------------------------------
        self.ui.textbox_start_range_channel_no_var.set('0')
        #----------------------------------------
        # For In Range comparison
        #----------------------------------------
        if ai_start_trigger.value == 4:
            #----------------------------------------
            # Get the settings of In Range comparison start condition
            #----------------------------------------
            lret.value = caio.AioGetAiStartInRangeEx(self.parent.id, 0, ctypes.byref(ai_range_level1), ctypes.byref(ai_range_level2), ctypes.byref(ai_state_times))
            if lret.value != 0:
                if lret.value != 20001:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Generate error message
                    #--------------------
                    message_string = f"AioGetAiStartInRangeEx = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                    err_flag = 1
                else:
                    #--------------------
                    # Disable control
                    #--------------------
                    self.ui.textbox_start_range_channel_no.config(state='disabled')
                    self.ui.textbox_start_range_upper_limit_value.config(state='disabled')
                    self.ui.textbox_start_range_lower_limit_value.config(state='disabled')
                    self.ui.textbox_start_range_state_times.config(state='disabled')
                #--------------------
                # Update the initial display of upper limit
                #--------------------
                self.ui.textbox_start_range_upper_limit_value_var.set('10.0')
                #--------------------
                # Update the initial display of lower limit
                #--------------------
                self.ui.textbox_start_range_lower_limit_value_var.set('1.0')
                #--------------------
                # Update the initial display of state-holding times
                #--------------------
                self.ui.textbox_start_range_state_times_var.set('1')
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                #--------------------
                # Update upper limit
                #--------------------
                self.ui.textbox_start_range_upper_limit_value_var.set(f"{ai_range_level1.value:.1f}")
                #--------------------
                # Update lower limit
                #--------------------
                self.ui.textbox_start_range_lower_limit_value_var.set(f"{ai_range_level2.value:.1f}")
                #--------------------
                # Update state-holding times
                #--------------------
                self.ui.textbox_start_range_state_times_var.set(f"{ai_state_times.value:d}")
        #----------------------------------------
        # For Out Range comparison
        #----------------------------------------
        elif ai_start_trigger.value == 5:
            #----------------------------------------
            # Get the settings of Out Range comparison start condition
            #----------------------------------------
            lret.value = caio.AioGetAiStartOutRangeEx(self.parent.id, 0, ctypes.byref(ai_range_level1), ctypes.byref(ai_range_level2), ctypes.byref(ai_state_times))
            if lret.value != 0:
                if lret.value != 20001:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Generate error message
                    #--------------------
                    message_string = f"AioGetAiStartOutRangeEx = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                    err_flag = 1
                else:
                    #--------------------
                    # Disable control
                    #--------------------
                    self.ui.textbox_start_range_channel_no.config(state='disabled')
                    self.ui.textbox_start_range_upper_limit_value.config(state='disabled')
                    self.ui.textbox_start_range_lower_limit_value.config(state='disabled')
                    self.ui.textbox_start_range_state_times.config(state='disabled')
                #--------------------
                # Update the initial display of upper limit
                #--------------------
                self.ui.textbox_start_range_upper_limit_value_var.set('10.0')
                #--------------------
                # Update the initial display of lower limit
                #--------------------
                self.ui.textbox_start_range_lower_limit_value_var.set('1.0')
                #--------------------
                # Update the initial display of state-holding times
                #--------------------
                self.ui.textbox_start_range_state_times_var.set('1')
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                #--------------------
                # Update upper limit
                #--------------------
                self.ui.textbox_start_range_upper_limit_value_var.set(f"{ai_range_level1.value:.1f}")
                #--------------------
                # Update lower limit
                #--------------------
                self.ui.textbox_start_range_lower_limit_value_var.set(f"{ai_range_level2.value:.1f}")
                #--------------------
                # Update state-holding times
                #--------------------
                self.ui.textbox_start_range_state_times_var.set(f"{ai_state_times.value:d}")
        #----------------------------------------
        # Other than In Range comparison / Out Range comparison
        #----------------------------------------    
        else:
            #--------------------
            # Update the initial display of upper limit
            #--------------------
            self.ui.textbox_start_range_upper_limit_value_var.set('10.0')
            #--------------------
            # Update the initial display of lower limit
            #--------------------
            self.ui.textbox_start_range_lower_limit_value_var.set('1.0')
            #--------------------
            # Update the initial display of state-holding times
            #--------------------
            self.ui.textbox_start_range_state_times_var.set('1')
        #----------------------------------------
        # Get clock type
        #----------------------------------------
        lret.value = caio.AioGetAiClockType(self.parent.id, ctypes.byref(ai_clock_type))
        if lret.value != 0:
            if lret.value != 20001:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Generate error message
                #--------------------
                message_string = f"AioGetAiClockType = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                err_flag = 1
            else:
                #--------------------
                # Disable control
                #--------------------
                self.ui.combobox_clock_type.config(state='disabled')
            #--------------------
            # Change the selection of the analog input clock type combobox to 0
            #--------------------
            self.ui.combobox_clock_type.current(0)
            self.OnSelendokComboClockType()
        #----------------------------------------
        # Normal termination process
        #----------------------------------------
        else:
            #--------------------
            # Convert from clock type to index
            #--------------------
            index = 0
            if ai_clock_type.value == 0:
                index = 0
            elif ai_clock_type.value == 1:
                index = 1
            else:
                index = 0
            #--------------------
            # Change the selection of the analog input clock type combobox
            #--------------------
            self.ui.combobox_clock_type.current(index)
            self.OnSelendokComboClockType()
        #----------------------------------------
        # Get sampling clock
        #----------------------------------------
        lret.value = caio.AioGetAiSamplingClock(self.parent.id, ctypes.byref(ai_sampling_clock))
        if lret.value != 0:
            if lret.value != 20001:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Generate error message
                #--------------------
                message_string = f"AioGetAiSamplingClock = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                err_flag = 1
            else:
                #--------------------
                # Disable control
                #--------------------
                self.ui.textbox_sampling_clock.config(state='disabled')
            #--------------------
            # Update the initial display of sampling clock
            #--------------------
            self.ui.textbox_sampling_clock_var.set('1000')
        #----------------------------------------
        # Normal termination process
        #----------------------------------------
        else:
            #--------------------
            # Update sampling clock
            #--------------------
            self.ui.textbox_sampling_clock_var.set(f"{ai_sampling_clock.value:f}")
        #----------------------------------------
        # For External clock
        #----------------------------------------
        if ai_clock_type.value == 1:
            #----------------------------------------
            # Get the timing of external clock input
            #----------------------------------------
            lret.value = caio.AioGetAiClockEdge(self.parent.id, ctypes.byref(ai_clock_edge))
            if lret.value != 0:
                if lret.value != 20001:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Generate error message
                    #--------------------
                    message_string = f"AioGetAiClockEdge = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                    err_flag = 1
                else:
                    #--------------------
                    # Disable control
                    #--------------------
                    self.ui.combobox_clock_edge.config(state='disabled')
                #--------------------
                # Change the selection of the analog input external clock input timing combobox to 0
                #--------------------
                self.ui.combobox_clock_edge.current(0)
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                #--------------------
                # Convert from input timing to index
                #--------------------
                index = 0
                if ai_clock_edge.value == 0:
                    index = 0
                elif ai_clock_edge.value == 1:
                    index = 1
                else:
                    index = 0
                #--------------------
                # Change the selection of the analog input external clock input timing combobox
                #--------------------
                self.ui.combobox_clock_edge.current(index)
        #----------------------------------------
        # Other than External clock
        #----------------------------------------
        else:
            #--------------------
            # Change the selection of the analog input external clock input timing combobox to 0
            #--------------------
            self.ui.combobox_clock_edge.current(0)
        #----------------------------------------
        # Get transfer size
        #----------------------------------------
        lret.value = caio.AioGetAiTransferSize(self.parent.id, ctypes.byref(ai_auto_mode), ctypes.byref(ai_transfer_size))
        if lret.value != 0:
            if lret.value != 20001:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Generate error message
                #--------------------
                message_string = f"AioGetAiTransferSize = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                err_flag = 1
            else:
                #--------------------
                # Disable control
                #--------------------
                self.ui.textbox_transfer_size.config(state='disabled')
            #--------------------
            # Update the initial display of transfer size
            #--------------------
            self.ui.textbox_transfer_size_var.set('1')
        #----------------------------------------
        # Normal termination process
        #----------------------------------------
        else:
            #--------------------
            # Update transfer size
            #--------------------
            self.ui.textbox_transfer_size_var.set(f"{ai_transfer_size.value:d}")
        #----------------------------------------
        # Get conversion speed
        #----------------------------------------
        lret.value = caio.AioGetAiScanClock(self.parent.id, ctypes.byref(ai_scan_clock))
        if lret.value != 0:
            if lret.value != 20001:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Generate error message
                #--------------------
                message_string = f"AioGetAiScanClock = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                err_flag = 1
            else:
                #--------------------
                # Disable control
                #--------------------
                self.ui.textbox_scan_clock.config(state='disabled')
            #--------------------
            # Update the initial display of conversion speed
            #--------------------
            self.ui.textbox_scan_clock_var.set('10')
        #----------------------------------------
        # Normal termination process
        #----------------------------------------
        else:
            #--------------------
            # Update conversion speed
            #--------------------
            self.ui.textbox_scan_clock_var.set(f"{ai_scan_clock.value:f}")
        #----------------------------------------
        # Get the conversion stop condition
        #----------------------------------------
        lret.value = caio.AioGetAiStopTrigger(self.parent.id, ctypes.byref(ai_stop_trigger))
        if lret.value != 0:
            if lret.value != 20001:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Generate error message
                #--------------------
                message_string = f"AioGetAiStopTrigger = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                err_flag = 1
            else:
                #--------------------
                # Disable control
                #--------------------
                self.ui.combobox_stop_condition.config(state='disabled')
            #--------------------
            # Change the selection in the analog input conversion stop condition combobox to 3
            #--------------------
            self.ui.combobox_stop_condition.current(3)
            self.OnSelendokComboStopCondition()
        #----------------------------------------
        # Normal termination process
        #----------------------------------------
        else:
            #--------------------
            # Convert from conversion stop condition to index
            #--------------------
            index = 0
            if ai_stop_trigger.value == 0:
                index = 3
            elif ai_stop_trigger.value == 1:
                index = 0
            elif ai_stop_trigger.value == 2:
                index = 1
            elif ai_stop_trigger.value == 3:
                index = 2
            elif ai_stop_trigger.value == 4:
                index = 3
            elif ai_stop_trigger.value == 5:
                index = 4
            elif ai_stop_trigger.value == 6:
                index = 5
            else:
                index = 3
            #--------------------
            # Change the selection in the analog input conversion stop condition combobox
            #--------------------
            self.ui.combobox_stop_condition.current(index)
            self.OnSelendokComboStopCondition()
        #----------------------------------------
        # Update the initial display of channel No.
        #----------------------------------------
        self.ui.textbox_stop_level_channel_no_var.set('0')
        #----------------------------------------
        # For Level comparison
        #----------------------------------------
        if ai_stop_trigger.value == 3:
            #----------------------------------------
            # Get the level when the level comparison is stopped
            #----------------------------------------
            lret.value = caio.AioGetAiStopLevelEx(self.parent.id, 0, ctypes.byref(ai_level), ctypes.byref(ai_direction))
            if lret.value != 0:
                if lret.value != 20001:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Generate error message
                    #--------------------
                    message_string = f"AioGetAiStopLevelEx = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                    err_flag = 1
                else:
                    #--------------------
                    # Disable control
                    #--------------------
                    self.ui.textbox_stop_level_channel_no.config(state='disabled')
                    self.ui.textbox_stop_level_comparison_value.config(state='disabled')
                    self.ui.combobox_stop_level_comparison_direction.config(state='disabled')
                #--------------------
                # Update the initial display of comparison value
                #--------------------
                self.ui.textbox_stop_level_comparison_value_var.set('1.0')
                #--------------------
                # Change the selection of the level comparison direction combobox to 0
                #--------------------
                self.ui.combobox_stop_level_comparison_direction.current(0)
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                #--------------------
                # Update comparison value
                #--------------------
                self.ui.textbox_stop_level_comparison_value_var.set(f"{ai_level.value:.1f}")
                #--------------------
                # Convert from level comparison direction to index
                #--------------------
                index = 0
                if ai_direction.value == 0:
                    index = 0
                elif ai_direction.value == 1:
                    index = 1
                elif ai_direction.value == 2:
                    index = 2
                else:
                    index = 0
                #--------------------
                # Change the selection of the level comparison direction combobox
                #--------------------
                self.ui.combobox_stop_level_comparison_direction.current(index)
        #----------------------------------------
        # Other than Level comparison
        #----------------------------------------
        else:
            #--------------------
            # Update the initial display of comparison value
            #--------------------
            self.ui.textbox_stop_level_comparison_value_var.set('1.0')
            #--------------------
            # Change the selection of the level comparison direction combobox to 0
            #--------------------
            self.ui.combobox_stop_level_comparison_direction.current(0)
        #----------------------------------------
        # Update the initial display of channel No.
        #----------------------------------------
        self.ui.textbox_stop_range_channel_no_var.set('0')
        #----------------------------------------
        # For In Range comparison
        #----------------------------------------
        if ai_stop_trigger.value == 5:
            #----------------------------------------
            # Get the settings of In Range comparison stop condition
            #----------------------------------------
            lret.value = caio.AioGetAiStopInRangeEx(self.parent.id, 0, ctypes.byref(ai_range_level1), ctypes.byref(ai_range_level2), ctypes.byref(ai_state_times))
            if lret.value != 0:
                if lret.value != 20001:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Generate error message
                    #--------------------
                    message_string = f"AioGetAiStopInRangeEx = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                    err_flag = 1
                else:
                    #--------------------
                    # Disable control
                    #--------------------
                    self.ui.textbox_stop_range_channel_no.config(state='disabled')
                    self.ui.textbox_stop_range_upper_limit_value.config(state='disabled')
                    self.ui.textbox_stop_range_lower_limit_value.config(state='disabled')
                    self.ui.textbox_stop_range_state_times.config(state='disabled')
                #--------------------
                # Update the initial display of upper limit
                #--------------------
                self.ui.textbox_stop_range_upper_limit_value_var.set('10.0')
                #--------------------
                # Update the initial display of lower limit
                #--------------------
                self.ui.textbox_stop_range_lower_limit_value_var.set('1.0')
                #--------------------
                # Update the initial display of state-holding times
                #--------------------
                self.ui.textbox_stop_range_state_times_var.set('1')
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                #--------------------
                # Update upper limit
                #--------------------
                self.ui.textbox_stop_range_upper_limit_value_var.set(f"{ai_range_level1.value:.1f}")
                #--------------------
                # Update lower limit
                #--------------------
                self.ui.textbox_stop_range_lower_limit_value_var.set(f"{ai_range_level2.value:.1f}")
                #--------------------
                # Update state-holding times
                #--------------------
                self.ui.textbox_stop_range_state_times_var.set(f"{ai_state_times.value:d}")
        #----------------------------------------
        # For Out Range comparison
        #----------------------------------------
        elif ai_stop_trigger.value == 6:
            #----------------------------------------
            # Get the settings of Out Range comparison stop condition
            #----------------------------------------
            lret.value = caio.AioGetAiStopOutRangeEx(self.parent.id, 0, ctypes.byref(ai_range_level1), ctypes.byref(ai_range_level2), ctypes.byref(ai_state_times))
            if lret.value != 0:
                if lret.value != 20001:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Generate error message
                    #--------------------
                    message_string = f"AioGetAiStopOutRangeEx = {lret.value} : {error_string.value.decode('sjis')}\n\n*The display could not be updated since an error occurred."
                    err_flag = 1
                else:
                    #--------------------
                    # Disable control
                    #--------------------
                    self.ui.textbox_stop_range_channel_no.config(state='disabled')
                    self.ui.textbox_stop_range_upper_limit_value.config(state='disabled')
                    self.ui.textbox_stop_range_lower_limit_value.config(state='disabled')
                    self.ui.textbox_stop_range_state_times.config(state='disabled')
                #--------------------
                # Update the initial display of upper limit
                #--------------------
                self.ui.textbox_stop_range_upper_limit_value_var.set('10.0')
                #--------------------
                # Update the initial display of lower limit
                #--------------------
                self.ui.textbox_stop_range_lower_limit_value_var.set('1.0')
                #--------------------
                # Update the initial display of state-holding times
                #--------------------
                self.ui.textbox_stop_range_state_times_var.set('1')
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                #--------------------
                # Update upper limit
                #--------------------
                self.ui.textbox_stop_range_upper_limit_value_var.set(f"{ai_range_level1.value:.1f}")
                #--------------------
                # Update lower limit
                #--------------------
                self.ui.textbox_stop_range_lower_limit_value_var.set(f"{ai_range_level2.value:.1f}")
                #--------------------
                # Update state-holding times
                #--------------------
                self.ui.textbox_stop_range_state_times_var.set(f"{ai_state_times.value:d}")
        #----------------------------------------
        # Other than In Range comparison / Out Range comparison
        #----------------------------------------    
        else:
            #--------------------
            # Update the initial display of upper limit
            #--------------------
            self.ui.textbox_stop_range_upper_limit_value_var.set('10.0')
            #--------------------
            # Update the initial display of lower limit
            #--------------------
            self.ui.textbox_stop_range_lower_limit_value_var.set('1.0')
            #--------------------
            # Update the initial display of state-holding times
            #--------------------
            self.ui.textbox_stop_range_state_times_var.set('1')
        #----------------------------------------
        # Display message if an error occurs
        #----------------------------------------
        if err_flag != 0:
            self.ui.show_error("Error", message_string)

    #========================================
    # Function name    isnum
    # Parameter        str     String
    # Parameter        base    Base
    # Details          Function to checks if a string can be converted to a number
    # Return value     True: Can be converted
    #                  False: Can not be converted
    #========================================
    @staticmethod
    def isnum(str, base):
        try:
            if 10 == base:
                int(str, 10)
            elif 16 == base:
                int(str, 16)
            else:
                float(str)
        except:
            return False
        return True

    #========================================
    # Function name    OnSelendokComboStartCondition
    # Parameter        event    Event(Fixed to None)
    # Details          Function of updating available controls
    # Return value     None
    #========================================
    def OnSelendokComboStartCondition(self, event=None):
        #----------------------------------------
        # Get the index of start condition from combobox
        #----------------------------------------
        index = self.ui.combobox_start_condition.current()
        #----------------------------------------
        # Branch processing by start condition
        #----------------------------------------
        if (self.start_trigger[index] == 0 or
            self.start_trigger[index] == 1 or 
            self.start_trigger[index] == 2):      # Software, External trigger rising edge, External trigger falling edge
            self.ui.textbox_start_level_channel_no.config(state="disabled")
            self.ui.textbox_start_level_comparison_value.config(state="disabled")
            self.ui.combobox_start_level_comparison_direction.config(state="disabled")
            self.ui.textbox_start_range_channel_no.config(state="disabled")
            self.ui.textbox_start_range_upper_limit_value.config(state="disabled")
            self.ui.textbox_start_range_lower_limit_value.config(state="disabled")
            self.ui.textbox_start_range_state_times.config(state="disabled")
        elif (self.start_trigger[index] == 3):    # Level comparison
            self.ui.textbox_start_level_channel_no.config(state="normal")
            self.ui.textbox_start_level_comparison_value.config(state="normal")
            self.ui.combobox_start_level_comparison_direction.config(state="readonly")
            self.ui.textbox_start_range_channel_no.config(state="disabled")
            self.ui.textbox_start_range_upper_limit_value.config(state="disabled")
            self.ui.textbox_start_range_lower_limit_value.config(state="disabled")
            self.ui.textbox_start_range_state_times.config(state="disabled")
        elif (self.start_trigger[index] == 4 or
              self.start_trigger[index] == 5):    # In Range comparison, Out Range comparison
            self.ui.textbox_start_level_channel_no.config(state="disabled")
            self.ui.textbox_start_level_comparison_value.config(state="disabled")
            self.ui.combobox_start_level_comparison_direction.config(state="disabled")
            self.ui.textbox_start_range_channel_no.config(state="normal")
            self.ui.textbox_start_range_upper_limit_value.config(state="normal")
            self.ui.textbox_start_range_lower_limit_value.config(state="normal")
            self.ui.textbox_start_range_state_times.config(state="normal")
        else:
            self.ui.textbox_start_level_channel_no.config(state="disabled")
            self.ui.textbox_start_level_comparison_value.config(state="disabled")
            self.ui.combobox_start_level_comparison_direction.config(state="disabled")
            self.ui.textbox_start_range_channel_no.config(state="disabled")
            self.ui.textbox_start_range_upper_limit_value.config(state="disabled")
            self.ui.textbox_start_range_lower_limit_value.config(state="disabled")
            self.ui.textbox_start_range_state_times.config(state="disabled")

    #========================================
    # Function name    OnSelendokComboClockType
    # Parameter        event    Event(Fixed to None)
    # Details          Function of updating available controls
    # Return value     None
    #========================================
    def OnSelendokComboClockType(self, event=None):
        lret = ctypes.c_long()                # For return code
        ai_auto_mode = ctypes.c_short()       # Set mode of transfer size
        ai_transfer_size = ctypes.c_long()    # Transfer Size

        #----------------------------------------
        # Get the index of clock type from combobox
        #----------------------------------------
        index = self.ui.combobox_clock_type.current()
        #----------------------------------------
        # Branch processing by clock type
        #----------------------------------------
        if self.clock_type[index] == 0:      # Internal clock
            self.ui.textbox_sampling_clock.config(state="normal")
            self.ui.combobox_clock_edge.config(state='disabled')
            self.ui.textbox_transfer_size.config(state='disabled')
        elif self.clock_type[index] == 1:    # External clock
            self.ui.textbox_sampling_clock.config(state="disabled")
            self.ui.combobox_clock_edge.config(state='readonly')
            #----------------------------------------
            # Get transfer size
            #----------------------------------------
            lret.value = caio.AioGetAiTransferSize(self.parent.id, ctypes.byref(ai_auto_mode), ctypes.byref(ai_transfer_size))
            #----------------------------------------
            # Abnormal termination process
            #----------------------------------------
            if lret.value != 0:
                self.ui.textbox_transfer_size.config(state='disabled')
            #----------------------------------------
            # Normal termination process
            #----------------------------------------
            else:
                self.ui.textbox_transfer_size.config(state='normal')
        else:
            self.ui.textbox_sampling_clock.config(state="normal")
            self.ui.combobox_clock_edge.config(state='disabled')
            self.ui.textbox_transfer_size.config(state='disabled')

    #========================================
    # Function name    OnSelendokComboStopCondition
    # Parameter        event    Event(Fixed to None)
    # Details          Function of updating available controls
    # Return value     None
    #========================================
    def OnSelendokComboStopCondition(self, event=None):
        #----------------------------------------
        # Get the index of stop condition from combobox
        #----------------------------------------
        index = self.ui.combobox_stop_condition.current()
        #----------------------------------------
        # Branch processing by stop condition
        #----------------------------------------
        if (self.stop_trigger[index] == 1 or
            self.stop_trigger[index] == 2 or 
            self.stop_trigger[index] == 4):      # External trigger rising edge, External trigger falling edge, Command (AioStopAi)
            self.ui.textbox_stop_level_channel_no.config(state="disabled")
            self.ui.textbox_stop_level_comparison_value.config(state="disabled")
            self.ui.combobox_stop_level_comparison_direction.config(state="disabled")
            self.ui.textbox_stop_range_channel_no.config(state="disabled")
            self.ui.textbox_stop_range_upper_limit_value.config(state="disabled")
            self.ui.textbox_stop_range_lower_limit_value.config(state="disabled")
            self.ui.textbox_stop_range_state_times.config(state="disabled")
        elif (self.stop_trigger[index] == 3):    # Level comparison
            self.ui.textbox_stop_level_channel_no.config(state="normal")
            self.ui.textbox_stop_level_comparison_value.config(state="normal")
            self.ui.combobox_stop_level_comparison_direction.config(state="readonly")
            self.ui.textbox_stop_range_channel_no.config(state="disabled")
            self.ui.textbox_stop_range_upper_limit_value.config(state="disabled")
            self.ui.textbox_stop_range_lower_limit_value.config(state="disabled")
            self.ui.textbox_stop_range_state_times.config(state="disabled")
        elif (self.stop_trigger[index] == 5 or
              self.stop_trigger[index] == 6):    # In Range comparison, Out Range comparison
            self.ui.textbox_stop_level_channel_no.config(state="disabled")
            self.ui.textbox_stop_level_comparison_value.config(state="disabled")
            self.ui.combobox_stop_level_comparison_direction.config(state="disabled")
            self.ui.textbox_stop_range_channel_no.config(state="normal")
            self.ui.textbox_stop_range_upper_limit_value.config(state="normal")
            self.ui.textbox_stop_range_lower_limit_value.config(state="normal")
            self.ui.textbox_stop_range_state_times.config(state="normal")
        else:
            self.ui.textbox_stop_level_channel_no.config(state="disabled")
            self.ui.textbox_stop_level_comparison_value.config(state="disabled")
            self.ui.combobox_stop_level_comparison_direction.config(state="disabled")
            self.ui.textbox_stop_range_channel_no.config(state="disabled")
            self.ui.textbox_stop_range_upper_limit_value.config(state="disabled")
            self.ui.textbox_stop_range_lower_limit_value.config(state="disabled")
            self.ui.textbox_stop_range_state_times.config(state="disabled")

    #========================================
    # Function name    OnClickedButtonOk
    # Parameter        None
    # Details          OK button click event function
    # Return value     None
    #========================================
    def OnClickedButtonOk(self):
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code

        #----------------------------------------
        # Set the data storage memory type to Overwrite the memory
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiMemoryType(self.parent.id, 1)
        if lret.value != 0:
            caio.AioGetErrorString(lret, error_string)
            text_string = f"AioSetAiMemoryType = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.show_error("Error", text_string)
            return
        #----------------------------------------
        # Get the conversion start condition setting value from combobox
        #----------------------------------------
        ai_start_trigger = ctypes.c_short(self.start_trigger[self.ui.combobox_start_condition.current()])
        #----------------------------------------
        # Set analog input conversion start condition
        #----------------------------------------
        lret.value = caio.AioSetAiStartTrigger(self.parent.id, ai_start_trigger)
        if lret.value != 0:
            caio.AioGetErrorString(lret, error_string)
            text_string = f"AioSetAiStartTrigger = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.show_error("Error", text_string)
            return
        #----------------------------------------
        # For Level comparison
        #----------------------------------------
        if ai_start_trigger.value == 3:
            #----------------------------------------
            # Get the Level comparison channel from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_level_channel_no_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_level_channel = ctypes.c_short(int(buf))
            #----------------------------------------
            # Get the Level comparison data from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_level_comparison_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_level = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the Level comparison direction from combobox
            #----------------------------------------
            ai_direction = ctypes.c_short(self.ui.combobox_start_level_comparison_direction.current())
            #----------------------------------------
            # Set the level at start of level comparison
            #----------------------------------------
            lret.value = caio.AioSetAiStartLevelEx(self.parent.id, ai_level_channel, ai_level, ai_direction)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiStartLevelEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # For In Range comparison
        #----------------------------------------
        elif ai_start_trigger.value == 4:
            #----------------------------------------
            # Get the channel of In Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_channel_no_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_channel = ctypes.c_short(int(buf))
            #----------------------------------------
            # Get the upper limit data of In Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_upper_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level1 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the lower limit data of In Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_lower_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level2 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the state-holding times from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_state_times_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_state_times = ctypes.c_long(int(buf))
            #----------------------------------------
            # Set In Range comparison start condition
            #----------------------------------------
            lret.value = caio.AioSetAiStartInRangeEx(self.parent.id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiStartInRangeEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # For Out Range comparison
        #----------------------------------------
        elif ai_start_trigger.value == 5:
            #----------------------------------------
            # Get the channel of Out Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_channel_no_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_channel = ctypes.c_short(int(buf))
            #----------------------------------------
            # Get the upper limit data of Out Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_upper_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level1 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the lower limit data of Out Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_lower_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level2 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the state-holding times from textbox
            #----------------------------------------
            buf = self.ui.textbox_start_range_state_times_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_state_times = ctypes.c_long(int(buf))
            #----------------------------------------
            # Set Out Range comparison start condition
            #----------------------------------------
            lret.value = caio.AioSetAiStartOutRangeEx(self.parent.id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiStartOutRangeEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # Get the clock type setting value from combobox
        #----------------------------------------
        ai_clock_type = ctypes.c_short(self.clock_type[self.ui.combobox_clock_type.current()])
        #----------------------------------------
        # Set analog input clock type
        #----------------------------------------
        lret.value = caio.AioSetAiClockType(self.parent.id, ai_clock_type)
        if lret.value != 0:
            caio.AioGetErrorString(lret, error_string)
            text_string = f"AioSetAiClockType = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.show_error("Error", text_string)
            return
        #----------------------------------------
        # For Internal clock
        #----------------------------------------
        if ai_clock_type.value == 0:
            #----------------------------------------
            # Get the sampling clock from textbox
            #----------------------------------------
            buf = self.ui.textbox_sampling_clock_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_sampling_clock = ctypes.c_float(float(buf))
            #----------------------------------------
            # Set analog input sampling clock
            #----------------------------------------
            lret.value = caio.AioSetAiSamplingClock(self.parent.id, ai_sampling_clock)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiSamplingClock = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
            #----------------------------------------
            # Set analog input transfer size
            #----------------------------------------
            lret.value = caio.AioSetAiTransferSize(self.parent.id, caio.AI_TRANSFERSIZE_AUTO, 0)
            #--------------------
            # Exit the process if not completed successfully
            #--------------------
            if lret.value != 0 and lret.value != 20001:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiTransferSize = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # For External clock
        #----------------------------------------
        elif ai_clock_type.value == 1:
            #----------------------------------------
            # Get the external clock input timing setting value from combobox
            #----------------------------------------
            ai_clock_edge = ctypes.c_short(self.ui.combobox_clock_edge.current())
            #----------------------------------------
            # Set the timing of external clock input
            #----------------------------------------
            lret.value = caio.AioSetAiClockEdge(self.parent.id, ai_clock_edge)
            #--------------------
            # Exit the process if not completed successfully
            #--------------------
            if lret.value != 0 and lret.value != 20001:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiClockEdge = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
            #----------------------------------------
            # Get the transfer size from textbox
            #----------------------------------------
            buf = self.ui.textbox_transfer_size_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_transfer_size = ctypes.c_long(int(buf))
            #----------------------------------------
            # Set analog input transfer size
            #----------------------------------------
            lret.value = caio.AioSetAiTransferSize(self.parent.id, caio.AI_TRANSFERSIZE_MANUAL, ai_transfer_size)
            #--------------------
            # Exit the process if not completed successfully
            #--------------------
            if lret.value != 0 and lret.value != 20001:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiTransferSize = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # When conversion speed function is available
        #----------------------------------------
        if self.ui.textbox_scan_clock['state'] == 'normal':
            #----------------------------------------
            # Get the conversion speed from textbox
            #----------------------------------------
            buf = self.ui.textbox_scan_clock_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_scan_clock = ctypes.c_float(float(buf))
            #----------------------------------------
            # Set analog input conversion speed
            #----------------------------------------
            lret.value = caio.AioSetAiScanClock(self.parent.id, ai_scan_clock)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiScanClock = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # Get the conversion stop condition setting value from combobox
        #----------------------------------------
        ai_stop_trigger = ctypes.c_short(self.stop_trigger[self.ui.combobox_stop_condition.current()])
        #----------------------------------------
        # Set analog input conversion stop condition
        #----------------------------------------
        lret.value = caio.AioSetAiStopTrigger(self.parent.id, ai_stop_trigger)
        if lret.value != 0:
            caio.AioGetErrorString(lret, error_string)
            text_string = f"AioSetAiStopTrigger = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.show_error("Error", text_string)
            return
        #----------------------------------------
        # For Level comparison
        #----------------------------------------
        if ai_stop_trigger.value == 3:
            #----------------------------------------
            # Get the Level comparison channel from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_level_channel_no_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_level_channel = ctypes.c_short(int(buf))
            #----------------------------------------
            # Get the Level comparison data from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_level_comparison_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_level = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the Level comparison direction from combobox
            #----------------------------------------
            ai_direction = ctypes.c_short(self.ui.combobox_stop_level_comparison_direction.current())
            #----------------------------------------
            # Set the level at stop of level comparison
            #----------------------------------------
            lret.value = caio.AioSetAiStopLevelEx(self.parent.id, ai_level_channel, ai_level, ai_direction)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiStopLevelEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # For In Range comparison
        #----------------------------------------
        elif ai_stop_trigger.value == 5:
            #----------------------------------------
            # Get the channel of In Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_channel_no_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_channel = ctypes.c_short(int(buf))
            #----------------------------------------
            # Get the upper limit data of In Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_upper_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level1 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the lower limit data of In Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_lower_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level2 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the state-holding times from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_state_times_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_state_times = ctypes.c_long(int(buf))
            #----------------------------------------
            # Set In Range comparison stop condition
            #----------------------------------------
            lret.value = caio.AioSetAiStopInRangeEx(self.parent.id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiStopInRangeEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # For Out Range comparison
        #----------------------------------------
        elif ai_stop_trigger.value == 6:
            #----------------------------------------
            # Get the channel of Out Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_channel_no_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_channel = ctypes.c_short(int(buf))
            #----------------------------------------
            # Get the upper limit data of Out Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_upper_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level1 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the lower limit data of Out Range comparison from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_lower_limit_value_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 0):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_range_level2 = ctypes.c_float(float(buf))
            #----------------------------------------
            # Get the state-holding times from textbox
            #----------------------------------------
            buf = self.ui.textbox_stop_range_state_times_var.get()
            #----------------------------------------
            # Check if the input value is a number
            #----------------------------------------
            if False == self.isnum(buf, 10):
                self.ui.show_error("Error", "The input value is invalid.")
                return
            else:
                ai_state_times = ctypes.c_long(int(buf))
            #----------------------------------------
            # Set Out Range comparison stop condition
            #----------------------------------------
            lret.value = caio.AioSetAiStopOutRangeEx(self.parent.id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            if lret.value != 0:
                caio.AioGetErrorString(lret, error_string)
                text_string = f"AioSetAiStopOutRangeEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.show_error("Error", text_string)
                return
        #----------------------------------------
        # Close window
        #----------------------------------------
        self.parent.condition_ok = True
        self.ui.condition_window.destroy()

    #========================================
    # Function name    OnClickedButtonCancel
    # Parameter        None
    # Details          Cancel button click event function
    # Return value     None
    #========================================
    def OnClickedButtonCancel(self):
        #----------------------------------------
        # Close window
        #----------------------------------------
        self.ui.condition_window.destroy()