﻿#================================================================
#================================================================
# API-AIO(WDM)
# Sampling synchronization between 2 devices using synchronization bus sample
# main file
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import ctypes
import caio
from SyncBusSamplingUI import SyncBusSamplingUI


#========================================
# SyncBusSampling class
#========================================
class SyncBusSampling:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Variable
        #----------------------------------------
        self.DISPLAY_SAMPLING_COUNT = 1000    # Maximum sampling count of sampling data displayed in listbox
        self.master_id = ctypes.c_short()     # ID for Master device access
        self.slave_id = ctypes.c_short()      # ID for Slave device access
        self.master_sampling_count = 0        # Master total sampling count
        self.slave_sampling_count = 0         # Slave total sampling count
        self.master_sampling_data = None      # Master Pointer to sampling data storage area
        self.slave_sampling_data = None       # Slave  Pointer to sampling data storage area
        self.ai_range = (  caio.PM10,         # +/-10V
                           caio.PM5,          # +/-5V
                           caio.PM25,         # +/-2.5V
                           caio.PM125,        # +/-1.25V
                           caio.P10,          # 0-10V
                           caio.P5,           # 0-5V
                           caio.P25,          # 0-2.5V
                           caio.P125,         # 0-1.25V
                           caio.P20MA,        # 0-20mA
                           caio.P4TO20MA,     # 4-20mA
                           caio.PM20MA,       # +/-20mA
                           caio.P1TO5)        # 1-5V
        self.master_pai_callback = None       # Pointer to master callback function
        self.master_pai_user_data = None      # User data of master callback function
        self.slave_pai_callback = None        # Pointer to slave callback function
        self.slave_pai_user_data = None       # User data of slave callback function
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self.ui = SyncBusSamplingUI()
        #----------------------------------------
        # Set the callback function for the UI controls
        #----------------------------------------
        self.ui.setup_commands(
            command_aio_init=self.OnClickedButtonAioInit,
            command_aio_reset_device=self.OnClickedButtonAioResetDevice,
            command_ai_setting=self.OnClickedButtonAiSetting,
            command_sampling_setting=self.OnClickedButtonSamplingSetting,
            command_sync_setting=self.OnClickedButtonSyncSetting,
            command_sampling_start=self.OnClickedButtonSamplingStart,
            command_get_status=self.OnClickedButtonGetStatus,
            command_sampling_stop=self.OnClickedButtonSamplingStop,
            command_master_output_sampling_data=self.OnClickedButtonMasterOutputSamplingData,
            command_slave_output_sampling_data=self.OnClickedButtonSlaveOutputSamplingData,
            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         ai_status           Status
    # Details           Function to get status string
    # Return value      ai_status_string    Status string
    #========================================
    @staticmethod
    def GetStatusString(ai_status):
        #----------------------------------------
        # Create the analog input status string
        #----------------------------------------
        #--------------------
        # When status 'Device is running' is ON
        #--------------------
        if (ai_status.value & caio.AIS_BUSY) == caio.AIS_BUSY:
            ai_status_string = "Operating"
        #--------------------
        # When status 'Device is running' is OFF
        #--------------------
        else:
            ai_status_string = "Stop"
        #--------------------
        # When status 'Wait the start trigger' is ON
        #--------------------
        if (ai_status.value & caio.AIS_START_TRG) == caio.AIS_START_TRG:
            ai_status_string += ", Wait the start trigger"
        #--------------------
        # When status 'The specified number of data are stored' is ON
        #--------------------
        if (ai_status.value & caio.AIS_DATA_NUM) == caio.AIS_DATA_NUM:
            ai_status_string += ", The specified number of data are stored"
        #--------------------
        # When status 'Overflow' is ON
        #--------------------
        if (ai_status.value & caio.AIS_OFERR) == caio.AIS_OFERR:
            ai_status_string += ", Overflow"
        #--------------------
        # When status 'Sampling clock error' is ON
        #--------------------
        if (ai_status.value & caio.AIS_SCERR) == caio.AIS_SCERR:
            ai_status_string += ", Sampling clock error"
        #--------------------
        # When status 'AD conversion error' is ON
        #--------------------
        if (ai_status.value & caio.AIS_AIERR) == caio.AIS_AIERR:
            ai_status_string += ", AD conversion error"
        #--------------------
        # When status 'Driver spec error' is ON
        #--------------------
        if (ai_status.value & caio.AIS_DRVERR) == caio.AIS_DRVERR:
            ai_status_string += ", Driver spec error"
        return ai_status_string

    #========================================
    # Function name    SamplingMasterCallBackProc
    # Parameter        id          Id get by AioInit
    # Parameter        ai_event    The event that occurred
    # Parameter        wParam      Parameter passed from driver
    # Parameter        lParam      Parameter unique to each event
    # Parameter        Param       Parameter specified in the AioSetAiCallBackProc function
    # Details          Function registered with the AioSetAiCallBackProc function
    #                  This function is called when an event occurs
    # Return value     None
    #========================================
    @staticmethod
    def SamplingMasterCallBackProc(id, ai_event, wParam, lParam, Param):
        ai_status = ctypes.c_long()                        # Status
        ai_sampling_count = ctypes.c_long()                # Sampling count
        ai_channels = ctypes.c_short()                     # Number of channels
        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
        #----------------------------------------
        # Event that device operation end
        # Acquire and display data
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_END:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.master_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_master_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Get the current sampling count
            #----------------------------------------
            lret.value = caio.AioGetAiSamplingCount(self.master_id, ctypes.byref(ai_sampling_count))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiSamplingCount = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Display sampling count
            #----------------------------------------
            text_buf = f"{ai_sampling_count.value}"
            self.ui.textbox_master_ai_sampling_count_var.set(text_buf)
            #----------------------------------------
            # Get the number of channels
            #----------------------------------------
            lret.value = caio.AioGetAiChannels(self.master_id, ctypes.byref(ai_channels))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Allocate the array for storing conversion data
            #----------------------------------------
            if self.master_sampling_data is not None:
                self.master_sampling_data = None
            try:
                self.master_sampling_data = (ctypes.c_float * (ai_sampling_count.value * ai_channels.value))()
            except:
                self.ui.textbox_master_return_string_var.set("Device Operation End Event Process : Failed to allocate the array for storing data")
                return
            #----------------------------------------
            # Get the sampling data
            #----------------------------------------
            lret.value = caio.AioGetAiSamplingDataEx(self.master_id, ctypes.byref(ai_sampling_count), self.master_sampling_data)
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiSamplingDataEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Update the total sampling count
            #----------------------------------------
            self.master_sampling_count = ai_sampling_count.value
            #----------------------------------------
            # Set the loop count for the sampling data display loop
            #----------------------------------------
            #--------------------
            # When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
            #--------------------
            if ai_sampling_count.value > self.DISPLAY_SAMPLING_COUNT:
                #--------------------
                # Limit the maximum number of loops to the data display maximum sampling count
                #--------------------
                loop_max_count = self.DISPLAY_SAMPLING_COUNT
                #--------------------
                # Set the top address of the buffer to be referenced to the address
                # before the maximum sampling count for data display from the end of the acquired data
                #--------------------
                top_address = ai_sampling_count.value - self.DISPLAY_SAMPLING_COUNT
            #--------------------
            # When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
            #--------------------
            else:
                #--------------------
                # Set the number of total sampling data to the maximum loop number
                #--------------------
                loop_max_count = ai_sampling_count.value
                #--------------------
                # Set the top address of the referenced buffer to 0
                #--------------------
                top_address = 0
            #----------------------------------------
            # Display sampling data
            # (Display from the end of acquired data to the maximum number of data display sampling count)
            #----------------------------------------
            #--------------------
            # When the sampling count of the data acquired this time is 1 or more
            #--------------------
            if ai_sampling_count.value > 0:
                #--------------------
                # Clear the listbox for sampling data display
                #--------------------
                self.ui.listbox_master_ai_sampling_data_var.set([""])
                data = []
                #--------------------
                # Repeat the process up to the maximum sampling count of the display data
                #--------------------
                for i in range(top_address, loop_max_count + top_address):
                    #--------------------
                    # Generate display string
                    #--------------------
                    text_string = f"{(i + 1):5d}:  "
                    #--------------------
                    # Repeat the process for the specified number of channels
                    #--------------------
                    for j in range(ai_channels.value):
                        text_string += f" {self.master_sampling_data[i * ai_channels.value + j]:10.5f}"
                    #----------------------------------------
                    # Collect sampling data
                    #----------------------------------------
                    data.append(text_string)
                #----------------------------------------
                # Display sampling start data
                #----------------------------------------
                self.ui.listbox_master_ai_sampling_data_var.set(data)
            #----------------------------------------
            # Display sampling start data
            #----------------------------------------
            self.ui.textbox_master_return_string_var.set("Device Operation End Event Process : Successful completion")
        #----------------------------------------
        # Event that overflow
        # Display the status
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_OFERR:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.master_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Overflow Occurrence Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_master_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_master_return_string_var.set("Overflow Occurrence Process : Successful completion")
        #----------------------------------------
        # Event that sampling clock error
        # Display the status
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_SCERR:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.master_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Sampling Clock Error Occurrence Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_master_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_master_return_string_var.set("Sampling Clock Error Occurrence Process : Successful completion")
        #----------------------------------------
        # Event that AD conversion error
        # Display the status
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_ADERR:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.master_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"AD Conversion Error Occurrence Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_master_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_master_return_string_var.set("AD Conversion Error Occurrence Process : Successful completion")

    #========================================
    # Function name    SamplingSlaveCallBackProc
    # Parameter        id          Id get by AioInit
    # Parameter        ai_event    The event that occurred
    # Parameter        wParam      Parameter passed from driver
    # Parameter        lParam      Parameter unique to each event
    # Parameter        Param       Parameter specified in the AioSetAiCallBackProc function
    # Details          Function registered with the AioSetAiCallBackProc function
    #                  This function is called when an event occurs
    # Return value     None
    #========================================
    @staticmethod
    def SamplingSlaveCallBackProc(id, ai_event, wParam, lParam, Param):
        ai_status = ctypes.c_long()                        # Status
        ai_sampling_count = ctypes.c_long()                # Sampling count
        ai_channels = ctypes.c_short()                     # Number of channels
        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
        #----------------------------------------
        # Event that AD conversion start
        # Display the status
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_START:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Event that AD conversion start Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_slave_return_string_var.set("Event that AD conversion start Process : Successful completion")
        #----------------------------------------
        # Event that device operation end
        # Acquire and display data
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_END:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Get the current sampling count
            #----------------------------------------
            lret.value = caio.AioGetAiSamplingCount(self.slave_id, ctypes.byref(ai_sampling_count))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiSamplingCount = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Display sampling count
            #----------------------------------------
            text_buf = f"{ai_sampling_count.value}"
            self.ui.textbox_slave_ai_sampling_count_var.set(text_buf)
            #----------------------------------------
            # Get the number of channels
            #----------------------------------------
            lret.value = caio.AioGetAiChannels(self.slave_id, ctypes.byref(ai_channels))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Allocate an array for storing data
            #----------------------------------------
            if self.slave_sampling_data is not None:
                self.slave_sampling_data = None
            try:
                self.slave_sampling_data = (ctypes.c_float * (ai_sampling_count.value * ai_channels.value))()
            except:
                self.ui.textbox_slave_return_string_var.set("Device Operation End Event Process : Failed to allocate an array for storing data")
                return
            #----------------------------------------
            # Get sampling data
            #----------------------------------------
            lret.value = caio.AioGetAiSamplingDataEx(self.slave_id, ctypes.byref(ai_sampling_count), self.slave_sampling_data)
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Device Operation End Event Process : AioGetAiSamplingDataEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Update the total sampling count
            #----------------------------------------
            self.slave_sampling_count = ai_sampling_count.value
            #----------------------------------------
            # Set the loop count for the sampling data display loop
            #----------------------------------------
            #--------------------
            # When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
            #--------------------
            if ai_sampling_count.value > self.DISPLAY_SAMPLING_COUNT:
                #--------------------
                # Limit the maximum number of loops to the data display maximum sampling count
                #--------------------
                loop_max_count = self.DISPLAY_SAMPLING_COUNT
                #--------------------
                # Set the top address of the buffer to be referenced to the address
                # before the maximum sampling count for data display from the end of the acquired data
                #--------------------
                top_address = ai_sampling_count.value - self.DISPLAY_SAMPLING_COUNT
            #--------------------
            # When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
            #--------------------
            else:
                #--------------------
                # Set the number of total sampling data to the maximum loop number
                #--------------------
                loop_max_count = ai_sampling_count.value
                #--------------------
                # Set the top address of the referenced buffer to 0
                #--------------------
                top_address = 0
            #----------------------------------------
            # Display sampling data
            # (Display from the end of acquired data to the maximum number of data display sampling count)
            #----------------------------------------
            #--------------------
            # When the sampling count of the data acquired this time is 1 or more
            #--------------------
            if ai_sampling_count.value > 0:
                #--------------------
                # Clear the listbox for sampling data display
                #--------------------
                self.ui.listbox_slave_ai_sampling_data_var.set([""])
                data = []
                #--------------------
                # Repeat the process up to the maximum sampling count of the display data
                #--------------------
                for i in range(top_address, loop_max_count + top_address):
                    #--------------------
                    # Generate display string
                    #--------------------
                    text_string = f"{(i + 1):5d}:  "
                    #--------------------
                    # Repeat the process for the specified number of channels
                    #--------------------
                    for j in range(ai_channels.value):
                        text_string += f" {self.slave_sampling_data[i * ai_channels.value + j]:10.5f}"
                    #----------------------------------------
                    # Collect sampling data
                    #----------------------------------------
                    data.append(text_string)
                #----------------------------------------
                # Display sampling start data
                #----------------------------------------
                self.ui.listbox_slave_ai_sampling_data_var.set(data)
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_slave_return_string_var.set("Device Operation End Event Process : Successful completion")
        #----------------------------------------
        # Event that overflow
        # Display the status
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_OFERR:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Overflow Occurrence Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_slave_return_string_var.set("Overflow Occurrence Process : Successful completion")
        #----------------------------------------
        # Event that sampling clock error
        # Display the status
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_SCERR:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Sampling Clock Error Occurrence Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_slave_return_string_var.set("Sampling Clock Error Occurrence Process : Successful completion")
        #----------------------------------------
        # Event that AD conversion error
        # Display the status
        #----------------------------------------
        if ai_event == caio.AIOM_AIE_ADERR:
            #----------------------------------------
            # Get status
            #----------------------------------------
            lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
            #----------------------------------------
            # Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"AD Conversion Error Occurrence Process : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # Create the status string
            #----------------------------------------
            ai_status_string = self.GetStatusString(ai_status)
            #----------------------------------------
            # Display the status
            #----------------------------------------
            self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
            #----------------------------------------
            # Display the message that event processing is completed
            #----------------------------------------
            self.ui.textbox_slave_return_string_var.set("AD Conversion 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

        #----------------------------------------
        # [Master] Get device name from textbox
        #----------------------------------------
        device_name = self.ui.textbox_master_device_name_var.get()
        #----------------------------------------
        # [Master] Device initialization
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioInit(device_name.encode(), ctypes.byref(self.master_id))
        #----------------------------------------
        # [Master] Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # [Master] Display message
        #----------------------------------------
        text_buf = f"AioInit = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_master_return_string_var.set(text_buf)
        #----------------------------------------
        # [Slave] Get device name from textbox
        #----------------------------------------
        device_name = self.ui.textbox_slave_device_name_var.get()
        #----------------------------------------
        # [Slave] Device initialization
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioInit(device_name.encode(), ctypes.byref(self.slave_id))
        #----------------------------------------
        # [Slave] Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # [Slave] Display message
        #----------------------------------------
        text_buf = f"AioInit = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_slave_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

        #----------------------------------------
        # [Master] Reset device
        #----------------------------------------
        lret.value = caio.AioResetDevice(self.master_id)
        #----------------------------------------
        # [Master] Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # [Master] Display message
        #----------------------------------------
        text_buf = f"AioResetDevice = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_master_return_string_var.set(text_buf)
        #----------------------------------------
        # [Slave] Reset device
        #----------------------------------------
        lret.value = caio.AioResetDevice(self.slave_id)
        #----------------------------------------
        # [Slave] Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # [Slave] Display message
        #----------------------------------------
        text_buf = f"AioResetDevice = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_slave_return_string_var.set(text_buf)

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

        #----------------------------------------
        # [Master] Get the analog input range setting value from combobox
        #----------------------------------------
        ai_range = ctypes.c_short(self.ai_range[self.ui.combobox_master_ai_range.current()])
        #----------------------------------------
        # [Master] Set the analog input range (all channels)
        #----------------------------------------
        lret.value = caio.AioSetAiRangeAll(self.master_id, ai_range)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Analog Input Set : AioSetAiRangeAll = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Get the input method setting value from combobox
        #----------------------------------------
        input_method = ctypes.c_short(self.ui.combobox_master_ai_input_method.current())
        #----------------------------------------
        # [Master] Set the analog input method
        #----------------------------------------
        lret.value = caio.AioSetAiInputMethod(self.master_id, input_method)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Analog Input Set : AioSetAiInputMethod = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Display the message that analog input settings is completed
        #----------------------------------------
        self.ui.textbox_master_return_string_var.set("Analog Input Set : Successful completion")
        #----------------------------------------
        # [Slave] Get the analog input range setting value from combobox
        #----------------------------------------
        ai_range = ctypes.c_short(self.ai_range[self.ui.combobox_slave_ai_range.current()])
        #----------------------------------------
        # [Slave] Set the analog input range (all channels)
        #----------------------------------------
        lret.value = caio.AioSetAiRangeAll(self.slave_id, ai_range)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Analog Input Set : AioSetAiRangeAll = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Get the input method setting value from combobox
        #----------------------------------------
        input_method = ctypes.c_short(self.ui.combobox_slave_ai_input_method.current())
        #----------------------------------------
        # [Slave] Set the analog input method
        #----------------------------------------
        lret.value = caio.AioSetAiInputMethod(self.slave_id, input_method)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Analog Input Set : AioSetAiInputMethod = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Display the message that analog input settings is completed
        #----------------------------------------
        self.ui.textbox_slave_return_string_var.set("Analog Input Set : Successful completion")

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

        #----------------------------------------
        # [Slave] Get the number of channels
        #----------------------------------------
        buf = self.ui.textbox_slave_ai_channels_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_channels = ctypes.c_short(int(buf))
        #----------------------------------------
        # [Slave] Set the number of analog input channels used for conversion
        #----------------------------------------
        lret.value = caio.AioSetAiChannels(self.slave_id, ai_channels)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Set the analog input conversion data transfer mode to device buffer mode
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiTransferMode(self.slave_id, 0)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiTransferMode = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Set the data storage memory type to FIFO
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiMemoryType(self.slave_id, 0)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiMemoryType = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Set the sampling start condition to Event controller output
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiStartTrigger(self.slave_id, 10)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiStartTrigger = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Set the clock type to Event controller output
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiClockType(self.slave_id, 10)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiClockType = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Set the sampling stop condition to Event controller output
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiStopTrigger(self.slave_id, 10)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiStopTrigger = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Set the callback routine: 'Event that AD conversion start', 'Event that device operation end', 
        # 'Event that overflow', 'Event that sampling clock error', and 'Event that AD conversion error'.
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        self.slave_pai_callback = caio.PAIO_AI_CALLBACK(self.SamplingSlaveCallBackProc)
        self.slave_pai_user_data = ctypes.cast(ctypes.pointer(ctypes.py_object(self)), ctypes.c_void_p)
        lret.value = caio.AioSetAiCallBackProc(self.slave_id, self.slave_pai_callback, (caio.AIE_START | caio.AIE_END | caio.AIE_OFERR | caio.AIE_SCERR | caio.AIE_ADERR), self.slave_pai_user_data)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiCallBackProc = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Display the message that sampling settings is completed
        #----------------------------------------
        self.ui.textbox_slave_return_string_var.set("Sampling Set : Successful completion")
        #----------------------------------------
        # [Master] Get the number of channels
        #----------------------------------------
        buf = self.ui.textbox_master_ai_channels_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_channels = ctypes.c_short(int(buf))
        #----------------------------------------
        # [Master] Set the number of analog input channels used for conversion
        #----------------------------------------
        lret.value = caio.AioSetAiChannels(self.master_id, ai_channels)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Set the analog input conversion data transfer mode to device buffer mode
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiTransferMode(self.master_id, 0)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiTransferMode = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Set the data storage memory type to FIFO
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiMemoryType(self.master_id, 0)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiMemoryType = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Set the sampling start condition to Software
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiStartTrigger(self.master_id, 0)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiStartTrigger = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Set the clock type to Internal Clock
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiClockType(self.master_id, 0)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiClockType = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Get the internal clock conversion speed from textbox
        #----------------------------------------
        buf = self.ui.textbox_master_ai_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))
        #----------------------------------------
        # [Master] Set the internal clock conversion speed
        #----------------------------------------
        lret.value = caio.AioSetAiSamplingClock(self.master_id, ai_sampling_clock)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiSamplingClock = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Set the sampling stop condition to Stop conversion by the specified times
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        lret.value = caio.AioSetAiStopTrigger(self.master_id, 0)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiStopTrigger = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Get the sampling stop times from textbox
        #----------------------------------------
        buf = self.ui.textbox_master_ai_stop_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:
            stop_times = ctypes.c_long(int(buf))
        #----------------------------------------
        # [Master] Set the sampling stop times
        #----------------------------------------
        lret.value = caio.AioSetAiStopTimes(self.master_id, stop_times)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiStopTimes = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Set the callback routine: 'Event that device operation end', 'Event that overflow', 
        # 'Event that sampling clock error', and 'Event that AD conversion error'.
        # Caution for modify this function (if modified, the application may not work)
        #----------------------------------------
        self.master_pai_callback = caio.PAIO_AI_CALLBACK(self.SamplingMasterCallBackProc)
        self.master_pai_user_data = ctypes.cast(ctypes.pointer(ctypes.py_object(self)), ctypes.c_void_p)
        lret.value = caio.AioSetAiCallBackProc(self.master_id, self.master_pai_callback, (caio.AIE_END | caio.AIE_OFERR | caio.AIE_SCERR | caio.AIE_ADERR), self.master_pai_user_data)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Set : AioSetAiCallBackProc = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Display the message that sampling settings is completed
        #----------------------------------------
        self.ui.textbox_master_return_string_var.set("Sampling Set : Successful completion")

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

        #----------------------------------------
        # [Master] Event Controller Setting : Connect Analog input internal clock signal to Synchronization bus master signal 1
        #----------------------------------------
        lret.value = caio.AioSetEcuSignal(self.master_id, caio.AIOECU_DEST_MASTER1, caio.AIOECU_SRC_AI_CLK)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sync Operation Set : AioSetEcuSignal = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Event Controller Setting : Connect Ai software start signal to Synchronization bus master signal 2
        #----------------------------------------
        lret.value = caio.AioSetEcuSignal(self.master_id, caio.AIOECU_DEST_MASTER2, caio.AIOECU_SRC_START)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sync Operation Set : AioSetEcuSignal = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Event Controller Setting: Connect Analog input signal that data of the specified sampling times have been converted (No delay) to Synchronization bus master signal 3
        #----------------------------------------
        lret.value = caio.AioSetEcuSignal(self.master_id, caio.AIOECU_DEST_MASTER3, caio.AIOECU_SRC_AI_STOP)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sync Operation Set : AioSetEcuSignal = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Display the message that synchronous operation settings is completed
        #----------------------------------------
        self.ui.textbox_master_return_string_var.set("Sync Operation Set : Successful completion")
        #----------------------------------------
        # [Slave] Event Controller Setting : Connect Synchronization bus slave signal 1 to Analog input sampling clock
        #----------------------------------------
        lret.value = caio.AioSetEcuSignal(self.slave_id, caio.AIOECU_DEST_AI_CLK, caio.AIOECU_SRC_SLAVE1)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sync Operation Set : AioSetEcuSignal = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Event Controller Setting : Connect Synchronization bus slave signal 2 to Analog input converting start signal
        #----------------------------------------
        lret.value = caio.AioSetEcuSignal(self.slave_id, caio.AIOECU_DEST_AI_START, caio.AIOECU_SRC_SLAVE2)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sync Operation Set : AioSetEcuSignal = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Event Controller Setting : Connect Synchronization bus slave signal 3 to Analog input converting stop signal
        #----------------------------------------
        lret.value = caio.AioSetEcuSignal(self.slave_id, caio.AIOECU_DEST_AI_STOP, caio.AIOECU_SRC_SLAVE3)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sync Operation Set : AioSetEcuSignal = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Display the message that synchronous operation settings is completed
        #----------------------------------------
        self.ui.textbox_slave_return_string_var.set("Sync Operation Set : Successful completion")

    #========================================
    # Function name    OnClickedButtonSamplingStart
    # Parameter        None
    # Details          Sampling Start button click event function
    # Return value     None
    #========================================
    def OnClickedButtonSamplingStart(self):
        ai_status = ctypes.c_long()                        # Analog input status
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code

        #----------------------------------------
        # [Slave] Clear memory
        #----------------------------------------
        lret.value = caio.AioResetAiMemory(self.slave_id)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioResetAiMemory = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Clear status
        #----------------------------------------
        lret.value = caio.AioResetAiStatus(self.slave_id)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioResetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Initialize the total sampling count
        #----------------------------------------
        self.slave_sampling_count = 0
        #----------------------------------------
        # [Slave] Start sampling
        #----------------------------------------
        lret.value = caio.AioStartAi(self.slave_id)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioStartAi = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Get status
        #----------------------------------------
        lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Create the status string
        #----------------------------------------
        ai_status_string = self.GetStatusString(ai_status)
        #----------------------------------------
        # [Slave] Display status
        #----------------------------------------
        self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
        #----------------------------------------
        # [Slave] Update the sampling count
        #----------------------------------------
        self.ui.textbox_slave_ai_sampling_count_var.set("0")
        #----------------------------------------
        # [Slave] Clear the listbox for sampling data display
        #----------------------------------------
        self.ui.listbox_slave_ai_sampling_data_var.set([""])
        #----------------------------------------
        # [Slave] Display the message that sampling start is completed
        #----------------------------------------
        self.ui.textbox_slave_return_string_var.set("Sampling Start : Successful completion")
        #----------------------------------------
        # [Master] Clear memory
        #----------------------------------------
        lret.value = caio.AioResetAiMemory(self.master_id)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioResetAiMemory = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Clear status
        #----------------------------------------
        lret.value = caio.AioResetAiStatus(self.master_id)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioResetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Initialize the total sampling count
        #----------------------------------------
        self.master_sampling_count = 0
        #----------------------------------------
        # [Master] Start sampling
        #----------------------------------------
        lret.value = caio.AioStartAi(self.master_id)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioStartAi = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Get status
        #----------------------------------------
        lret.value = caio.AioGetAiStatus(self.master_id, ctypes.byref(ai_status))
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Sampling Start : AioStartAi = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Create the status string
        #----------------------------------------
        ai_status_string = self.GetStatusString(ai_status)
        #----------------------------------------
        # [Master] Display status
        #----------------------------------------
        self.ui.textbox_master_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
        #----------------------------------------
        # [Master] Update the sampling count
        #----------------------------------------
        self.ui.textbox_master_ai_sampling_count_var.set("0")
        #----------------------------------------
        # [Master] Clear the listbox for sampling data display
        #----------------------------------------
        self.ui.listbox_master_ai_sampling_data_var.set([""])
        #----------------------------------------
        # [Master] Display the message that sampling start is completed
        #----------------------------------------
        self.ui.textbox_master_return_string_var.set("Sampling Start : Successful completion")

    #========================================
    # Function name    OnClickedButtonGetStatus
    # Parameter        None
    # Details          Get Status button click event function
    # Return value     None
    #========================================
    def OnClickedButtonGetStatus(self):
        ai_sampling_count = ctypes.c_long()                # Sampling count
        ai_status = ctypes.c_long()                        # Sampling status
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code

        #----------------------------------------
        # [Slave] Get the sampling count
        #----------------------------------------
        lret.value = caio.AioGetAiSamplingCount(self.slave_id, ctypes.byref(ai_sampling_count))
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Get Status : AioGetAiSamplingCount = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Update the sampling count
        #----------------------------------------
        text_buf = f"{ai_sampling_count.value}"
        self.ui.textbox_slave_ai_sampling_count_var.set(text_buf)
        #----------------------------------------
        # [Slave] Get status
        #----------------------------------------
        lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Get Status : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Create the status string
        #----------------------------------------
        ai_status_string = self.GetStatusString(ai_status)
        #----------------------------------------
        # [Slave] Display status
        #----------------------------------------
        self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
        #----------------------------------------
        # [Slave] Display the message that get status is completed
        #----------------------------------------
        self.ui.textbox_slave_return_string_var.set("Get Status : Successful completion")
        #----------------------------------------
        # [Master] Get the sampling count
        #----------------------------------------
        lret.value = caio.AioGetAiSamplingCount(self.master_id, ctypes.byref(ai_sampling_count))
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Get Status : AioGetAiSamplingCount = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Update the sampling count
        #----------------------------------------
        text_buf = f"{ai_sampling_count.value}"
        self.ui.textbox_master_ai_sampling_count_var.set(text_buf)
        #----------------------------------------
        # [Master] Get status
        #----------------------------------------
        lret.value = caio.AioGetAiStatus(self.master_id, ctypes.byref(ai_status))
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Get Status : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # Create the status string
        #----------------------------------------
        ai_status_string = self.GetStatusString(ai_status)
        #----------------------------------------
        # [Master] Display status
        #----------------------------------------
        self.ui.textbox_master_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
        #----------------------------------------
        # [Master] Display the message that get status is completed
        #----------------------------------------
        self.ui.textbox_master_return_string_var.set("Get Status : Successful completion")

    #========================================
    # Function name    OnClickedButtonSamplingStop
    # Parameter        None
    # Details          Sampling Stop button click event function
    # Return value     None
    #========================================
    def OnClickedButtonSamplingStop(self):
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code
        ai_status = ctypes.c_long()                        # Status
        ai_sampling_count = ctypes.c_long()                # Sampling count
        ai_channels = ctypes.c_short()                     # Number of channels

        #----------------------------------------
        # [Master] Stop sampling
        #----------------------------------------
        lret.value = caio.AioStopAi(self.master_id)
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Stop sampling : AioStopAi = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Get status
        #----------------------------------------
        lret.value = caio.AioGetAiStatus(self.master_id, ctypes.byref(ai_status))
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Stop sampling : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Create the status string
        #----------------------------------------
        ai_status_string = self.GetStatusString(ai_status)
        #----------------------------------------
        # [Master] Display the status
        #----------------------------------------
        self.ui.textbox_master_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
        #----------------------------------------
        # [Master] Get the current sampling count
        #----------------------------------------
        lret.value = caio.AioGetAiSamplingCount(self.master_id, ctypes.byref(ai_sampling_count))
        #----------------------------------------
        # [Master] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Stop sampling : AioGetAiSamplingCount = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Master] Display sampling count
        #----------------------------------------
        text_buf = f"{ai_sampling_count.value}"
        self.ui.textbox_master_ai_sampling_count_var.set(text_buf)
        #----------------------------------------
        # [Master] When the sampling count of the data acquired this time is 1 or more
        #----------------------------------------
        if ai_sampling_count.value > 0:
            #----------------------------------------
            # [Master] Get the number of channels
            #----------------------------------------
            lret.value = caio.AioGetAiChannels(self.master_id, ctypes.byref(ai_channels))
            #----------------------------------------
            # [Master] Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Stop sampling : AioGetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # [Master] Allocate the array for storing conversion data
            #----------------------------------------
            if self.master_sampling_data is not None:
                self.master_sampling_data = None
            try:
                self.master_sampling_data = (ctypes.c_float * (ai_sampling_count.value * ai_channels.value))()
            except:
                self.ui.textbox_master_return_string_var.set("Stop sampling : Failed to allocate the array for storing data")
                return
            #----------------------------------------
            # [Master] Get the sampling data
            #----------------------------------------
            lret.value = caio.AioGetAiSamplingDataEx(self.master_id, ctypes.byref(ai_sampling_count), self.master_sampling_data)
            #----------------------------------------
            # [Master] Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Stop sampling : AioGetAiSamplingDataEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_master_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # [Master] Update the total sampling count
            #----------------------------------------
            self.master_sampling_count = ai_sampling_count.value
            #----------------------------------------
            # [Master] Set the loop count for the sampling data display loop
            #----------------------------------------
            #--------------------
            # When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
            #--------------------
            if ai_sampling_count.value > self.DISPLAY_SAMPLING_COUNT:
                #--------------------
                # Limit the maximum number of loops to the data display maximum sampling count
                #--------------------
                loop_max_count = self.DISPLAY_SAMPLING_COUNT
                #--------------------
                # Set the top address of the buffer to be referenced to the address
                # before the maximum sampling count for data display from the end of the acquired data
                #--------------------
                top_address = ai_sampling_count.value - self.DISPLAY_SAMPLING_COUNT
            #--------------------
            # When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
            #--------------------
            else:
                #--------------------
                # Set the number of total sampling data to the maximum loop number
                #--------------------
                loop_max_count = ai_sampling_count.value
                #--------------------
                # Set the top address of the referenced buffer to 0
                #--------------------
                top_address = 0
            #----------------------------------------
            # [Master] Display sampling data
            # (Display from the end of acquired data to the maximum number of data display sampling count)
            #----------------------------------------
            #--------------------
            # Clear the listbox for sampling data display
            #--------------------
            self.ui.listbox_master_ai_sampling_data_var.set([""])
            data = []
            #--------------------
            # Repeat the process up to the maximum sampling count of the display data
            #--------------------
            for i in range(top_address, loop_max_count + top_address):
                #--------------------
                # Generate display string
                #--------------------
                text_string = f"{(i + 1):5d}:  "
                #--------------------
                # Repeat the process for the specified number of channels
                #--------------------
                for j in range(ai_channels.value):
                    text_string += f" {self.master_sampling_data[i * ai_channels.value + j]:10.5f}"
                #----------------------------------------
                # [Master] Collect sampling data
                #----------------------------------------
                data.append(text_string)
            #----------------------------------------
            # [Master] Display sampling start data
            #----------------------------------------
            self.ui.listbox_master_ai_sampling_data_var.set(data)
            #----------------------------------------
            # [Master] Display the message that sampling stop is completed
            #----------------------------------------
            self.ui.textbox_master_return_string_var.set("Stop sampling : Successful completion")
        #----------------------------------------
        # [Master] When the sampling count of the data acquired this time is 0 or less
        #----------------------------------------
        else:
            #----------------------------------------
            # [Master] Display the message that sampling stop is completed
            #----------------------------------------
            self.ui.textbox_master_return_string_var.set("Stop sampling : Successful completion")
        #----------------------------------------
        # [Slave] Stop sampling
        #----------------------------------------
        lret.value = caio.AioStopAi(self.slave_id)
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Stop sampling : AioStopAi = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Get status
        #----------------------------------------
        lret.value = caio.AioGetAiStatus(self.slave_id, ctypes.byref(ai_status))
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Stop sampling : AioGetAiStatus = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Create the status string
        #----------------------------------------
        ai_status_string = self.GetStatusString(ai_status)
        #----------------------------------------
        # [Slave] Display the status
        #----------------------------------------
        self.ui.textbox_slave_ai_status_var.set(f"{ai_status.value:08x}H ({ai_status_string})")
        #----------------------------------------
        # [Slave] Get the current sampling count
        #----------------------------------------
        lret.value = caio.AioGetAiSamplingCount(self.slave_id, ctypes.byref(ai_sampling_count))
        #----------------------------------------
        # [Slave] Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Stop sampling : AioGetAiSamplingCount = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            return
        #----------------------------------------
        # [Slave] Display sampling count
        #----------------------------------------
        text_buf = f"{ai_sampling_count.value}"
        self.ui.textbox_slave_ai_sampling_count_var.set(text_buf)
        #----------------------------------------
        # [Slave] When the sampling count of the data acquired this time is 1 or more
        #----------------------------------------
        if ai_sampling_count.value > 0:
            #----------------------------------------
            # [Slave] Get the number of channels
            #----------------------------------------
            lret.value = caio.AioGetAiChannels(self.slave_id, ctypes.byref(ai_channels))
            #----------------------------------------
            # [Slave] Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Stop sampling : AioGetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # [Slave] Allocate an array for storing data
            #----------------------------------------
            if self.slave_sampling_data is not None:
                self.slave_sampling_data = None
            try:
                self.slave_sampling_data = (ctypes.c_float * (ai_sampling_count.value * ai_channels.value))()
            except:
                self.ui.textbox_slave_return_string_var.set("Stop sampling : Failed to allocate an array for storing data")
                return
            #----------------------------------------
            # [Slave] Get sampling data
            #----------------------------------------
            lret.value = caio.AioGetAiSamplingDataEx(self.slave_id, ctypes.byref(ai_sampling_count), self.slave_sampling_data)
            #----------------------------------------
            # [Slave] Error process
            #----------------------------------------
            if lret.value != 0:
                #--------------------
                # Get error string
                #--------------------
                caio.AioGetErrorString(lret, error_string)
                #--------------------
                # Display message
                #--------------------
                text_buf = f"Stop sampling : AioGetAiSamplingDataEx = {lret.value} : {error_string.value.decode('sjis')}"
                self.ui.textbox_slave_return_string_var.set(text_buf)
                return
            #----------------------------------------
            # [Slave] Update the total sampling count
            #----------------------------------------
            self.slave_sampling_count = ai_sampling_count.value
            #----------------------------------------
            # [Slave] Set the loop count for the sampling data display loop
            #----------------------------------------
            #--------------------
            # When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
            #--------------------
            if ai_sampling_count.value > self.DISPLAY_SAMPLING_COUNT:
                #--------------------
                # Limit the maximum number of loops to the data display maximum sampling count
                #--------------------
                loop_max_count = self.DISPLAY_SAMPLING_COUNT
                #--------------------
                # Set the top address of the buffer to be referenced to the address
                # before the maximum sampling count for data display from the end of the acquired data
                #--------------------
                top_address = ai_sampling_count.value - self.DISPLAY_SAMPLING_COUNT
            #--------------------
            # When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
            #--------------------
            else:
                #--------------------
                # Set the number of total sampling data to the maximum loop number
                #--------------------
                loop_max_count = ai_sampling_count.value
                #--------------------
                # Set the top address of the referenced buffer to 0
                #--------------------
                top_address = 0
            #----------------------------------------
            # [Slave] Display sampling data
            # (Display from the end of acquired data to the maximum number of data display sampling count)
            #----------------------------------------
            #--------------------
            # Clear the listbox for sampling data display
            #--------------------
            self.ui.listbox_slave_ai_sampling_data_var.set([""])
            data = []
            #--------------------
            # Repeat the process up to the maximum sampling count of the display data
            #--------------------
            for i in range(top_address, loop_max_count + top_address):
                #--------------------
                # Generate display string
                #--------------------
                text_string = f"{(i + 1):5d}:  "
                #--------------------
                # Repeat the process for the specified number of channels
                #--------------------
                for j in range(ai_channels.value):
                    text_string += f" {self.slave_sampling_data[i * ai_channels.value + j]:10.5f}"
                #----------------------------------------
                # [Slave] Collect sampling data
                #----------------------------------------
                data.append(text_string)
            #----------------------------------------
            # [Slave] Display sampling start data
            #----------------------------------------
            self.ui.listbox_slave_ai_sampling_data_var.set(data)
            #----------------------------------------
            # [Slave] Display the message that sampling stop is completed
            #----------------------------------------
            self.ui.textbox_slave_return_string_var.set("Stop sampling : Successful completion")
        #----------------------------------------
        # [Slave] When the sampling count of the data acquired this time is 0 or less
        #----------------------------------------
        else:
            #----------------------------------------
            # [Slave] Display the message that sampling stop is completed
            #----------------------------------------
            self.ui.textbox_slave_return_string_var.set("Stop sampling : Successful completion")

    #========================================
    # Function name    OnClickedButtonMasterOutputSamplingData
    # Parameter        None
    # Details          Master Device Output Acquired Data to File... button click event function
    # Return value     None
    #========================================
    def OnClickedButtonMasterOutputSamplingData(self):
        ai_channels = ctypes.c_short()                     # Number of channels
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code
        file_name = "MasterSamplingData.csv"               # Sampling data output file name

        #----------------------------------------
        # Open file
        #----------------------------------------
        try:
            file = open(file_name, 'w')
        except:
            self.ui.textbox_master_return_string_var.set("Output Acquired Data to File : open = file open error.")
            return
        #----------------------------------------
        # Get the number of channels
        #----------------------------------------
        lret.value = caio.AioGetAiChannels(self.master_id, ctypes.byref(ai_channels))
        #----------------------------------------
        # Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Output Acquired Data to File : AioGetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_master_return_string_var.set(text_buf)
            #--------------------
            # Close file
            #--------------------
            file.close()
            return
        #----------------------------------------
        # Create sampling data for file output
        #----------------------------------------
        #--------------------
        # If the total sampling count is 1 or more, output the file
        #--------------------
        if self.master_sampling_count > 0:
            #--------------------
            # Repeat the process up to the total sampling count
            #--------------------
            for i in range(self.master_sampling_count):
                #--------------------
                # Generate display string
                #--------------------
                text_string = f"{(i + 1):d},"
                #--------------------
                # Repeat the process for the specified number of channels
                #--------------------
                for j in range(ai_channels.value):
                    text_string += f"{self.master_sampling_data[i * ai_channels.value + j]:0.5f}"
                    #--------------------
                    # Add newline character for the last channel
                    #--------------------
                    if j == ai_channels.value - 1:
                        text_string += "\n"
                    #--------------------
                    # Add comma for channels other than the last channel
                    #--------------------
                    else:
                        text_string += ","
                #----------------------------------------
                # Write data
                #----------------------------------------
                file.write(text_string)
        #----------------------------------------
        # Close file
        #----------------------------------------
        file.close()
        #----------------------------------------
        # Display the message that file output is completed
        #----------------------------------------
        text_buf = f"Output Acquired Data to File : Save data to \"{file_name}\"."
        self.ui.textbox_master_return_string_var.set(text_buf)

    #========================================
    # Function name    OnClickedButtonSlaveOutputSamplingData
    # Parameter        None
    # Details          Slave Device Output Acquired Data to File... button click event function
    # Return value     None
    #========================================
    def OnClickedButtonSlaveOutputSamplingData(self):
        ai_channels = ctypes.c_short()                     # Number of channels
        error_string = ctypes.create_string_buffer(256)    # Error code string
        lret = ctypes.c_long()                             # For return code
        file_name = "SlaveSamplingData.csv"                # Sampling data output file name

        #----------------------------------------
        # Open file
        #----------------------------------------
        try:
            file = open(file_name, 'w')
        except:
            self.ui.textbox_slave_return_string_var.set("Output Acquired Data to File : open = file open error.")
            return
        #----------------------------------------
        # Get the number of channels
        #----------------------------------------
        lret.value = caio.AioGetAiChannels(self.slave_id, ctypes.byref(ai_channels))
        #----------------------------------------
        # Error process
        #----------------------------------------
        if lret.value != 0:
            #--------------------
            # Get error string
            #--------------------
            caio.AioGetErrorString(lret, error_string)
            #--------------------
            # Display message
            #--------------------
            text_buf = f"Output Acquired Data to File : AioGetAiChannels = {lret.value} : {error_string.value.decode('sjis')}"
            self.ui.textbox_slave_return_string_var.set(text_buf)
            #--------------------
            # Close file
            #--------------------
            file.close()
            return
        #----------------------------------------
        # Create sampling data for file output
        #----------------------------------------
        #--------------------
        # If the total sampling count is 1 or more, output the file
        #--------------------
        if self.slave_sampling_count > 0:
            #--------------------
            # Repeat the process up to the total sampling count
            #--------------------
            for i in range(self.slave_sampling_count):
                #--------------------
                # Generate display string
                #--------------------
                text_string = f"{(i + 1):d},"
                #--------------------
                # Repeat the process for the specified number of channels
                #--------------------
                for j in range(ai_channels.value):
                    text_string += f"{self.slave_sampling_data[i * ai_channels.value + j]:0.5f}"
                    #--------------------
                    # Add newline character for the last channel
                    #--------------------
                    if j == ai_channels.value - 1:
                        text_string += "\n"
                    #--------------------
                    # Add comma for channels other than the last channel
                    #--------------------
                    else:
                        text_string += ","
                #----------------------------------------
                # Write data
                #----------------------------------------
                file.write(text_string)
        #----------------------------------------
        # Close file
        #----------------------------------------
        file.close()
        #----------------------------------------
        # Display the message that file output is completed
        #----------------------------------------
        text_buf = f"Output Acquired Data to File : Save data to \"{file_name}\"."
        self.ui.textbox_slave_return_string_var.set(text_buf)

    #========================================
    # 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

        #----------------------------------------
        # [Master] Release the array for storing AD conversion data
        #----------------------------------------
        if self.master_sampling_data is not None:
            self.master_sampling_data = None
        #----------------------------------------
        # [Master] Exit process
        #----------------------------------------
        lret.value = caio.AioExit(self.master_id)
        #----------------------------------------
        # [Master] Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # [Master] Display message
        #----------------------------------------
        text_buf = f"AioExit = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_master_return_string_var.set(text_buf)
        #----------------------------------------
        # [Slave] Release the array for storing AD conversion data
        #----------------------------------------
        if self.slave_sampling_data is not None:
            self.slave_sampling_data = None
        #----------------------------------------
        # [Slave] Exit process
        #----------------------------------------
        lret.value = caio.AioExit(self.slave_id)
        #----------------------------------------
        # [Slave] Get error string
        #----------------------------------------
        caio.AioGetErrorString(lret, error_string)
        #----------------------------------------
        # [Slave] Display message
        #----------------------------------------
        text_buf = f"AioExit = {lret.value} : {error_string.value.decode('sjis')}"
        self.ui.textbox_slave_return_string_var.set(text_buf)

    #========================================
    # Function name    OnClickedButtonClose
    # Parameter        None
    # Details          Close button click event function
    # Return value     None
    #========================================
    def OnClickedButtonClose(self):
        #----------------------------------------
        # Release the array for storing AD conversion data
        #----------------------------------------
        if self.master_sampling_data is not None:
            self.master_sampling_data = None
        if self.slave_sampling_data is not None:
            self.slave_sampling_data = None
        #----------------------------------------
        # Exit process
        #----------------------------------------
        caio.AioExit(self.master_id)
        caio.AioExit(self.slave_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 SyncBusSampling
    #----------------------------------------
    instance = SyncBusSampling()
    instance.run()

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