#================================================================
#================================================================
# API-CNT(WDM) for Device with sampling function
# Counter Sample
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
import ctypes
import ctypes.wintypes
import sys
import ccnt


#================================================================
# LOWORD
#================================================================
def LOWORD(a):
    return a & 0xffff


#================================================================
# HOWORD
#================================================================
def HIWORD(a):
    return (a >> 16) & 0xffff


#================================================================
# Macro definition
#================================================================
MENU_NUM_MAIN = 0               # Main menu
MENU_NUM_CHMAX = 1              # The number acquisition of use channels
MENU_NUM_ZMODE = 2              # Set the Z phase mode
MENU_NUM_ZLOGIC = 3             # Set the Z phase logic
MENU_NUM_CHANNELSIGNAL = 4      # Set the external signal source
MENU_NUM_COUNTDIRECTION = 5     # Set the direction of count
MENU_NUM_DIGITALFILTER = 6      # Set the digital filter
MENU_NUM_PHASE = 7              # Set the phases
MENU_NUM_MUL = 8                # Set the multiple
MENU_NUM_CLR = 9                # Set the clear method
MENU_NUM_NOTIFICATION = 10      # Set the notification
MENU_NUM_COUNT_MATCH_CH = 11    # Set the count match channel
MENU_NUM_SET_REG0 = 12          # Set the comparison value to comparison register 0
MENU_NUM_SET_REG1 = 13          # Set the comparison value to comparison register 1
MENU_NUM_COUNTERROR = 14        # Set the count error notification
MENU_NUM_CARRYBORROW = 15       # Set the carry borrow notification
MENU_NUM_HARDWARE_SETTINGS = 16 # Set the hardware event settings
MENU_NUM_HARDWARE_INPUT = 17    # Set the input hardware event
MENU_NUM_INPUT_TYPE = 18        # Set the input hardware event type
MENU_NUM_RF0 = 19               # Set the input direction for RF0
MENU_NUM_RF0_VALUE = 20         # Set the preset register value for RF0
MENU_NUM_RF1 = 21               # Set the input direction for RF1
MENU_NUM_HARDWARE_OUTPUT = 22   # Set the output hardware event
MENU_NUM_OUTPUT_TYPE = 23       # Set the output hardware event type
MENU_NUM_PULSE_LENGTH = 24      # Set the one-shot pulse width
MENU_NUM_OUTPUT_CHANNEL = 25    # Set the general output channel
MENU_NUM_OUTPUT_DATA = 26       # Set the general output data (off or on)
MENU_NUM_HARDWARE_COUNT = 27    # Set the count match hardware event
MENU_NUM_PRESET = 28            # Presets the count value of specified channel
MENU_CNT_OPEN = 0               # CNT initialization
MENU_MODE_SET = 1               # Set the counter mode
MENU_NOTIFICATION = 2           # Set the notification
MENU_HARDWARE_EVENT = 3         # Set the hardware event settings
MENU_START_COUNT = 4            # Count start
MENU_READ_COUNT = 5             # Get counter value
MENU_READ_STATUS = 6            # Get status
MENU_PRESET = 7                 # Preset
MENU_RESET = 8                  # Reset counter
MENU_STOP_COUNT = 9             # Count stop
MENU_CNT_CLOSE = 10             # CNT exit
MENU_END = 11                   # Exit sample
MENU_COUNTMATCH = 1             # Set the count match notification
MENU_COUNTERROR = 2             # Set the count error notification
MENU_CARRYBORROW = 3            # Set the carry borrow notification
MENU_NOTIFICATION_BACK = 4      # Back to main screen
MENU_HARDWARE_INPUT = 1         # Set the hardware input
MENU_HARDWARE_OUTPUT = 2        # Set the hardware output
MENU_GENERAL_OUTPUT = 3         # Set the general output
MENU_COUNT_MATCH_EVENT = 4      # Set the count match event
MENU_HARDWARE_EVENT_BACK = 5    # Back to main screen
MENU_INPUT_EVENT_TYPE = 1       # Set the input hardware event type
MENU_RF0_DIRECTION = 2          # Set the input direction for RF0
MENU_RF0_VALUDE = 3             # Set the preset value for RF0
MENU_RF1_DIRECTION = 4          # Set the input direction for RF1
MENU_INPUT_SIGANL_BACK = 5      # Back to Previous interface
MENU_OUTPUT_EVENT_TYPE = 1      # Set the output hardware event type
MENU_PULSE_WIDTH = 2            # Set the one-shot pulse width
MENU_OUTPUT_SIGANL_BACK = 3     # Back to previous interface
    
CHANNEL_NUM = 4
#================================================================
# External variables
#================================================================
MaxChannels = 0                                     # Total of channels
count_match_type = ctypes.c_ulong * CHANNEL_NUM     # Create array type (Count match times)
count_match = count_match_type()                    # Count match times


#================================================================
# Function that checks if a string can be converted to a number
#================================================================
def isnum(str, base):
    try:
        if 16 == base:
            int(str, 16)
        else:
            int(str)
    except:
        return False
    return True


