﻿#================================================================
#================================================================
# API-AIO(WDM)
# Counter sample
# main file
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import ctypes
import Setting
import caio
from CounterUI import CounterUI


#========================================
# Counter class
#========================================
class Counter:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Variable
        #----------------------------------------
        self.setting_ok = False                     # Setting dialog result
        self.id = ctypes.c_short()                  # ID for device access
        self.channel = ctypes.c_short()             # Counter channel
        self.event_count = 0                        # Comparison count match event occurrence count
        self.compare_data_num = ctypes.c_long(1)    # Total number of comparison count data to be set
        self.compare_repeat = ctypes.c_short(0)     # Repeat setting of comparison count data array
        self.compare_data_0 = 100                   # Comparison count data 0
        self.compare_data_1 = 200                   # Comparison count data 1
        self.compare_data_2 = 300                   # Comparison count data 2
        self.compare_data_3 = 400                   # Comparison count data 3
        self.compare_data_4 = 500                   # Comparison count data 4
        self.preset_data_num = ctypes.c_long(1)     # Total number of the preset data to be set
        self.preset_repeat = ctypes.c_short(0)      # Repeat setting of preset data array
        self.preset_data_0 = 0                      # Counter preset data 0
        self.preset_data_1 = 100                    # Counter preset data 1
        self.preset_data_2 = 200                    # Counter preset data 2
        self.preset_data_3 = 300                    # Counter preset data 3
        self.preset_data_4 = 400                    # Counter preset data 4
        self.pcnt_callback = None                   # Pointer to callback function
        self.user_data = None                       # User data of callback function
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self.ui = CounterUI()
        #----------------------------------------
        # Set the callback function for the UI controls
        #----------------------------------------
        self.ui.setup_commands(
            command_aio_init=self.OnClickedButtonAioInit,
            command_aio_reset_device=self.OnClickedButtonAioResetDevice,
            command_channel_no=self.OnChangedEditChannelNo,
            command_settings=self.OnClickedButtonSetting,
            command_aio_preset_cnt=self.OnClickedButtonAioPresetCnt,
            command_start=self.OnClickedButtonStart,
            command_aio_get_cnt_count=self.OnClickedButtonAioGetCntCount,
            command_aio_get_cnt_status=self.OnClickedButtonAioGetCntStatus,
            command_stop=self.OnClickedButtonStop,
            command_aio_exit=self.OnClickedButtonAioExit,
            command_close=self.OnClickedButtonClose
        )

    #========================================
    # 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     GetStatusString
    # Parameter         cnt_status           Status
    # Details           Function to get status string
    # Return value      cnt_status_string    Status string
    #========================================
    @staticmethod
    def GetStatusString(cnt_status):
        #----------------------------------------
        # Create counter input status string
        #----------------------------------------
        #--------------------
        # When status 'Counter in operation' is ON
        #--------------------
        if (cnt_status.value & caio.CNTS_BUSY) == caio.CNTS_BUSY:
            cnt_status_string = "Operating"
        #--------------------
        # When status 'Counter in operation' is OFF
        #--------------------
        else:
            cnt_status_string = "Stop"
        #--------------------
        # When status 'Comparison count match' is ON
        #--------------------
        if (cnt_status.value & caio.CNTS_DATA_NUM) == caio.CNTS_DATA_NUM:
            cnt_status_string += ", Comparison count match"
        #--------------------
        # When status 'Overrun' is ON
        #--------------------
        if (cnt_status.value & caio.CNTS_ORERR) == caio.CNTS_ORERR:
            cnt_status_string += ", Overrun"
        #--------------------
        # When status 'Counter operation error' is ON
        #--------------------
        if (cnt_status.value & caio.CNTS_ERR) == caio.CNTS_ERR:
            text_buf += ", Counter operation error"
        return cnt_status_string

    #========================================
    # Function name    CallBackProc
    # Parameter        id           Id get by AioInit
    # Parameter        cnt_event    The event that occurred
    # Parameter        wParam       Parameter passed from driver
    # Parameter        lParam       Parameter unique to each event
    # Parameter        Param        Parameter specified in the AioSetCntCallBackProc function
    # Details          Function registered with the AioSetCntCallBackProc function
    #                  This function is called when an event occurs
    # Return value     None
    #========================================
    @staticmethod
    def CallBackProc(id, cnt_event, wParam, lParam, Param):
        cnt_count = ctypes.c_long()                        # Count value
        cnt_status = ctypes.c_long()                       # Status
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code

        #----------------------------------------
        # Create class object
        #----------------------------------------
        obj_ptr = ctypes.cast(Param, ctypes.POINTER(ctypes.py_object))
        self = obj_ptr.contents.value
        #----------------------------------------
        # Comparison count match event
        # Acquire and display data
        #----------------------------------------
        if cnt_event == caio.AIOM_CNTE_DATA_NUM:
            #----------------------------------------
            # Get counter channel
            #----------------------------------------
            cnt_channel = wParam
            if cnt_channel == self.channel.value:
                #----------------------------------------
                # Get count value
                #----------------------------------------
                lret.value = caio.AioGetCntCount(self.id, self.channel, ctypes.byref(cnt_count))
                #----------------------------------------
                # Error process
                #----------------------------------------
                #--------------------
                # Exit the process if not completed successfully
                #--------------------
                if lret.value != 0:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Display message
                    #--------------------
                    text_buf = f"Comparison Count Match Event Process : AioGetCntCount = {lret.value} : {error_string.value.decode('sjis')}"
                    self.ui.textbox_return_string_var.set(text_buf)
                    return
                #----------------------------------------
                # Display count value
                #----------------------------------------
                text_buf = f"{cnt_count.value}"
                self.ui.textbox_count_var.set(text_buf)
                #----------------------------------------
                # Get status
                #----------------------------------------
                lret.value = caio.AioGetCntStatus(self.id, self.channel, ctypes.byref(cnt_status))
                #----------------------------------------
                # Error process
                #----------------------------------------
                #--------------------
                # Exit the process if not completed successfully
                #--------------------
                if lret.value != 0:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Display message
                    #--------------------
                    text_buf = f"Comparison Count Match Event Process : AioGetCntStatus = {lret.value} : {error_string.value.decode('sjis')}"
                    self.ui.textbox_return_string_var.set(text_buf)
                    return
                #----------------------------------------
                # Create the status string
                #----------------------------------------
                cnt_status_string = self.GetStatusString(cnt_status)
                #----------------------------------------
                # Display the status
                #----------------------------------------
                self.ui.textbox_status_var.set(f"{cnt_status.value:08x}H ({cnt_status_string})")
                #----------------------------------------
                # Display the comparison count match event occurrence count
                #----------------------------------------
                self.event_count += 1
                text_buf = f"{self.event_count}"
                self.ui.textbox_event_count_var.set(text_buf)
                #----------------------------------------
                # Reset status
                #----------------------------------------
                lret.value = caio.AioResetCntStatus(self.id, self.channel, caio.CNTS_DATA_NUM)
                #----------------------------------------
                # Error process
                #----------------------------------------
                #--------------------
                # Exit the process if not completed successfully
                #--------------------
                if lret.value != 0:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Display message
                    #--------------------
                    text_buf = f"Comparison Count Match Event Process : AioResetCntStatus = {lret.value} : {error_string.value.decode('sjis')}"
                    self.ui.textbox_return_string_var.set(text_buf)
                    return
                #----------------------------------------
                # Display the message that event processing is completed
                #----------------------------------------
                self.ui.textbox_return_string_var.set("Comparison Count Match Event Process : Successful completion")
        #----------------------------------------
        # Counter operation error
        # Display the status
        #----------------------------------------
        if cnt_event == caio.AIOM_CNTE_ERR:
            #----------------------------------------
            # Get counter channel
            #----------------------------------------
            cnt_channel = wParam
            if cnt_channel == self.channel.value:
                #----------------------------------------
                # Get status
                #----------------------------------------
                lret.value = caio.AioGetCntStatus(self.id, self.channel, ctypes.byref(cnt_status))
                #----------------------------------------
                # Error process
                #----------------------------------------
                #--------------------
                # Exit the process if not completed successfully
                #--------------------
                if lret.value != 0:
                    #--------------------
                    # Get error string
                    #--------------------
                    caio.AioGetErrorString(lret, error_string)
                    #--------------------
                    # Display message
                    #--------------------
                    text_buf = f"Counter Operation Error Occurrence Process : AioGetCntStatus = {lret.value} : {error_string.value.decode('sjis')}"
                    self.ui.textbox_return_string_var.set(text_buf)
                    return
                #----------------------------------------
                # Create the status string
                #----------------------------------------
                cnt_status_string = self.GetStatusString(cnt_status)
                #----------------------------------------
                # Display the status
                #----------------------------------------
                self.ui.textbox_status_var.set(f"{cnt_status.value:08x}H ({cnt_status_string})")
                #----------------------------------------
                # Display the message that event processing is completed
                #----------------------------------------
                self.ui.textbox_return_string_var.set("Counter Operation Error Occurrence Process : Successful completion")

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

        #----------------------------------------
        # Get device name from textbox
        #----------------------------------------
        device_name = self.ui.textbox_device_name_var.get()
        #----------------------------------------
        # Device initialization
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioInit(device_name.encode(), ctypes.byref(self.id))
        #----------------------------------------
        # Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # Display message
        #----------------------------------------
        text_buf = f"AioInit = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_return_string_var.set(text_buf)

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

        #----------------------------------------
        # Reset device
        #----------------------------------------
        lret.value = caio.AioResetDevice(self.id)
        #----------------------------------------
        # Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # Display message
        #----------------------------------------
        text_buf = f"AioResetDevice = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_return_string_var.set(text_buf)

    #========================================
    # Function name    OnChangedEditChannelNo
    # Parameter        event    Event(None Fixed)
    # Details          ChannelNo textbox changed event function
    # Return value     None
    #========================================
    def OnChangedEditChannelNo(self, event=None):
        #----------------------------------------
        # Get counter channel from textbox
        #----------------------------------------
        buf = self.ui.textbox_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:
            self.channel = ctypes.c_short(int(buf))

    #========================================
    # Function name    OnClickedButtonSetting
    # Parameter        None
    # Details          Set button click event function
    # Return value     None
    #========================================
    def OnClickedButtonSetting(self):
        #----------------------------------------
        # Open the condition setting dialog
        #----------------------------------------
        Setting.SettingWindow(self)
        if self.setting_ok:
            #----------------------------------------
            # Display the message that setting is completed
            #----------------------------------------
            text_buf = "Set : Successful completion"
            self.ui.textbox_return_string_var.set(text_buf)

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

        #----------------------------------------
        # Get counter preset value from textbox
        #----------------------------------------
        buf = self.ui.textbox_preset_data_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:
            preset_data = ctypes.c_long(int(buf))
        #----------------------------------------
        # Preset the counter
        #----------------------------------------
        lret.value = caio.AioPresetCnt(self.id, self.channel, preset_data)
        #----------------------------------------
        # Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # Display message
        #----------------------------------------
        text_buf = f"AioPresetCnt = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_return_string_var.set(text_buf)
        #----------------------------------------
        # When the count value preset function is completed successfully
        #----------------------------------------
        if lret.value == 0:
            #--------------------
            # Get the count value
            #--------------------
            lret.value = caio.AioGetCntCount(self.id, self.channel, ctypes.byref(cnt_count))
            #--------------------
            # Error process
            #--------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"AioGetCntCount = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_return_string_var.set(text_buf)
                return
            #--------------------
            # Update count value
            #--------------------
            text_buf = f"{cnt_count.value}"
            self.ui.textbox_count_var.set(text_buf)

    #========================================
    # Function name    OnClickedButtonStart
    # Parameter        None
    # Details          Count Start button click event function
    # Return value     None
    #========================================
    def OnClickedButtonStart(self):
        cnt_count = ctypes.c_long()                        # Count value
        cnt_status = ctypes.c_long()                       # Counter status
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code

        #----------------------------------------
        # Set the callback routine: 'Comparison count match event', 'Counter operation error'.
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        self.pcnt_callback = caio.PAIO_CNT_CALLBACK(self.CallBackProc)
        self.user_data = ctypes.cast(ctypes.pointer(ctypes.py_object(self)), ctypes.c_void_p)
        lret.value = caio.AioSetCntCallBackProc(self.id, self.channel, self.pcnt_callback, (caio.CNTE_DATA_NUM | caio.CNTE_ERR), self.user_data)
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Start : AioSetCntCallBackProc = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Reset status
        #----------------------------------------
        lret.value = caio.AioResetCntStatus(self.id, self.channel, caio.CNTS_DATA_NUM | caio.CNTS_ORERR)
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Start : AioResetCntStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Initialize comparison match count
        #----------------------------------------
        self.event_count = 0
        #----------------------------------------
        # Start the counter action
        #----------------------------------------
        lret.value = caio.AioStartCnt(self.id, self.channel)
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Start : AioStartCnt = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Get status
        #----------------------------------------
        lret.value = caio.AioGetCntStatus(self.id, self.channel, ctypes.byref(cnt_status))
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Start : AioGetCntStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Create the status string
        #----------------------------------------
        cnt_status_string = self.GetStatusString(cnt_status)
        #----------------------------------------
        # Display the status
        #----------------------------------------
        self.ui.textbox_status_var.set(f"{cnt_status.value:08x}H ({cnt_status_string})")
        #----------------------------------------
        # Get count value
        #----------------------------------------
        lret.value = caio.AioGetCntCount(self.id, self.channel, ctypes.byref(cnt_count))
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Start : AioGetCntCount = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Display count value
        #----------------------------------------
        text_buf = f"{cnt_count.value}"
        self.ui.textbox_count_var.set(text_buf)
        #----------------------------------------
        # Update the comparison match count
        #----------------------------------------
        self.ui.textbox_event_count_var.set("0")
        #----------------------------------------
        # Display the message that count start is completed
        #----------------------------------------
        self.ui.textbox_return_string_var.set("Count Start : Successful completion")

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

        #----------------------------------------
        # Get the count value
        #----------------------------------------
        lret.value = caio.AioGetCntCount(self.id, self.channel, ctypes.byref(cnt_count))
        #----------------------------------------
        # Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # Display message
        #----------------------------------------
        text_buf = f"AioGetCntCount = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_return_string_var.set(text_buf)
        #----------------------------------------
        # When the count value acquisition function is completed successfully
        #----------------------------------------
        if lret.value == 0:
            #--------------------
            # Update count value
            #--------------------
            text_buf = f"{cnt_count.value}"
            self.ui.textbox_count_var.set(text_buf)

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

        #----------------------------------------
        # Get status
        #----------------------------------------
        lret.value = caio.AioGetCntStatus(self.id, self.channel, ctypes.byref(cnt_status))
        #----------------------------------------
        # Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # Display message
        #----------------------------------------
        text_buf = f"AioGetCntStatus = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_return_string_var.set(text_buf)
        #----------------------------------------
        # When the status acquisition function is completed successfully
        #----------------------------------------
        if lret.value == 0:
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            cnt_status_string = self.GetStatusString(cnt_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_status_var.set(f"{cnt_status.value:08x}H ({cnt_status_string})")

    #========================================
    # Function name    OnClickedButtonStop
    # Parameter        None
    # Details          Count Stop button click event function
    # Return value     None
    #========================================
    def OnClickedButtonStop(self):
        cnt_count = ctypes.c_long()                        # Count value
        cnt_status = ctypes.c_long()                       # Status
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code

        #----------------------------------------
        # Stop the counter action
        #----------------------------------------
        lret.value = caio.AioStopCnt(self.id, self.channel)
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Stop : AioStopCnt = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Get the count value
        #----------------------------------------
        lret.value = caio.AioGetCntCount(self.id, self.channel, ctypes.byref(cnt_count))
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Stop : AioGetCntCount = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Display count value
        #----------------------------------------
        text_buf = f"{cnt_count.value}"
        self.ui.textbox_count_var.set(text_buf)
        #----------------------------------------
        # Get status
        #----------------------------------------
        lret.value = caio.AioGetCntStatus(self.id, self.channel, ctypes.byref(cnt_status))
        #----------------------------------------
        # Error process
        #----------------------------------------
        #--------------------
        # Exit the process if not completed successfully
        #--------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Count Stop : AioGetCntStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Create the status string
        #----------------------------------------
        cnt_status_string = self.GetStatusString(cnt_status)
        #----------------------------------------
        # Display the status
        #----------------------------------------
        self.ui.textbox_status_var.set(f"{cnt_status.value:08x}H ({cnt_status_string})")
        #----------------------------------------
        # Display the message that count stop is completed
        #----------------------------------------
        self.ui.textbox_return_string_var.set("Count Stop : Successful completion")

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

        #----------------------------------------
        # Exit process
        #----------------------------------------
        lret.value = caio.AioExit(self.id)
        #----------------------------------------
        # Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # Display message
        #----------------------------------------
        text_buf = f"AioExit = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_return_string_var.set(text_buf)

    #========================================
    # Function name    OnClickedButtonClose
    # Parameter        None
    # Details          Close button click event function
    # Return value     None
    #========================================
    def OnClickedButtonClose(self):
        #----------------------------------------
        # Exit process
        #----------------------------------------
        caio.AioExit(self.id)
        #----------------------------------------
        # Close window
        #----------------------------------------
        self.ui.root.destroy()

    #========================================
    # Function name    run
    # Parameter        None
    # Details          Run the application function
    # Return value     None
    #========================================
    def run(self):
        self.ui.run()


#========================================
# main function
#========================================
def main():
    #----------------------------------------
    # Create and Run Counter
    #----------------------------------------
    instance = Counter()
    instance.run()

#========================================
# Call main function
#========================================
if __name__ == "__main__":
    main()