#================================================================
# Count callback function
#================================================================
def count_call_back(dev_id, wparam, lparam, param):
    global count_match
    #----------------------------------------
    # Count match message process
    #----------------------------------------
    #----------------------------------------
    # Channel number of count match
    #----------------------------------------
    ChNo = LOWORD(wparam)
    #----------------------------------------
    # Rising :1, Falling :2
    #----------------------------------------
    Kind = HIWORD(wparam)
    #----------------------------------------
    # Comparison Value
    #----------------------------------------
    Count = lparam

    if ChNo == 0:       # Count match: 0CH
        count_match[0] = count_match[0] + 1
        if Kind == 1:
            print('Count Match! Channel No.: 0, UP, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[0]))
        else:
            print('Count Match! Channel No.: 0, DOWN, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[0]))
    elif ChNo == 1:     # Count match: 1CH
        count_match[1] = count_match[1] + 1
        if Kind == 1:
            print('Count Match! Channel No.: 1, UP, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[1]))
        else:
            print('Count Match! Channel No.: 1, DOWN, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[1]))
    elif ChNo == 2:     # Count match: 2CH
        count_match[2] = count_match[2] + 1
        if Kind == 1:
            print('Count Match! Channel No.: 2, UP, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[2]))
        else:
            print('Count Match! Channel No.: 2, DOWN, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[2]))
    elif ChNo == 3:     # Count match: 3CH
        count_match[3] = count_match[3] + 1
        if Kind == 1:
            print('Count Match! Channel No.: 3, UP, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[3]))
        else:
            print('Count Match! Channel No.: 3, DOWN, Count: {0:08x}(Hex) Matched: {1:d} times'.format(Count, count_match[3]))
    return


#================================================================
# Counter error callback function
#================================================================
def count_error_call_back(dev_id, wparam, lparam, param):
    #----------------------------------------
    # Counter error message process
    #----------------------------------------
    #----------------------------------------
    # Channel number of counter error
    #----------------------------------------
    ChNo = LOWORD(wparam)
    #----------------------------------------
    # Error Kind of counter error
    #----------------------------------------
    Kind = LOWORD(lparam)

    if Kind == ccnt.CNT_AI_ERROR:        # Abnormal input error
        print('Abnormal input error! Channel No.: {0:d}'.format(ChNo))
    elif Kind == ccnt.CNT_FLT_ERROR:     # Filter error
        print('Filter error! Channel No.: {0:d}'.format(ChNo))
    elif Kind == ccnt.CNT_ALM_ERROR:     # Alarm error
        print('Alarm error! Channel No.: {0:d}'.format(ChNo))
    return


#================================================================
# Carry/borrow callback function
#================================================================
def carry_borrow_call_back(dev_id, wparam, lparam, param):
    #----------------------------------------
    # Carry/borrow message process
    #----------------------------------------
    #----------------------------------------
    # Channel number of carry/borrow
    #----------------------------------------
    ChNo = LOWORD(wparam)
    #----------------------------------------
    # Notic factor of carry/borrow
    #----------------------------------------
    Kind = LOWORD(lparam)

    if Kind == ccnt.CNT_CB_CARRY:        # Carry
        print('Carry! Channel No.: {0:d}'.format(ChNo))
    elif Kind == ccnt.CNT_CB_BORROW:     # Borrow
        print('Borrow! Channel No.: {0:d}'.format(ChNo))
    return


pcount_callback = ccnt.PCNT_MATCH_CALLBACK(count_call_back)
pcount_error_callback = ccnt.PCNT_COUNTER_ERR_CALLBACK(count_error_call_back)
pcarray_borrow_callback = ccnt.PCNT_CARRY_BORROW_CALLBACK(carry_borrow_call_back)


#================================================================
# Process the input data from keyboard
#================================================================
def input_data(MenuNum):
    if MenuNum == MENU_NUM_CHMAX:
        print(f"(1-{MaxChannels}):", end='')
    input_str = input()
    return input_str


#================================================================
# Function that menu is displayed
#================================================================
def menu(MenuNum):
    #----------------------------------------
    # Initialize the menu
    #----------------------------------------
    MenuDat = [
        #----------------------------------------
        # Main Menu Data
        # MENU_NUM_MAIN
        #----------------------------------------
        {
            #----------------------------------------
            # Use line..1   Not use..0
            #----------------------------------------
            'Line':1,
            #----------------------------------------
            # Title
            #----------------------------------------
            'StrMenu':'Menu',
            #----------------------------------------
            # Number of items
            #----------------------------------------
            'ItemCount':12,
            #----------------------------------------
            # Item    #Menu item       #Macro that correspond with the menu
            #----------------------------------------
            'Item':[{'Str':'0.CntInit', 'Num':MENU_CNT_OPEN},
                    {'Str':'1.Counter Mode', 'Num':MENU_MODE_SET},
                    {'Str':'2.Notification Setting', 'Num':MENU_NOTIFICATION},
                    {'Str':'3.Hardware Event', 'Num':MENU_HARDWARE_EVENT},
                    {'Str':'4.CntStartCount', 'Num':MENU_START_COUNT},
                    {'Str':'5.CntReadCount', 'Num':MENU_READ_COUNT},
                    {'Str':'6.CntReadStatus', 'Num':MENU_READ_STATUS},
                    {'Str':'7.CntPreset', 'Num':MENU_PRESET},
                    {'Str':'8.CntZeroClearCount', 'Num':MENU_RESET},
                    {'Str':'9.CntStopCount', 'Num':MENU_STOP_COUNT},
                    {'Str':'10.CntExit', 'Num':MENU_CNT_CLOSE},
                    {'Str':'11.Exit', 'Num':MENU_END}],
            #----------------------------------------
            # Question
            #----------------------------------------
            'Question':'Please select the executing command.',
            #----------------------------------------
            # Prompt
            #----------------------------------------
            'Prompt':'(0-11):',
        },
        #----------------------------------------
        # Mode Setting--1 Menu Data
        # MENU_NUM_CHMAX
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'',
            'ItemCount':0,
            'Item':[],
            'Question':'Please specify the using number of channels\n(Set the same setting to all channels.)',
            'Prompt':'',
        },
        #----------------------------------------
        # Mode Setting--2 Menu Data
        # MENU_NUM_ZMODE
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the Z phase mode',
            'ItemCount':3,
            'Item':[{'Str':'1.Not Use', 'Num':ccnt.CNT_ZPHASE_NOT_USE},
                    {'Str':'2.One Time', 'Num':ccnt.CNT_ZPHASE_NEXT_ONE},
                    {'Str':'3.Every Time', 'Num':ccnt.CNT_ZPHASE_EVERY_TIME}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # Mode Setting--3 Menu Data
        # MENU_NUM_ZLOGIC
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the Z phase logic',
            'ItemCount':2,
            'Item':[{'Str':'1.Positive Logic', 'Num':ccnt.CNT_ZLOGIC_POSITIVE},
                    {'Str':'2.Negative Logic', 'Num':ccnt.CNT_ZLOGIC_NEGATIVE}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-2):',
        },
        #----------------------------------------
        # Mode Setting--4 Menu Data
        # MENU_NUM_CHANNELSIGNAL
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the external signal source',
            'ItemCount':3,
            'Item':[{'Str':'1.Photocoupler insulation', 'Num':ccnt.CNT_SIGTYPE_ISOLATE},
                    {'Str':'2.TTL', 'Num':ccnt.CNT_SIGTYPE_TTL},
                    {'Str':'3.Line Receiver', 'Num':ccnt.CNT_SIGTYPE_LINERECEIVER}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # Mode Setting--5 Menu Data
        # MENU_NUM_COUNTDIRECTION
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the count direction',
            'ItemCount':2,
            'Item':[{'Str':'1.DOWN Count', 'Num':ccnt.CNT_DIR_DOWN},
                    {'Str':'2.UP Count', 'Num':ccnt.CNT_DIR_UP}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-2):',
        },
        #----------------------------------------
        # Mode Setting--6 Menu Data
        # MENU_NUM_DIGITALFILTER
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'',
            'ItemCount':0,
            'Item':[],
            'Question':'Please input the digital filter coefficient',
            'Prompt':'(0x00-0x14):',
        },
        #----------------------------------------
        # Mode Setting--7	Menu Data
        # MENU_NUM_PHASE
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the operation mode',
            'ItemCount':3,
            'Item':[{'Str':'1.Single phase input', 'Num':ccnt.CNT_MODE_1PHASE},
                    {'Str':'2.2-phase input', 'Num':ccnt.CNT_MODE_2PHASE},
                    {'Str':'3.Single phase input with gate control', 'Num':ccnt.CNT_MODE_GATECONTROL}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # Mode Setting--8 Menu Data
        # MENU_NUM_MUL
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the operation mode',
            'ItemCount':3,
            'Item':[{'Str':'1.1X', 'Num':ccnt.CNT_MUL_X1},
                    {'Str':'2.2X', 'Num':ccnt.CNT_MUL_X2},
                    {'Str':'3.4X', 'Num':ccnt.CNT_MUL_X4}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # Mode Setting--9 Menu Data
        # MENU_NUM_CLR
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the operation mode',
            'ItemCount':2,
            'Item':[{'Str':'1.Async Clear', 'Num':ccnt.CNT_CLR_ASYNC},
                    {'Str':'2.Sync Clear', 'Num':ccnt.CNT_CLR_SYNC}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-2):',
        },
        #----------------------------------------
        # Notification Setting--1 Menu Data
        # MENU_NUM_NOTIFICATION
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the Notification Setting',
            'ItemCount':4,
            'Item':[{'Str':'1.Count Match', 'Num':MENU_COUNTMATCH},
                    {'Str':'2.Error', 'Num':MENU_COUNTERROR},
                    {'Str':'3.Carry/Borrow', 'Num':MENU_CARRYBORROW},
                    {'Str':'4.Back', 'Num':MENU_NOTIFICATION_BACK}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-4):',
        },
        #----------------------------------------
        # Notification Setting--2 Menu Data
        # MENU_NUM_COUNT_MATCH_CH
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'',
            'ItemCount':0,
            'Item':[],
            'Question':'Please input the channel number.',
            'Prompt':'Channel:',
        },
        #----------------------------------------
        # Notification Setting--3 Menu Data
        # MENU_NUM_SET_REG0
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'',
            'ItemCount':0,
            'Item':[],
            'Question':'Please input the REG0 data.',
            'Prompt':'Data(Decimal):',
        },
        #----------------------------------------
        # Notification Setting--4 Menu Data
        # MENU_NUM_SET_REG1
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'',
            'ItemCount':0,
            'Item':[],
            'Question':'Please input the REG1 data.',
            'Prompt':'Data(Decimal):',
        },
        #----------------------------------------
        # Notification Setting--5 Menu Data
        # MENU_NUM_COUNTERROR
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the Count Error Notification',
            'ItemCount':2,
            'Item':[{'Str':'1.Notify', 'Num':1},
                    {'Str':'2.Not Notify', 'Num':0}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-2):',
        },
        #----------------------------------------
        # Notification Setting--6 Menu Data
        # MENU_NUM_CARRYBORROW
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set the Carry/Borrow Notification',
            'ItemCount':2,
            'Item':[{'Str':'1.Notify', 'Num':1},
                    {'Str':'2.Not Notify', 'Num':0}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-2):',
        },
        #----------------------------------------
        # Hardware Event Setting--1 Menu Data
        # MENU_NUM_HARDWARE_SETTINGS
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'HardwareEvent Settings',
            'ItemCount':5,
            'Item':[{'Str':'1.Event Signal Input', 'Num':MENU_HARDWARE_INPUT},
                    {'Str':'2.Event Signal Output', 'Num':MENU_HARDWARE_OUTPUT},
                    {'Str':'3.General Output', 'Num':MENU_GENERAL_OUTPUT},
                    {'Str':'4.Count Match Event', 'Num':MENU_COUNT_MATCH_EVENT},
                    {'Str':'5.Back', 'Num':MENU_HARDWARE_EVENT_BACK}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-5):',
        },
        #----------------------------------------
        # Hardware Event Setting--2 Menu Data
        # MENU_NUM_HARDWARE_INPUT
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Event Signal Input',
            'ItemCount':5,
            'Item':[{'Str':'1.Select Event Type', 'Num':MENU_INPUT_EVENT_TYPE},
                    {'Str':'2.Set Control Input Signal RF0 Direction (Rise, Fall)', 'Num':MENU_RF0_DIRECTION},
                    {'Str':'3.RF0 Preset Value', 'Num':MENU_RF0_VALUDE},
                    {'Str':'4.Set Control Input Signal RF1 Direction (Rise, Fall)', 'Num':MENU_RF1_DIRECTION},
                    {'Str':'5.Back', 'Num':MENU_INPUT_SIGANL_BACK}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-5):',
        },
        #----------------------------------------
        # Hardware Event Setting--3 Menu Data
        # MENU_NUM_INPUT_TYPE
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Select Event Type',
            'ItemCount':4,
            'Item':[{'Str':'1.General Input', 'Num':ccnt.CNT_IEVENT_GENINP},
                    {'Str':'2.Counter Start/Stop', 'Num':ccnt.CNT_IEVENT_STARTSTOP},
                    {'Str':'3.Counter Preset', 'Num':ccnt.CNT_IEVENT_PRESET},
                    {'Str':'4.Counter Clear', 'Num':ccnt.CNT_IEVENT_CLEAR}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-4):',
        },
        #----------------------------------------
        # Hardware Event Setting--4 Menu Data
        # MENU_NUM_RF0
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set Control Input Signal RF0 Direction (Rise, Fall)',
            'ItemCount':3,
            'Item':[{'Str':'1.Rise', 'Num':ccnt.CNT_INP_RIZE},
                    {'Str':'2.Fall', 'Num':ccnt.CNT_INP_FALL},
                    {'Str':'3.None', 'Num':ccnt.CNT_INP_NONE}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # Hardware Event Setting--5 Menu Data
        # MENU_NUM_RF0_VALUE
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'Set RF0 Preset Value',
            'ItemCount':0,
            'Item':[],
            'Question':'Please input the RF0 data.',
            'Prompt':'Data (Hex e.g. 0x1000):',
        },
        #----------------------------------------
        # Hardware Event Setting--6 Menu Data
        # MENU_NUM_RF1
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Set Control Input Signal RF1 Direction (Rise, Fall)',
            'ItemCount':3,
            'Item':[{'Str':'1.Rise', 'Num':ccnt.CNT_INP_RIZE},
                    {'Str':'2.Fall', 'Num':ccnt.CNT_INP_FALL},
                    {'Str':'3.None', 'Num':ccnt.CNT_INP_NONE}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # Hardware Event Setting--7 Menu Data
        # MENU_NUM_HARDWARE_OUTPUT
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Event Signal Output',
            'ItemCount':3,
            'Item':[{'Str':'1.Select Event Type', 'Num':MENU_OUTPUT_EVENT_TYPE},
                    {'Str':'2.Pulse Width', 'Num':MENU_PULSE_WIDTH},
                    {'Str':'3.Back', 'Num':MENU_OUTPUT_SIGANL_BACK}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # Hardware Event Setting--8 Menu Data
        # MENU_NUM_OUTPUT_TYPE
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Select Event Type',
            'ItemCount':6,
            'Item':[{'Str':'1.General-purpose output', 'Num':ccnt.CNT_OEVENT_GENOUT},
                    {'Str':'2.Count Match REG0', 'Num':ccnt.CNT_OEVENT_MATCH0},
                    {'Str':'3.Count Match REG1', 'Num':ccnt.CNT_OEVENT_MATCH1},
                    {'Str':'4.Abnormal Input Error', 'Num':ccnt.CNT_OEVENT_AIERR},
                    {'Str':'5.Digital Filter Error', 'Num':ccnt.CNT_OEVENT_FLTERR},
                    {'Str':'6.Alarm Error', 'Num':ccnt.CNT_OEVENT_ALMERR}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-6):',
        },
        #----------------------------------------
        # Hardware Event Setting--9 Menu Data
        # MENU_NUM_PULSE_LENGTH
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Pulse Width (Coefficient)',
            'ItemCount':5,
            'Item':[{'Str':'0', 'Num':0x00},
                    {'Str':'1', 'Num':0x01},
                    {'Str':'2', 'Num':0x02},
                    {'Str':'3', 'Num':0x03},
                    {'Str':'4', 'Num':0x04},
                    {'Str':'5', 'Num':0x05},
                    {'Str':'6', 'Num':0x06},
                    {'Str':'7', 'Num':0x07},
                    {'Str':'8', 'Num':0x08},
                    {'Str':'9', 'Num':0x09},
                    {'Str':'A', 'Num':0x0A},
                    {'Str':'B', 'Num':0x0B},
                    {'Str':'C', 'Num':0x0C},
                    {'Str':'D', 'Num':0x0D},
                    {'Str':'E', 'Num':0x0E},
                    {'Str':'F', 'Num':0x0F}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-5):',
        },
        #----------------------------------------
        # Hardware Event Setting--10 Menu Data
        # MENU_NUM_OUTPUT_CHANNEL
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'General Output',
            'ItemCount':0,
            'Item':[],
            'Question':'Please input the output channel.',
            'Prompt':'Channel :',
        },
        #----------------------------------------
        # Hardware Event Setting--11 Menu Data
        # MENU_NUM_OUTPUT_DATA
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'General Output',
            'ItemCount':2,
            'Item':[{'Str':'1.ON', 'Num':0x01},
                    {'Str':'2.OFF', 'Num':0x00}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-2):',
        },
        #----------------------------------------
        # Hardware Event Setting--12 Menu Data
        # MENU_NUM_HARDWARE_COUNT
        #----------------------------------------
        {
            'Line':1,
            'StrMenu':'Count Match Event',
            'ItemCount':3,
            'Item':[{'Str':'1.None', 'Num':ccnt.CNT_MATCH_NONE},
                    {'Str':'2.Preset', 'Num':ccnt.CNT_MATCH_PRESET},
                    {'Str':'3.Clear', 'Num':ccnt.CNT_MATCH_CLEAR}],
            'Question':'Please input the setting number.',
            'Prompt':'(1-3):',
        },
        #----------------------------------------
        # (7) Menu Data
        # MENU_NUM_PRESET
        #----------------------------------------
        {
            'Line':0,
            'StrMenu':'',
            'ItemCount':0,
            'Item':[],
            'Question':'Set the same value to all channels.\nPlease input the preset data.',
            'Prompt':'Data (Decimal):',
        }]

    #----------------------------------------
    # Display the Menu and acquisition of input data
    #----------------------------------------
    while True:
        if MenuDat[MenuNum]['Line'] != 0:
            print("\n===================================")
            print(f"{MenuDat[MenuNum]['StrMenu']}")
            print("===================================")
            print("-----------------------------------")
            for i in range(0, (MenuDat[MenuNum]['ItemCount'])):
                print(f"{MenuDat[MenuNum]['Item'][i]['Str']}")  # Display Item
            print("-----------------------------------\n")
            print(f"{MenuDat[MenuNum]['Question']}")    
            print(f"{MenuDat[MenuNum]['Prompt']}", end='')
        else:
            print(f"{MenuDat[MenuNum]['Question']}")
            print(f"{MenuDat[MenuNum]['Prompt']}", end='')
        #----------------------------------------
        # Get the input data
        #----------------------------------------
        input_str = input_data(MenuNum)
        #----------------------------------------
        # Check the input data
        #----------------------------------------
        #----------------------------------------
        # The end macro is returned if the item of end is selected
        #----------------------------------------
        if MenuNum == 0:
            if input_str == 'q' or input_str == 'Q':
                return MENU_END
        #----------------------------------------
        # The input data is returned if the menu item is 0
        #----------------------------------------
        if MenuDat[MenuNum]['ItemCount'] == 0:
            if MenuNum == MENU_NUM_DIGITALFILTER or MenuNum == MENU_NUM_RF0_VALUE:
                if False == isnum(input_str, 16):
                    continue
                return int(input_str, 16)
            else:
                if False == isnum(input_str, 10):
                    continue
                return int(input_str)
        #----------------------------------------
        # Check whether the input data is outside of the number of items
        #----------------------------------------
        if False == isnum(input_str, 10):
            continue
        if MenuNum == 0:
            if 0 <= int(input_str) and int(input_str) < MenuDat[MenuNum]['ItemCount']:
                return MenuDat[MenuNum]['Item'][int(input_str)]['Num']
        else:
            if 0 < int(input_str) and int(input_str) <= MenuDat[MenuNum]['ItemCount']:
                return MenuDat[MenuNum]['Item'][int(input_str) - 1]['Num']
        print("\nThe used range is outside.\nPlease input once again.")


#================================================================
# Main function
#================================================================
def main():
    input_data = ctypes.c_short()                   # Return value of menu function
    buf = ctypes.create_string_buffer(256)          # Error string
    lret = ctypes.c_long()                          # Return value
    cnt_id = ctypes.c_short()
    sts = ctypes.c_short()                          # Buffer for status
    input_preset = ctypes.c_ulong()                 # Specify preset data
    preset_data_type = ctypes.c_ulong * 10          # Create array type
    preset_data = preset_data_type()
    device_name = ctypes.create_string_buffer(50)   # Device name
    ch_no = ctypes.c_short()                        # Channel No.
    ch_no_array_type = ctypes.c_short * 10          # Create array type
    ch_no_array = ch_no_array_type()                # Array for storing number of channels  
    ch_max = ctypes.c_short()                       # Using number of channels
    cnt_data_type = ctypes.c_ulong * 10             # Create array type
    cnt_data = cnt_data_type()                      # Array for storing counter data
    phase = ctypes.c_short()                        # Specify the number of phases
    mul = ctypes.c_short()                          # Specify multiply
    sync_clr = ctypes.c_short()                     # Specify synchronization clear, asynchronization clear
    input_event = ctypes.c_ulong()                  # Hardware input event type
    RF0 = ctypes.c_short(ccnt.CNT_INP_NONE)         # RF0 direction
    RF0_value = ctypes.c_ulong(0x1000)              # RF0 value
    RF1 = ctypes.c_short(ccnt.CNT_INP_NONE)         # RF1 direction
    output_event = ctypes.c_ulong()                 # Hardware output event type
    pulse_length = ctypes.c_short(1)                # Pulse width

    print("<<< Counter Sample Program >>>")
    #----------------------------------------
    # Execute command and display the result
    #----------------------------------------
    while True:
        input_data.value = menu( MENU_NUM_MAIN)
        if input_data.value == MENU_END:
            ccnt.CntExit(cnt_id)
            break
        #----------------------------------------
        # Initialization
        #----------------------------------------
        if input_data.value == MENU_CNT_OPEN:
            device_name = input("Please input the device name of using board.\nDevice Name:")
            lret.value = ccnt.CntInit(device_name.encode(), ctypes.byref(cnt_id))
            ccnt.CntGetErrorString(lret, buf)
            print(f"CntInit RetCode = {lret.value}: {buf.value.decode('sjis')}")
            if lret.value != ccnt.CNT_ERR_SUCCESS:
                break
            lret.value = ccnt.CntGetMaxChannels(cnt_id, ctypes.byref(ch_max))
            #----------------------------------------
            # Update the External variables
            #----------------------------------------
            global MaxChannels
            MaxChannels = ch_max.value
        #----------------------------------------
        # Set mode
        #----------------------------------------
        elif input_data.value == MENU_MODE_SET:    
            #----------------------------------------
            # Using the number of channels
            #----------------------------------------
            input_data.value = menu( MENU_NUM_CHMAX)
            ch_max.value = input_data.value
            for ch_no.value in range(0, ch_max.value):
                ch_no_array[ch_no.value] = ch_no.value
            #----------------------------------------
            # Set the Z phase mode
            #----------------------------------------
            input_data.value = menu( MENU_NUM_ZMODE)
            for ch_no.value in range(0, ch_max.value):
                lret.value = ccnt.CntSetZMode(cnt_id, ch_no, input_data)
                ccnt.CntGetErrorString(lret, buf)
                print(f"Ch{ch_no.value} CntSetZMode RetCode = {lret.value}: {buf.value.decode('sjis')}")
                if lret.value != ccnt.CNT_ERR_SUCCESS:
                    break
            #----------------------------------------
            # Set the Z phase logic
            #----------------------------------------
            input_data.value = menu( MENU_NUM_ZLOGIC)
            for ch_no.value in range(0, ch_max.value):
                lret.value = ccnt.CntSetZLogic(cnt_id, ch_no, input_data)
                ccnt.CntGetErrorString(lret, buf)
                print(f"Ch{ch_no.value} CntSetZLogic RetCode = {lret.value}: {buf.value.decode('sjis')}")
                if lret.value != ccnt.CNT_ERR_SUCCESS:
                    break
            #----------------------------------------
            # Set the external signal source
            #----------------------------------------
            input_data.value = menu( MENU_NUM_CHANNELSIGNAL)
            for ch_no.value in range(0, ch_max.value):
                lret.value = ccnt.CntSelectChannelSignal(cnt_id, ch_no, input_data)
                ccnt.CntGetErrorString(lret, buf)
                print(f"Ch{ch_no.value} CntSelectChannelSignal RetCode = {lret.value}: {buf.value.decode('sjis')}")
                if lret.value != ccnt.CNT_ERR_SUCCESS:
                    break
            #----------------------------------------
            # Set the direction of count
            #----------------------------------------
            input_data.value = menu( MENU_NUM_COUNTDIRECTION)
            for ch_no.value in range(0, ch_max.value):
                lret.value = ccnt.CntSetCountDirection(cnt_id, ch_no, input_data)
                ccnt.CntGetErrorString(lret, buf)
                print(f"Ch{ch_no.value} CntSetCountDirection RetCode = {lret.value}: {buf.value.decode('sjis')}")
                if lret.value != ccnt.CNT_ERR_SUCCESS:
                    break
            #----------------------------------------
            # Digital filter coefficient
            #----------------------------------------
            input_data.value = menu( MENU_NUM_DIGITALFILTER)
            for ch_no.value in range(0, ch_max.value):
                lret.value = ccnt.CntSetDigitalFilter(cnt_id, ch_no, input_data)
                ccnt.CntGetErrorString(lret, buf)
                print(f"Ch{ch_no.value} CntSetDigitalFilter RetCode = {lret.value}: {buf.value.decode('sjis')}")
                if lret.value != ccnt.CNT_ERR_SUCCESS:
                    break
            #----------------------------------------
            # Set operation mode
            #----------------------------------------
            phase.value = menu( MENU_NUM_PHASE)
            mul.value = menu( MENU_NUM_MUL)
            sync_clr.value = menu( MENU_NUM_CLR)
            for ch_no.value in range(0, ch_max.value):
                lret.value = ccnt.CntSetOperationMode(cnt_id, ch_no, phase, mul, sync_clr)
                ccnt.CntGetErrorString(lret, buf)
                print(f"Ch{ch_no.value} CntSetOperationMode RetCode = {lret.value}: {buf.value.decode('sjis')}")
                if lret.value != ccnt.CNT_ERR_SUCCESS:
                    break
        #----------------------------------------
        # Set notification        
        #----------------------------------------
        elif input_data.value == MENU_NOTIFICATION:  
            while True:
                input_data.value = menu( MENU_NUM_NOTIFICATION)
                #----------------------------------------
                # Back to Previous interface
                #----------------------------------------
                if input_data.value == MENU_NOTIFICATION_BACK:
                    break
                #----------------------------------------
                # Set count match notification & compare register
                #----------------------------------------
                elif input_data.value == MENU_COUNTMATCH:
                    ChNo = menu( MENU_NUM_COUNT_MATCH_CH)
                    reg_data = [0, 0]
                    reg_data[0] = menu( MENU_NUM_SET_REG0)
                    reg_data[1] = menu( MENU_NUM_SET_REG1)
                    for RegNo in range(0, 2):
                        lret.value = ccnt.CntNotifyCountUp(cnt_id, ChNo, RegNo, reg_data[RegNo], 0)
                        ccnt.CntGetErrorString(lret, buf)
                        print(f"CntNotifyCountUp RetCode = {lret.value}: {buf.value.decode('sjis')}")
                        if lret.value != ccnt.CNT_ERR_SUCCESS:
                            break
                    #----------------------------------------
                    # Set count match callback
                    #----------------------------------------
                    lret.value = ccnt.CntCountUpCallbackProc(cnt_id, pcount_callback, "Match Count")
                    #----------------------------------------
                    # The error check is commented out for devices that do not have the event notification function
                    # If you need the error check, uncomment it
                    #----------------------------------------
                    #ccnt.CntGetErrorString(lret, buf)
                    #print(f"CntCountUpCallbackProc RetCode = {lret.value}: {buf.value.decode('sjis')}")
                    #if lret.value != ccnt.CNT_ERR_SUCCESS:
                    #    continue
                #----------------------------------------
                # Set counter error notification
                #----------------------------------------
                elif input_data.value == MENU_COUNTERROR:
                    input_data.value = menu( MENU_NUM_COUNTERROR)
                    if input_data.value == 1: # Notify
                        lret.value = ccnt.CntNotifyCounterError(cnt_id, 0)
                        ccnt.CntGetErrorString(lret, buf)
                        print(f"CntNotifyCounterError RetCode = {lret.value}: {buf.value.decode('sjis')}")
                        if lret.value != ccnt.CNT_ERR_SUCCESS:
                            continue
                    else:               # Not Notify
                        lret.value = ccnt.CntStopNotifyCounterError(cnt_id)
                        ccnt.CntGetErrorString(lret, buf)
                        print(f"CntStopNotifyCounterError RetCode = {lret.value}: {buf.value.decode('sjis')}")
                        if lret.value != ccnt.CNT_ERR_SUCCESS:
                            continue
                    #----------------------------------------
                    # Set counter error callback
                    #----------------------------------------
                    lret.value = ccnt.CntCounterErrorCallbackProc(cnt_id, pcount_error_callback, "Count Error")
                    ccnt.CntGetErrorString(lret, buf)
                    print(f"CntCounterErrorCallbackProc RetCode = {lret.value}: {buf.value.decode('sjis')}")
                    if lret.value != ccnt.CNT_ERR_SUCCESS:
                        continue
                #----------------------------------------
                # Set carry/borrow notification
                #----------------------------------------
                elif input_data.value == MENU_CARRYBORROW:
                    input_data.value = menu( MENU_NUM_CARRYBORROW)
                    if input_data.value == 1: # Notify
                        lret.value = ccnt.CntNotifyCarryBorrow(cnt_id, 0)
                        ccnt.CntGetErrorString(lret, buf)
                        print(f"CntNotifyCarryBorrow RetCode = {lret.value}: {buf.value.decode('sjis')}")
                        if lret.value != ccnt.CNT_ERR_SUCCESS:
                            continue
                    else:               # Not Notify
                        lret.value = ccnt.CntStopNotifyCarryBorrow(cnt_id)
                        ccnt.CntGetErrorString(lret, buf)
                        print(f"CntStopNotifyCarryBorrow RetCode = {lret.value}: {buf.value.decode('sjis')}")
                        if lret.value != ccnt.CNT_ERR_SUCCESS:
                            continue
                    #----------------------------------------
                    # Set carry/borrow callback
                    #----------------------------------------
                    lret.value = ccnt.CntCarryBorrowCallbackProc(cnt_id, pcarray_borrow_callback, "Carry/Borrow")
                    ccnt.CntGetErrorString(lret, buf)
                    print(f"CntCarryBorrowCallbackProc RetCode = {lret.value}: {buf.value.decode('sjis')}")
                    if lret.value != ccnt.CNT_ERR_SUCCESS:
                        continue
        #----------------------------------------
        # Set hardware event
        #----------------------------------------
        elif input_data.value == MENU_HARDWARE_EVENT:
            while True:
                input_data.value = menu( MENU_NUM_HARDWARE_SETTINGS)
                #----------------------------------------
                # Back to previous interface
                #----------------------------------------
                if input_data.value == MENU_HARDWARE_EVENT_BACK:
                    break
                #----------------------------------------
                # Set hardware event of control input signal
                #----------------------------------------
                elif input_data.value == MENU_HARDWARE_INPUT:
                    while True:
                        input_data.value = menu( MENU_NUM_HARDWARE_INPUT)
                        #----------------------------------------
                        # Back to previous interface
                        #----------------------------------------
                        if input_data.value == MENU_INPUT_SIGANL_BACK:
                            break
                        #----------------------------------------
                        # Set kind of hardware event of control input signal
                        #----------------------------------------
                        elif input_data.value == MENU_INPUT_EVENT_TYPE:
                            input_event.value = menu( MENU_NUM_INPUT_TYPE)
                        #----------------------------------------
                        # Set control input signal direction of RF0
                        #----------------------------------------
                        elif input_data.value == MENU_RF0_DIRECTION:
                            RF0.value = menu( MENU_NUM_RF0)
                        #----------------------------------------
                        # Set preset value
                        #----------------------------------------
                        elif input_data.value == MENU_RF0_VALUDE:
                            RF0_value.value = menu( MENU_NUM_RF0_VALUE)
                        #----------------------------------------
                        # Set control input signal direction of RF1
                        #----------------------------------------
                        elif input_data.value == MENU_RF1_DIRECTION:
                            RF1.value = menu( MENU_NUM_RF1)
                        #----------------------------------------
                        # Set hardware event of control input signal
                        #----------------------------------------
                        for ChNo in range(0, CHANNEL_NUM):
                            lret.value = ccnt.CntSetInputHardwareEvent(cnt_id, ChNo, input_event, RF0, RF1, 0)
                            ccnt.CntGetErrorString(lret, buf)
                            print(f"CntSetInputHardwareEvent RetCode = {lret.value}: {buf.value.decode('sjis')}")
                            if lret.value != ccnt.CNT_ERR_SUCCESS:
                                break
                        for ChNo in range(0, CHANNEL_NUM):
                            if input_event.value == ccnt.CNT_IEVENT_PRESET:
                                lret.value = ccnt.CntSetPresetRegister(cnt_id, ChNo, RF0_value, 0)
                                ccnt.CntGetErrorString(lret, buf)
                                print(f"CntSetPresetRegister RetCode = {lret.value}: {buf.value.decode('sjis')}")
                                if lret.value != ccnt.CNT_ERR_SUCCESS:
                                    break
                #----------------------------------------
                # Set hardware event of control output signal
                #----------------------------------------
                elif input_data.value == MENU_HARDWARE_OUTPUT:
                    while True:
                        input_data.value = menu( MENU_NUM_HARDWARE_OUTPUT)
                        #----------------------------------------
                        # Back to previous interface
                        #----------------------------------------
                        if input_data.value == MENU_OUTPUT_SIGANL_BACK:
                            break
                        #----------------------------------------
                        # Set kind of hardware event of control output signal
                        #----------------------------------------
                        elif input_data.value == MENU_OUTPUT_EVENT_TYPE:
                            output_event.value = menu( MENU_NUM_OUTPUT_TYPE)
                        #----------------------------------------
                        # Set one-shot pulse pulse width coefficient
                        #----------------------------------------
                        elif input_data.value == MENU_PULSE_WIDTH:
                            pulse_length.value = menu( MENU_NUM_PULSE_LENGTH)
                        #----------------------------------------
                        # Set hardware event of control output signal
                        #----------------------------------------
                        for ChNo in range(0, CHANNEL_NUM):
                            lret.value = ccnt.CntSetOutputHardwareEvent(cnt_id, ChNo, 0, output_event, pulse_length)
                            ccnt.CntGetErrorString(lret, buf)
                            print(f"CntSetOutputHardwareEvent RetCode = {lret.value}: {buf.value.decode('sjis')}")
                            if lret.value != ccnt.CNT_ERR_SUCCESS:
                                break
                #----------------------------------------
                # Set general output data
                #----------------------------------------
                elif input_data.value == MENU_GENERAL_OUTPUT:
                    output_channel = menu( MENU_NUM_OUTPUT_CHANNEL)
                    output_data = menu( MENU_NUM_OUTPUT_DATA)
                    lret.value = ccnt.CntOutputDOBit(cnt_id, output_channel, 0, output_data)
                    ccnt.CntGetErrorString(lret, buf)
                    print(f"CntOutputDOBit RetCode = {lret.value}: {buf.value.decode('sjis')}")
                    if lret.value != ccnt.CNT_ERR_SUCCESS:
                        continue
                #----------------------------------------
                # Set hardware event of count match
                #----------------------------------------
                elif input_data.value == MENU_COUNT_MATCH_EVENT:
                    count_event = menu( MENU_NUM_HARDWARE_COUNT)
                    #----------------------------------------
                    # Set preset register
                    #----------------------------------------
                    if count_event == ccnt.CNT_MATCH_PRESET:
                        RF0_value.value = menu( MENU_NUM_RF0_VALUE)
                    #----------------------------------------
                    # Set hardware event of count match
                    #----------------------------------------
                    for ChNo in range(0, CHANNEL_NUM):
                        if count_event == ccnt.CNT_MATCH_PRESET:
                            lret.value = ccnt.CntSetPresetRegister(cnt_id, ChNo, RF0_value, 0)
                            ccnt.CntGetErrorString(lret, buf)
                            print(f"CntSetPresetRegister RetCode = {lret.value}: {buf.value.decode('sjis')}")
                            if lret.value != ccnt.CNT_ERR_SUCCESS:
                                break
                        lret.value = ccnt.CntSetCountMatchHardwareEvent(cnt_id, ChNo, 0, count_event, 0)
                        ccnt.CntGetErrorString(lret, buf)
                        print(f"CntSetCountMatchHardwareEvent RetCode = {lret.value}: {buf.value.decode('sjis')}")
                        if lret.value != ccnt.CNT_ERR_SUCCESS:
                            break
        #----------------------------------------
        # Counter start
        #----------------------------------------
        elif input_data.value == MENU_START_COUNT:
            lret.value = ccnt.CntStartCount(cnt_id, ch_no_array, ch_max)
            ccnt.CntGetErrorString(lret, buf)
            print(f"CntStartCount RetCode = {lret.value}: {buf.value.decode('sjis')}")
        #----------------------------------------
        # Get count value
        #----------------------------------------
        elif input_data.value == MENU_READ_COUNT:    
            lret.value = ccnt.CntReadCount(cnt_id, ch_no_array, ch_max, cnt_data)
            if lret.value == ccnt.CNT_ERR_SUCCESS:
                print("===================================")
                print("    Input Data (Decimal)")
                print("===================================")
                for ch_no.value in range(0, ch_max.value):
                    print(f"Channel{ch_no.value}: {cnt_data[ch_no.value]}")
            ccnt.CntGetErrorString(lret, buf)
            print(f"CntReadCount RetCode = {lret.value}: {buf.value.decode('sjis')}")
        #----------------------------------------
        # Get status
        #----------------------------------------
        elif input_data.value == MENU_READ_STATUS:
            print("===================================")
            print("    Status")
            print("===================================")
            for ch_no.value in range(0, ch_max.value):                
                lret.value = ccnt.CntReadStatus(cnt_id, ch_no, ctypes.byref(sts))
                if lret.value == ccnt.CNT_ERR_SUCCESS:
                    print(f"Channel{ch_no.value}: {sts.value:x}h")
                ccnt.CntGetErrorString(lret, buf)
                print(f"CntReadStatus RetCode = {lret.value}: {buf.value.decode('sjis')}")
        #----------------------------------------
        # Set preset
        #----------------------------------------
        elif input_data.value == MENU_PRESET:
            input_preset.value = menu( MENU_NUM_PRESET)
            for ch_no.value in range(0, ch_max.value):
                preset_data[ch_no.value] = input_preset.value
            lret.value = ccnt.CntPreset(cnt_id, ch_no_array, ch_max, preset_data)
            ccnt.CntGetErrorString(lret, buf)
            print(f"CntPreset RetCode = {lret.value}: {buf.value.decode('sjis')}")
        #----------------------------------------
        # Set reset
        #----------------------------------------
        elif input_data.value == MENU_RESET:
            lret.value = ccnt.CntZeroClearCount(cnt_id, ch_no_array, ch_max)
            ccnt.CntGetErrorString(lret, buf)
            print(f"CntZeroClearCount RetCode = {lret.value}: {buf.value.decode('sjis')}")
        #----------------------------------------
        # Counter stop
        #----------------------------------------
        elif input_data.value == MENU_STOP_COUNT:
            lret.value = ccnt.CntStopCount(cnt_id, ch_no_array, ch_max)
            ccnt.CntGetErrorString(lret, buf)
            print(f"CntStopCount RetCode = {lret.value}: {buf.value.decode('sjis')}")
        #----------------------------------------
        # Exit
        #----------------------------------------
        elif input_data.value == MENU_CNT_CLOSE:
            lret.value = ccnt.CntExit(cnt_id)
            ccnt.CntGetErrorString(lret, buf)
            print(f"CntExit RetCode = {lret.value}: {buf.value.decode('sjis')}")
    sys.exit()    


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