Imports System.IO.Ports
Imports System.Math
Imports System.Reflection
Imports System.Runtime.InteropServices.ComTypes
Imports System.Runtime.Remoting
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock
Imports Microsoft
Imports Mysqlx
Imports Mysqlx.XDevAPI

Public Class MainForm
    Public Const FormVersion As String = "3.0.3"
    Private Const FormName As String = "DUALSONIC Product Manager Ver " & FormVersion

    Private Const TDNameSize As Int16 = 10
    Private Const StrLen As Int16 = 10
    Private Const WriteSerialLength As Int16 = 6
    Private Const SerialLength As Int16 = 9
    Private Const OldSerialParsingLength As Int16 = 12
    Private Const NewSerialParsingLength As Int16 = 16
    Private Const TwosComplement32Mask As Long = 4294967296

    Private Const OneDay As Int32 = 86400
    Private Const OneHour As Int32 = 3600
    Private Const OneMinute As Int16 = 60
    Private Const OneSecond As Int32 = 1000
    Public lowPrdCmdFlag As Boolean = False


    Private Enum ErrorList
        DataError = -5
        DBError = -4
        ResultError = -3
        ReceiveError = -2
        SendError = -1
        NonError
        MAXError
    End Enum

    Private tmpCmdMessage As String = String.Empty
    Private cmdMessage As String = String.Empty
    Private DBCmd As String = String.Empty

    '현재 선택한 제품 정보
    Public Structure ModelInfo
        Public Code As String
        Public Name As String
        Public DeviceTypeCode As String
        Public TDUseYn As String
        Public HifuUseYn As String
        Public RfUseYn As String
        Public EpUseYn As String
        Public LowProdUseYn As String
        Public LdmUseYn As String
    End Structure
    Public SelectedModelInfo As ModelInfo

    Private Sub StartForm()
        Me.Text = FormName
        Me.Size = New Size(1920, 800)

        UseSerialSetting(SerialPort1)
        ckbDBUse.Checked = True
    End Sub

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        StartForm()
        StartTop()
        ComboBoxSetting()
    End Sub

    Private Sub ComboBoxSetting()
        ModelCboSetting()
        ClassCboSetting()
        VersionCboSetting()
    End Sub

    Private Sub ModelCboSetting()
        comboBoxModel.Items.Clear()
        comboBoxModel.DisplayMember = "Key"
        comboBoxModel.ValueMember = "Value"

        DBCmd = "SELECT ModelName, ModelCode FROM " & ApplyModelInfoDB & " GROUP BY ModelCode"
        If DBQueryReader(DBCmd) Then
            Dim RowCount As Int16 = (sqlDataQuery.Length / 2) - 1
            Dim dataSetCount As Int32 = 0
            Dim ModelName As String
            Dim ModelCode As String

            For row = 0 To RowCount
                For col = 0 To 1
                    If col = 0 Then
                        ModelName = sqlDataQuery(dataSetCount)
                    ElseIf col = 1 Then
                        ModelCode = sqlDataQuery(dataSetCount)
                    End If
                    dataSetCount += 1
                Next
                comboBoxModel.Items.Add(New DictionaryEntry(ModelName, ModelCode))
            Next
        End If
        comboBoxModel.Text = "MAXIMUM"
    End Sub

    Private Sub ClassCboSetting()
        comboBoxClass.Items.Clear()
        comboBoxClass.DisplayMember = "Key"
        comboBoxClass.ValueMember = "Value"

        comboBoxClass.Items.Add(New DictionaryEntry("국내-세트", 1))
        comboBoxClass.Items.Add(New DictionaryEntry("국내-A/S", 2))
        comboBoxClass.Items.Add(New DictionaryEntry("국내-샵용", 3))
        comboBoxClass.Items.Add(New DictionaryEntry("해외-세트", 5))
        comboBoxClass.Items.Add(New DictionaryEntry("해외-A/S", 6))
        comboBoxClass.Items.Add(New DictionaryEntry("체험단", 7))

        comboBoxClass.SelectedIndex = 0
    End Sub

    Private Sub VersionCboSetting()
        comboBoxVersion.Items.Clear()
        comboBoxVersion.DisplayMember = "Key"
        comboBoxVersion.ValueMember = "Value"

        comboBoxVersion.Items.Add(New DictionaryEntry("V1", 0))
        comboBoxVersion.Items.Add(New DictionaryEntry("V2", 1))
        comboBoxVersion.Items.Add(New DictionaryEntry("V3", 2))
        comboBoxVersion.Items.Add(New DictionaryEntry("V4", 3))
        comboBoxVersion.Items.Add(New DictionaryEntry("V5", 4))
        comboBoxVersion.Items.Add(New DictionaryEntry("V6", 5))
        comboBoxVersion.Items.Add(New DictionaryEntry("V7", 6))
        comboBoxVersion.Items.Add(New DictionaryEntry("V8", 7))
        comboBoxVersion.Items.Add(New DictionaryEntry("V9", 8))

        comboBoxVersion.SelectedIndex = 0
    End Sub

    Private Sub TypeCboSetting()
        comboBoxType.Items.Clear()
        comboBoxType.DisplayMember = "Key"
        comboBoxType.ValueMember = "Value"

        DBCmd = "SELECT CartTypeName, CartTypeCode FROM " & ApplyModelInfoDB & " WHERE ModelCode = '" & comboBoxModel.SelectedItem.Value.ToString & "' GROUP BY CartTypeCode ORDER BY CartTypeCode ASC"
        If DBQueryReader(DBCmd) Then
            Dim RowCount As Int16 = (sqlDataQuery.Length / 2) - 1
            Dim dataSetCount As Int32 = 0
            Dim TypeName As String
            Dim TypeCode As String

            For row = 0 To RowCount
                For col = 0 To 1
                    If col = 0 Then
                        TypeName = sqlDataQuery(dataSetCount)
                    ElseIf col = 1 Then
                        TypeCode = sqlDataQuery(dataSetCount)
                    End If
                    dataSetCount += 1
                Next
                comboBoxType.Items.Add(New DictionaryEntry(TypeName, TypeCode))
            Next
        End If
        comboBoxType.SelectedIndex = 0
    End Sub

    Private Sub ColorCboSetting()
        comboBoxColor.Items.Clear()
        comboBoxColor.DisplayMember = "Key"
        comboBoxColor.ValueMember = "Value"

        DBCmd = "SELECT ColorName, ColorCode FROM " & ApplyModelInfoDB & " WHERE ModelCode = '" & comboBoxModel.SelectedItem.Value.ToString & "' AND CartTypeCode = '" & comboBoxType.SelectedItem.Value.ToString & "' ORDER BY ColorCode ASC"
        If DBQueryReader(DBCmd) Then
            Dim RowCount As Int16 = (sqlDataQuery.Length / 2) - 1
            Dim dataSetCount As Int32 = 0
            Dim ColorName As String
            Dim ColorCode As String

            For row = 0 To RowCount
                For col = 0 To 1
                    If col = 0 Then
                        ColorName = sqlDataQuery(dataSetCount)
                    ElseIf col = 1 Then
                        ColorCode = sqlDataQuery(dataSetCount)
                    End If
                    dataSetCount += 1
                Next
                comboBoxColor.Items.Add(New DictionaryEntry(ColorName, ColorCode))
            Next
        End If
        comboBoxColor.SelectedIndex = 0
    End Sub

    Private Function FindIndexMachingValue(ByVal comboBox As Windows.Forms.ComboBox, value As String) As Int16
        For index = 0 To comboBox.Items.Count - 1
            If comboBox.Items(index).Value.ToString = value Then
                Return index
            End If
        Next
        Return -1
    End Function

    Private Sub comboBoxModel_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBoxModel.SelectedIndexChanged
        If comboBoxModel.SelectedIndex <> -1 Then
            With SelectedModelInfo
                .Code = comboBoxModel.SelectedItem.Value.ToString
                .Name = comboBoxModel.SelectedItem.Key.ToString
                .TDUseYn = False
                .HifuUseYn = False
                .RfUseYn = False
                .EpUseYn = False
                .LdmUseYn = False
                .LowProdUseYn = False
            End With

            DBCmd = "SELECT DeviceTypeCode, TDUseYn, HifuUseYn, RfUseYn, EpUseYn, LdmUseYn, LowPriceUseYn FROM " & ApplyModelInfoDB & " WHERE ModelCode = '" & SelectedModelInfo.Code & "' LIMIT 1"
            If DBQueryReader(DBCmd) Then
                With SelectedModelInfo
                    .DeviceTypeCode = sqlDataQuery(0)
                    .TDUseYn = sqlDataQuery(1)
                    .HifuUseYn = sqlDataQuery(2)
                    .RfUseYn = sqlDataQuery(3)
                    .EpUseYn = sqlDataQuery(4)
                    .LdmUseYn = sqlDataQuery(5)
                    .LowProdUseYn = sqlDataQuery(6)
                End With
            End If

            ResetClear()
            ResultReset()

            If SelectedModelInfo.DeviceTypeCode = "D" Then
                SelectModeType = ModeList.DUALSONIC
                DUALForm_Setting()
                DUALSONIC_UseOn()
                Device_UseOn()
                btnALFuncReset1.Enabled = True

            ElseIf SelectedModelInfo.DeviceTypeCode = "A" Then
                If SelectedModelInfo.LdmUseYn = "Y" Then
                    SelectModeType = ModeList.LDM
                    LDMForm_Setting()
                    LDM_UseOn()
                    btnALFuncReset1.Enabled = False

                ElseIf SelectedModelInfo.LowProdUseYn = "Y" Then
                    SelectModeType = ModeList.LOW_PROD
                    LOW_PRODForm_Setting()
                    LOW_PROD_UseOn()
                    btnALFuncReset1.Enabled = False

                ElseIf SelectedModelInfo.HifuUseYn = "Y" And SelectedModelInfo.RfUseYn = "Y" And SelectedModelInfo.EpUseYn = "Y" Then
                    SelectModeType = ModeList.MAXIMUM
                    MAXForm_Setting()
                    MAXIMUM_UseOn()
                    btnALFuncReset1.Enabled = True

                ElseIf SelectedModelInfo.HifuUseYn = "Y" And SelectedModelInfo.RfUseYn = "N" And SelectedModelInfo.EpUseYn = "N" Then
                    SelectModeType = ModeList.EFFECT
                    EFFECTForm_Setting()
                    EFFECT_UseOn()
                    btnALFuncReset1.Enabled = True

                ElseIf SelectedModelInfo.HifuUseYn = "N" And SelectedModelInfo.RfUseYn = "Y" And SelectedModelInfo.EpUseYn = "Y" Then
                    SelectModeType = ModeList.ALTO
                    ALTOForm_Setting()
                    ALTO_UseOn()
                    btnALFuncReset1.Enabled = False

                End If
            End If

            TypeCboSetting()
            SetTDSearchLdmRowVisible()

        End If
    End Sub

    ''' <summary>LdmUseYn일 때만 검색 결과 TD Data의 Frequency2/Power2 행 표시, 레이아웃 높이 동적 조정</summary>
    Private Sub SetTDSearchLdmRowVisible()
        Dim visible As Boolean = (SelectedModelInfo.LdmUseYn = "Y")
        lbMesFreq2.Visible = visible
        txbMesFreq2.Visible = visible
        lbMesPw2.Visible = visible
        txbMesPw2.Visible = visible
        lbMesFreq3.Visible = visible
        txbMesFreq3.Visible = visible
        lbMesPw3.Visible = visible
        txbMesPw3.Visible = visible

        ' LDM 행 표시 시 gbTDSearch 확대, gbPDSearch/버튼 아래로 밀고, 패널·폼 높이 증가 (버튼은 항상 일렬 유지)
        Const LdmRowHeight As Integer = 45
        If visible Then
            gbTDSearch.Height = 270
            gbPDSearch.Top = 218 + (LdmRowHeight * 2)
            gbDBLoad.Height = 505 + (LdmRowHeight * 2)
            btnReset.Top = 515 + (LdmRowHeight * 2)
            btnEnd.Top = 514 + (LdmRowHeight * 2)
            btnwrite.Top = 515 + (LdmRowHeight * 2)
            pnLoadBot.Height = 667 + (LdmRowHeight * 2)
            pnDBLoad.Height = 757 + (LdmRowHeight * 2)
            Me.Height = 800 + (LdmRowHeight * 2)
        Else
            gbTDSearch.Height = 180
            gbPDSearch.Top = 218
            gbDBLoad.Height = 505
            btnReset.Top = 515
            btnEnd.Top = 514
            btnwrite.Top = 515
            pnLoadBot.Height = 667
            pnDBLoad.Height = 757
            Me.Height = 800
        End If
    End Sub

    Private Sub comboBoxType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBoxType.SelectedIndexChanged
        If comboBoxType.SelectedIndex <> -1 Then
            ColorCboSetting()
        End If
    End Sub

    Private Sub rdbDev_Click(sender As Object, e As EventArgs) Handles rdbDev.Click, rdbUseTime.Click

        Dim CheckOkNg As RadioButton = CType(sender, RadioButton)

        If rdbDev.Checked = True Then
            Device_UseOn()
        ElseIf rdbUseTime.Checked = True Then
            Select Case SelectModeType
                Case ModeList.DUALSONIC
                    DUALSONIC_UseOn()
                Case ModeList.MAXIMUM
                    MAXIMUM_UseOn()
                Case ModeList.ALTO
                    ALTO_UseOn()
                Case ModeList.EFFECT
                    EFFECT_UseOn()
                Case ModeList.LOW_PROD
                    LOW_PROD_UseOn()
                Case ModeList.LDM
                    LDM_UseOn()
                Case Else
                    MsgBox("잘못된 경로입니다.", vbCritical, FormName)
            End Select
        Else
            MsgBox("잘못된 경로입니다.", vbCritical)
        End If

    End Sub

    Private Sub Device_UseOn()
        gbDevice.Visible = True
        gbRunTime.Visible = False
        tcMAXUse.Visible = False
        gbDUALUse.Visible = False
        tcAltoUse.Visible = False
    End Sub

    Private Sub DUALSONIC_UseOn()
        gbDevice.Visible = False
        gbRunTime.Visible = True
        tcMAXUse.Visible = False
        gbDUALUse.Visible = True
        tcAltoUse.Visible = False
    End Sub

    Private Sub MAXIMUM_UseOn()
        gbDevice.Visible = False
        gbRunTime.Visible = True
        tcMAXUse.Visible = True
        gbDUALUse.Visible = False
        tcAltoUse.Visible = False
        EnsureMxAllTabs()
        tcMAXUse.SelectedIndex = 0
    End Sub

    Private Sub ALTO_UseOn()
        gbDevice.Visible = False
        gbRunTime.Visible = True
        tcMAXUse.Visible = False
        gbDUALUse.Visible = False
        tcAltoUse.Visible = True
    End Sub

    Private Sub EFFECT_UseOn()
        gbDevice.Visible = False
        gbRunTime.Visible = False
        tcMAXUse.Visible = False
        gbDUALUse.Visible = False
        tcAltoUse.Visible = False
    End Sub

    ''' <summary>LOW_PROD/LDM: tcMAXUse에서 「전체」탭만 남기고 HIFU/RF 탭을 제거합니다.</summary>
    Private Sub EnsureMxOnlyFullTab()
        If tcMAXUse.TabPages.Contains(tpMXHIFU) Then tcMAXUse.TabPages.Remove(tpMXHIFU)
        If tcMAXUse.TabPages.Contains(tpMXRF) Then tcMAXUse.TabPages.Remove(tpMXRF)
        If btnMxAllReset.Enabled = True Then btnMxAllReset.Enabled = False
    End Sub

    ''' <summary>MAXIMUM 등: HIFU/RF 탭을 다시 붙입니다.</summary>
    Private Sub EnsureMxAllTabs()
        If Not tcMAXUse.TabPages.Contains(tpMXHIFU) Then tcMAXUse.TabPages.Add(tpMXHIFU)
        If Not tcMAXUse.TabPages.Contains(tpMXRF) Then tcMAXUse.TabPages.Add(tpMXRF)
        If btnMxAllReset.Enabled = False Then btnMxAllReset.Enabled = True
    End Sub
    Private Sub LOW_PROD_UseOn()
        gbDevice.Visible = False
        gbRunTime.Visible = True
        tcMAXUse.Visible = True
        gbDUALUse.Visible = False
        tcAltoUse.Visible = False
        EnsureMxOnlyFullTab()
        tcMAXUse.SelectedIndex = 0
    End Sub

    Private Sub LDM_UseOn()
        gbDevice.Visible = False
        gbRunTime.Visible = True
        tcMAXUse.Visible = True
        gbDUALUse.Visible = False
        tcAltoUse.Visible = False
        EnsureMxOnlyFullTab()
        tcMAXUse.SelectedIndex = 0
    End Sub

    Private Sub btnComport_Click(sender As Object, e As EventArgs) Handles btnComport.Click
        'ResetClear()
        'ResultReset()

        If btnComport.Text = "검색" Then
            pnBottom.Enabled = False
            If LoadCompartList() Then
                btnComport.Text = "연결"
            End If
        ElseIf btnComport.Text = "연결" Then
            If cboComport.SelectedIndex = -1 Then
                MsgBox("연결하고자 하는 포트를 선택하여 주십시오.", vbExclamation, FormName)
            Else

                If UseSerial_Ch1.IsOpen Then
                    UseSerial_Ch1.Close()
                End If

                If OpenSerial(cboComport.SelectedItem) Then
                    pnBottom.Enabled = True
                End If
                btnComport.Text = "닫기"
            End If


        Else
            If UseSerial_Ch1.IsOpen Then
                UseSerial_Ch1.Close()
            End If
            cboComport.SelectedIndex = -1
            btnComport.Text = "검색"
        End If
    End Sub

    Private Function LoadCompartList() As Boolean
        Try
            cboComport.Items.Clear()
            Dim PortName() As String = SerialPort.GetPortNames

            For i = 0 To PortName.Length - 1
                cboComport.Items.Add(PortName(i))
            Next

            cboComport.Text = "SELECT!"
            Return True

        Catch ex As Exception
            MsgBox("COM 포트 불러오기 실패!" & vbCrLf & ex.Message, vbCritical, FormName)
            Return False
        End Try
    End Function

    Private Sub btnAuth_Click(sender As Object, e As EventArgs) Handles btnAuth.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_AuthFunc()
            Case ModeList.MAXIMUM, ModeList.ALTO, ModeList.EFFECT, ModeList.LOW_PROD, ModeList.LDM
                FlashDataReset()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub btnFwRead_Click(sender As Object, e As EventArgs) Handles btnFwRead.Click ''펌웨어 읽기 버튼 클릭 이벤트
        Select Case SelectModeType
            Case ModeList.DUALSONIC, ModeList.MAXIMUM, ModeList.ALTO, ModeList.EFFECT, ModeList.LOW_PROD, ModeList.LDM
                FwReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub


    Private Sub FwReadReady()
        txbFwBox.Visible = True
        txbFwBox.Text = "RUNNING"
        txbFwBox.BackColor = Color.GreenYellow
        txbFwBox.Refresh()
    End Sub

    Private Sub FwReadFunc()
        Dim stagecnt As Int16 = ErrorList.NonError
        Try
            FwReadReady()

            tmpCmdMessage = "dsfwver"
            cmdMessage = "[" & tmpCmdMessage & "]"
            If SendData(cmdMessage) Then
                stagecnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim ReceiveData As String = PollingReceiveData(10000)

            If ReceiveData = "False" Then
                stagecnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            Else
                txbFwVersion.Text = ReceiveData
                txbFwBox.Text = "SUCCESS"
                txbFwBox.BackColor = Color.Green
                GoTo End_Finish
            End If

        Catch ex As Exception
            MsgBox("펌웨어 읽기 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbFwBox.Text = "ERROR" + Str(stagecnt)
        txbFwBox.BackColor = Color.Red
        txbFwBox.Refresh()

End_Finish:
        txbFwBox.Refresh()
    End Sub



    Private Sub AuthReady()
        txbAuthBox.Visible = True
        txbAuthBox.Text = "RUNNING"
        txbAuthBox.BackColor = Color.GreenYellow
        txbAuthBox.Refresh()
    End Sub

    Private Sub DUALSONIC_AuthFunc()
        Const PollingTimeOut As Int16 = 10000
        Dim StageCnt As Int16 = ErrorList.NonError
        Try
            AuthReady()

            tmpCmdMessage = "cartauth"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim ReceiveData As String = PollingReceiveData(PollingTimeOut)

            If ReceiveData = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If ReceiveData = "success" Then
                txbAuthBox.Text = "SUCCESS"
                txbAuthBox.BackColor = Color.Green
                GoTo End_Finish
            Else
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

        Catch ex As Exception
            MsgBox("인증 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbAuthBox.Text = "ERROR" + Str(StageCnt)
        txbAuthBox.BackColor = Color.Red
End_Finish:
        txbAuthBox.Refresh()
    End Sub

    Private Sub FlashDataReset()
        Dim StageCnt As Int16 = ErrorList.NonError
        Dim TDNameStr As String = String.Empty

        Try
            AuthReady()

            If MsgBox("Flash Data 를 초기화 하시겠습니까?", vbExclamation + vbOKCancel, FormName) = MsgBoxResult.Ok Then
                If ckbDBUse.Checked = True Then

                    TDNameStr = TDNameLoad()

                    If TDNameStr = Str(ErrorList.SendError) Or TDNameStr = Str(ErrorList.ReceiveError) Then
                        MsgBox("TD 시리얼 번호를 확인할 수 없습니다. 저장하고자 하는 Serial 번호를 먼저 입력하여 주십시오.", vbCritical, FormName)
                        StageCnt = TDNameStr
                        GoTo FAIL_Finish
                    End If

                End If

                Dim ResetResult As Int16 = DataResetFunc()

                If ResetResult = ErrorList.SendError Or ResetResult = ErrorList.ReceiveError Then
                    StageCnt = ResetResult
                    GoTo FAIL_Finish
                End If

                If ckbDBUse.Checked = True Then
                    If ResetDBUpdate(TDNameStr) = False Then
                        StageCnt = ErrorList.DBError
                        GoTo FAIL_Finish
                    End If
                End If

            End If

            txbAuthBox.Text = "SUCCESS"
            txbAuthBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("초기화 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try
FAIL_Finish:
        txbAuthBox.Text = "ERROR" + Str(StageCnt)
        txbAuthBox.BackColor = Color.Red
End_Finish:
        txbAuthBox.Refresh()
    End Sub

    Private Function ResetDBUpdate(TDSerial As String) As Boolean
        If SelectedModelInfo.LdmUseYn = "Y" Then
            DBCmd = "UPDATE " & UseLDMCartridgeTbl & " SET TestStep = '11', Product_SN = NULL, Ct_Manufacture_Name = NULL, Ct_Manufacture_Year = NULL, Ct_Manufacture_Month = NULL, Ct_SN = NULL, Ct_Model = '', Ct_Type = '', Ct_Abroad = NULL, Ct_Agency = NULL , Ct_Frequency = NULL, Ct_Frequency2 = NULL, Ct_Frequency3 = NULL, Ct_Power = '0', Ct_Power2 = '0', Ct_Power3 = '0', Ct_Max_ShotCnt = '0', Ct_Remain_ShotCnt = '0' WHERE PV_SN = '" & TDSerial & "'"
        ElseIf SelectedModelInfo.TDUseYn = "Y" Then
            DBCmd = "UPDATE " & UseCartridgeTbl & " SET TestStep = '11', Product_SN = NULL, Ct_Manufacture_Name = NULL, Ct_Manufacture_Year = NULL, Ct_Manufacture_Month = NULL, Ct_SN = NULL, Ct_Model = '', Ct_Type = '', Ct_Abroad = NULL, Ct_Agency = NULL , Ct_Frequency = NULL, Ct_Power = '0', Ct_Max_ShotCnt = '0', Ct_Remain_ShotCnt = '0' WHERE PV_SN = '" & TDSerial & "'"
        End If
        If DBCommand(DBCmd) Then
            Return True
        End If
        Return False
    End Function

    Private Function DataResetFunc() As Boolean
        Dim retval As String

        tmpCmdMessage = "resetinf"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        retval = PollingReceiveData(1500)

        If retval <> "ok" Then
            Return ErrorList.ReceiveError
        End If

        Return ErrorList.NonError
    End Function

    Private Sub btnIfWrite_Click(sender As Object, e As EventArgs) Handles btnIfWrite.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_WriteFunc()
            Case ModeList.MAXIMUM, ModeList.ALTO, ModeList.EFFECT, ModeList.LOW_PROD, ModeList.LDM
                MAXIMUM_WriteFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Function InfoWriteStart() As Boolean

        InfoReadReady()

        If nudIfYear.Text.Replace(" ", "") = String.Empty Or nudIfMonth.Text.Replace(" ", "") = String.Empty Or nudIfSerial.Text.Replace(" ", "") = String.Empty Then
            MsgBox("빈 칸이 존재합니다.", vbExclamation, FormName)
            Return True
        End If

        If txbIfCode.Text = String.Empty Then
            txbIfCode.Text = "jomt"
        End If

        If comboBoxType.SelectedIndex < 0 Then
            MsgBox("타입을 선택하여 주십시오.", vbExclamation, FormName)
            Return True
        End If

        If comboBoxColor.SelectedIndex < 0 Then
            MsgBox("색상을 선택하여 주십시오.", vbExclamation, FormName)
            Return True
        End If

        If comboBoxClass.SelectedIndex < 0 Then
            MsgBox("유통구분을 선택하여 주십시오.", vbExclamation, FormName)
            Return True
        End If

        If comboBoxVersion.SelectedIndex < 0 Then
            MsgBox("버전을 선택하여 주십시오.", vbExclamation, FormName)
            Return True
        End If

        Return False

    End Function

    Private Sub DUALSONIC_WriteFunc()
        Dim StageCnt As Int16 = ErrorList.NonError
        Try
            If InfoWriteStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim ModelCode As String = SelectedModelInfo.Code
            Dim TypeCode As String = comboBoxType.SelectedItem.Value.ToString

            ''************************************
            ''*******   제조사코드 발신    *******
            ''************************************

            tmpCmdMessage = "cartmanu"
            cmdMessage = "[sb" & tmpCmdMessage & " " & "" & txbIfCode.Text & "" & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''*******  모델 넘버 발신   **********
            ''************************************

            tmpCmdMessage = "ctmodel"
            cmdMessage = "[sb" & tmpCmdMessage & " " & ModelCode & TypeCode & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''*******    Cart Type 전송   ********
            ''************************************

            tmpCmdMessage = "carttype"
            cmdMessage = "[sb" & tmpCmdMessage & " " & TypeCode & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''******  시리얼넘버 발신   **********
            ''************************************
            Dim SerialValue As String = SerialNumSetting(nudIfSerial.Value.ToString)
            Dim MonthParsing As String = MonthSetting(nudIfMonth.Value.ToString)

            If SerialValue = "FAIL" Or MonthParsing = "FAIL" Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            If SerialValue.Length <> WriteSerialLength Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim nudSerial = nudIfYear.Text & MonthParsing & Mid(SerialValue, 4, 3) & Mid(SerialValue, 1, 3)
            Dim productSerial = CInt(ModelCode) & CInt(TypeCode) & nudSerial
            txbIfNumber.Text = productSerial

            tmpCmdMessage = "cartsn"
            cmdMessage = "[sb" & tmpCmdMessage & " " & nudSerial & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            Else
                txbInfoBox.Text = "SUCCESS"
                txbInfoBox.BackColor = Color.Green
                GoTo End_Finish
            End If

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbInfoBox.Text = "ERROR" + Str(StageCnt)
        txbInfoBox.BackColor = Color.Red
End_Finish:
        txbInfoBox.Refresh()
    End Sub

    Private Sub MAXIMUM_WriteFunc()
        Dim StageCnt As Int16 = ErrorList.NonError
        Dim TDNameStr As String = String.Empty
        Const AboardCode As Int32 = 82
        Const AgencyCode As Int16 = 0

        Try
            If InfoWriteStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            If ckbDBUse.Checked = True Then

                TDNameStr = TDNameLoad()

                If TDNameStr = Str(ErrorList.SendError) Or TDNameStr = Str(ErrorList.ReceiveError) Then
                    MsgBox("TD 시리얼 번호를 확인할 수 없습니다. 저장하고자 하는 Serial 번호를 먼저 입력하여 주십시오.", vbExclamation, FormName)
                    StageCnt = TDNameStr
                    GoTo FAIL_Finish
                End If

                Dim DBExist As Boolean = TDNameDBLoad(TDNameStr)

                If DBExist = False Then
                    MsgBox("TD 시리얼 번호가 데이터베이스에 등록되어 있지 않습니다.", vbCritical, FormName)
                    StageCnt = ErrorList.ReceiveError
                    GoTo FAIL_Finish
                End If

            End If

            Dim SerialValue As String = SerialNumSetting(nudIfSerial.Value.ToString)
            Dim MonthParsing As String = MonthSetting(nudIfMonth.Value.ToString)
            Dim cartridgeSerial = SerialValue
            SerialValue = nudIfYear.Value & MonthParsing & SerialValue
            Dim TypeCode As String = comboBoxType.SelectedItem.Value.ToString
            Dim ColorCode As String = comboBoxColor.SelectedItem.Value.ToString
            Dim ClassCode As String = comboBoxClass.SelectedItem.Value.ToString
            Dim VersionCode As String = comboBoxVersion.SelectedItem.Value.ToString

            If SelectedModelInfo.Code.Length = 2 Then
                Dim QcOpt As String = ClassCode & TypeCode & VersionCode

                Dim ResultData As Int16 = OptimumInfoWrite(txbIfCode.Text, SerialValue, SelectedModelInfo.Code, ColorCode, QcOpt, AboardCode, AgencyCode)

                If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                    StageCnt = ResultData
                    GoTo FAIL_Finish
                End If

                txbIfNumber.Text = SelectedModelInfo.Code & ColorCode & SerialValue & QcOpt
            Else
                Dim ModelCode = SelectedModelInfo.Code & TypeCode

                Dim ResultData As Int16 = MAXInfoWrite(txbIfCode.Text, SerialValue, ModelCode, AboardCode, AgencyCode)

                If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                    StageCnt = ResultData
                    GoTo FAIL_Finish
                End If

                txbIfNumber.Text = ModelCode & Mid(SerialValue, 1, 4) & Mid(SerialValue, 8, 3) & Mid(SerialValue, 5, 3)
            End If

            If ckbDBUse.Checked = True Then
                Dim QueryTable As String = UseCartridgeTbl
                If SelectedModelInfo.LdmUseYn = "Y" Then
                    QueryTable = UseLDMCartridgeTbl
                ElseIf SelectedModelInfo.TDUseYn <> "Y" Then
                    QueryTable = NonCartridgeTbl
                End If

                Dim DBCmd = "UPDATE " & QueryTable & " SET " &
                                "Product_SN = '" & txbIfNumber.Text & "', " &
                                "Ct_Manufacture_Name = '" & txbIfCode.Text & "', " &
                                "Ct_Manufacture_Year = '" & nudIfYear.Value & "', " &
                                "Ct_Manufacture_Month = '" & nudIfMonth.Value & "', " &
                                "Ct_SN = '" & cartridgeSerial & "', " &
                                "Ct_Model = '" & SelectedModelInfo.Name & "', " &
                                "Ct_Type = '" & comboBoxType.SelectedItem.Key.ToString & "' " &
                                "WHERE PV_SN = '" & TDNameStr & "'"

                DBCommand(DBCmd)
            End If

            txbInfoBox.Text = "SUCCESS"
            txbInfoBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbInfoBox.Text = "ERROR" + Str(StageCnt)
        txbInfoBox.BackColor = Color.Red
End_Finish:
        txbInfoBox.Refresh()
    End Sub

    Private Function OptimumInfoWrite(Factory As String, ProdNum As String, ModelType As String, ModelOpt As String, QcOpt As String, AboardNum As Integer, AgencyNum As Integer) As Int16
        Dim retVal As String
        tmpCmdMessage = "wrsconf1"

        cmdMessage = "[" & tmpCmdMessage & " " & Factory & "," & ProdNum & "," & ModelType & "," & ModelOpt & "," & QcOpt & "," & AboardNum & "," & AgencyNum & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        retVal = PollingReceiveData(2000)

        If retVal <> Factory & "," & ProdNum & "," & ModelType & "," & ModelOpt & "," & QcOpt & "," & AboardNum & "," & AgencyNum Then
            Return ErrorList.ReceiveError
        End If

        Return ErrorList.NonError
    End Function

    Private Function MAXInfoWrite(Factory As String, ProdNum As String, ModelType As String, AboardNum As Integer, AgencyNum As Integer) As Int16
        Dim retVal As String
        tmpCmdMessage = "wrsconf1"

        cmdMessage = "[" & tmpCmdMessage & " " & Factory & "," & ProdNum & "," & ModelType & "," & AboardNum & "," & AgencyNum & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        retVal = PollingReceiveData(2000)

        If retVal <> Factory.ToLower & "," & ProdNum & "," & ModelType & "," & AboardNum & "," & AgencyNum Then
            Return ErrorList.ReceiveError
        End If

        Return ErrorList.NonError
    End Function

    Private Function MonthSetting(MonthData As String) As String
        If Len(MonthData) = 2 Then
            Return MonthData
        ElseIf Len(MonthData) = 1 Then
            Return "0" & MonthData
        Else
            Return "FAIL"
        End If
    End Function

    Private Function SerialNumSetting(SerialNum As String) As String
        For cnt = Len(SerialNum) To 5
            SerialNum = "0" + SerialNum
        Next

        If SerialNum.Length = 6 Then
            Return SerialNum
        Else
            Return "FAIL"
        End If
    End Function

    Private Sub btnIfRead_Click(sender As Object, e As EventArgs) Handles btnIfRead.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_ReadFunc()
            Case ModeList.MAXIMUM, ModeList.ALTO, ModeList.EFFECT, ModeList.LOW_PROD, ModeList.LDM
                MAXIMUM_ReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub DUALSONIC_ReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            InfoResest()
            InfoReadReady()

            ''************************************
            ''*******    제조사 코드 수신  *******
            ''************************************

            tmpCmdMessage = "cartmanu"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbIfCode.Text = DataResult

            ''************************************
            ''******   모델타입 수신    ********
            ''************************************

            tmpCmdMessage = "ctmodel"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            Dim ModelCode As Int16 = Val(Mid(DataResult, 1, 1))

            If ModelCode <> SelectedModelInfo.Code Then
                StageCnt = ErrorList.DataError
                MsgBox(SelectedModelInfo.Name & " 제품군이 아닙니다.", vbCritical, FormName)
                GoTo FAIL_Finish
            End If

            ''************************************
            ''*******    Cart Type 수신   ********
            ''************************************

            tmpCmdMessage = "carttype"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            Dim cartCode = Mid(DataResult, 1, 1)
            Dim index As Integer = FindIndexMachingValue(comboBoxType, cartCode)

            If index <> -1 Then
                comboBoxType.SelectedIndex = index
            Else
                MsgBox("없는 타입 번호입니다.", vbCritical, FormName)
                GoTo FAIL_Finish
            End If

            '************************************
            '******  시리얼넘버 발신   **********
            '************************************

            tmpCmdMessage = "cartsn"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If


            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            Dim serial = Long.Parse(DataResult)

            If serial < 0 Then
                serial += TwosComplement32Mask
            End If

            Dim SerialValue As String = serial.ToString

            If (ModelCode & cartCode & SerialValue).Length <> OldSerialParsingLength Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            txbIfNumber.Text = ModelCode & cartCode & Mid(SerialValue, 1, 2) & Mid(SerialValue, 3, 2) & Mid(SerialValue, 8, 3) & Mid(SerialValue, 5, 3)

            nudIfYear.Text = Mid(SerialValue, 1, 2)
            nudIfMonth.Text = Mid(SerialValue, 3, 2)
            nudIfSerial.Text = Mid(SerialValue, 5, 6)

            Dim ClassCode = Mid(SerialValue, 5, 1)
            index = FindIndexMachingValue(comboBoxClass, ClassCode)
            If index <> -1 Then
                comboBoxClass.SelectedIndex = index
            Else
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim ColorCode = Mid(SerialValue, 6, 1)
            index = FindIndexMachingValue(comboBoxColor, ColorCode)
            If index <> -1 Then
                comboBoxColor.SelectedIndex = index
            Else
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            txbInfoBox.Text = "SUCCESS"
            txbInfoBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbInfoBox.Text = "ERROR" + Str(StageCnt)
        txbInfoBox.BackColor = Color.Red
End_Finish:
        txbInfoBox.Refresh()
    End Sub

    Private Sub MAXIMUM_ReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            InfoReadReady()

            Dim ResultData As String = ReadInfo()

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim ResultDataArray() As String = ResultData.Split(",")
            Dim MakeCode As String = ResultDataArray(0)
            Dim SerialValue As String = ResultDataArray(1)

            If ResultDataArray.Length = 7 Then  'JOMT,2511000110,71,A,750,0,0
                Dim ModelCode As String = ResultDataArray(2)
                Dim ColorCode As String = ResultDataArray(3)
                Dim QcOptValue As String = ResultDataArray(4)
                Dim TypeCode As String = Mid(QcOptValue, 2, 1)
                Dim ClassCode As String = Mid(QcOptValue, 1, 1)
                Dim VersionCode As String = Mid(QcOptValue, 3, 1)
                Dim index As Integer

                txbIfCode.Text = MakeCode

                If (ModelCode & ColorCode & SerialValue & QcOptValue).Length <> NewSerialParsingLength Then
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                txbIfNumber.Text = ModelCode & ColorCode & SerialValue & QcOptValue
                nudIfYear.Text = Mid(SerialValue, 1, 2)
                nudIfMonth.Text = Mid(SerialValue, 3, 2)
                nudIfSerial.Text = Mid(SerialValue, 5, 6)

                If ModelCode <> SelectedModelInfo.Code Then
                    StageCnt = ErrorList.DataError
                    MsgBox(SelectedModelInfo.Name & " 제품군이 아닙니다.", vbCritical, FormName)
                    GoTo FAIL_Finish
                End If

                index = FindIndexMachingValue(comboBoxType, TypeCode)
                If index <> -1 Then
                    comboBoxType.SelectedIndex = index
                Else
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                index = FindIndexMachingValue(comboBoxColor, ColorCode)
                If index <> -1 Then
                    comboBoxColor.SelectedIndex = index
                Else
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                index = FindIndexMachingValue(comboBoxClass, ClassCode)
                If index <> -1 Then
                    comboBoxClass.SelectedIndex = index
                Else
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                index = FindIndexMachingValue(comboBoxVersion, VersionCode)
                If index <> -1 Then
                    comboBoxVersion.SelectedIndex = index
                Else
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                txbInfoBox.Text = "SUCCESS"
                txbInfoBox.BackColor = Color.Green
                GoTo End_Finish

            Else                                'jomt,2511710001,70,82,0
                Dim ProdValue As String = ResultDataArray(2)
                Dim ModelCode As String = Mid(ProdValue, 1, 1)
                Dim TypeCode As String = Mid(ProdValue, 2, 1)
                Dim ColorCode = Mid(SerialValue, 6, 1)
                Dim ClassCode = Mid(SerialValue, 5, 1)
                Dim index As Integer

                txbIfCode.Text = MakeCode

                If (ProdValue & SerialValue).Length <> OldSerialParsingLength Then
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                txbIfNumber.Text = ProdValue & Mid(SerialValue, 1, 2) & Mid(SerialValue, 3, 2) & Mid(SerialValue, 8, 3) & Mid(SerialValue, 5, 3)
                nudIfYear.Text = Mid(SerialValue, 1, 2)
                nudIfMonth.Text = Mid(SerialValue, 3, 2)
                nudIfSerial.Text = Mid(SerialValue, 5, 6)

                If ModelCode <> SelectedModelInfo.Code Then
                    StageCnt = ErrorList.DataError
                    MsgBox(SelectedModelInfo.Name & " 제품군이 아닙니다.", vbCritical, FormName)
                    GoTo FAIL_Finish
                End If

                index = FindIndexMachingValue(comboBoxType, TypeCode)
                If index <> -1 Then
                    comboBoxType.SelectedIndex = index
                Else
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                index = FindIndexMachingValue(comboBoxColor, ColorCode)
                If index <> -1 Then
                    comboBoxColor.SelectedIndex = index
                Else
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                index = FindIndexMachingValue(comboBoxClass, ClassCode)
                If index <> -1 Then
                    comboBoxClass.SelectedIndex = index
                Else
                    StageCnt = ErrorList.DataError
                    GoTo FAIL_Finish
                End If

                txbInfoBox.Text = "SUCCESS"
                txbInfoBox.BackColor = Color.Green
                GoTo End_Finish

            End If

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbInfoBox.Text = "ERROR" + Str(StageCnt)
        txbInfoBox.BackColor = Color.Red
End_Finish:
        txbInfoBox.Refresh()
    End Sub

    Private Function ReadInfo() As String
        tmpCmdMessage = "rdsconf1"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(2000)

        If InStr(1, retval, ",") > 0 Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If

    End Function

    Private Sub InfoReadReady()
        txbInfoBox.Visible = True
        txbInfoBox.Text = "RUNNING"
        txbInfoBox.BackColor = Color.GreenYellow
        txbInfoBox.Refresh()
    End Sub

    Private Sub InfoResest()
        txbIfNumber.Text = String.Empty
        txbIfNumber.Refresh()
        txbIfCode.Text = String.Empty
        txbIfCode.Refresh()
        nudIfYear.Text = Format(Now, "yy")
        nudIfYear.Refresh()
        nudIfMonth.Text = Format(Now, "MM")
        nudIfMonth.Refresh()
        nudIfSerial.Text = String.Empty
        nudIfSerial.Refresh()

        comboBoxType.SelectedIndex = 0
        comboBoxColor.SelectedIndex = 0
        comboBoxClass.SelectedIndex = 0
        comboBoxVersion.SelectedIndex = 0

        txbInfoBox.Visible = False
        txbInfoBox.Text = String.Empty
        txbInfoBox.BackColor = Color.FromArgb(0, 192, 0)
    End Sub

    Private Function TDNameDBLoad(TDName As String) As Boolean
        If SelectedModelInfo.LdmUseYn = "Y" Then
            DBCmd = "SELECT EXISTS (SELECT 1 FROM " & UseLDMCartridgeTbl & " WHERE PV_SN = '" & TDName & "')"
        ElseIf SelectedModelInfo.TDUseYn = "Y" Then
            DBCmd = "SELECT EXISTS (SELECT 1 FROM " & UseCartridgeTbl & " WHERE PV_SN = '" & TDName & "')"
        Else
            DBCmd = "SELECT EXISTS (SELECT 1 FROM " & NonCartridgeTbl & " WHERE PV_SN = '" & TDName & "')"
        End If
        Return DBQueryScalar(DBCmd)
    End Function

    Private Function TDNameLoad() As String

        Dim retVal As String

        tmpCmdMessage = "settdsn"

        If SelectedModelInfo.LdmUseYn = "Y" Then
            tmpCmdMessage = "setldmsn"
        End If

        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return Str(ErrorList.SendError)
        End If

        retVal = PollingReceiveData(1500)

        If retVal.Length = TDNameSize Then
            Return retVal
        Else
            Return Str(ErrorList.ReceiveError)
        End If

    End Function

    Private Sub btnArRead_Click(sender As Object, e As EventArgs) Handles btnArRead.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_AreaReadFunc()
            Case ModeList.MAXIMUM, ModeList.ALTO, ModeList.EFFECT, ModeList.LOW_PROD, ModeList.LDM
                MAXIMUM_AreaReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub AreaReadStart()
        txbAreaBox.Visible = True
        txbAreaBox.Text = "RUNNING"
        txbAreaBox.BackColor = Color.GreenYellow
        txbAreaBox.Refresh()
    End Sub

    Private Sub DUALSONIC_AreaReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError
        Try
            AreaReadStart()

            ''************************************
            ''*******    txbAbroad 수신    *******
            ''************************************

            tmpCmdMessage = "ctabroad"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbAbroad.Text = DataResult

            ''************************************
            ''*******  txbAgency 수신   **********
            ''************************************

            tmpCmdMessage = "ctagency"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbAgency.Text = DataResult

            txbAreaBox.Text = "SUCCESS"
            txbAreaBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbAreaBox.Text = "ERROR" + Str(StageCnt)
        txbAreaBox.BackColor = Color.Red
End_Finish:
        txbAreaBox.Refresh()
    End Sub

    Private Sub MAXIMUM_AreaReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError
        Try
            AreaReadStart()

            Dim ResultData As String = ReadInfo()

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim ResultDataArray() As String = ResultData.Split(",")

            If ResultDataArray.Length = 7 Then  'JOMT,2511000110,71,A,750,0,0
                txbAbroad.Text = ResultDataArray(5)
                txbAgency.Text = ResultDataArray(6)
            Else                                'jomt,2511710001,70,82,0
                txbAbroad.Text = ResultDataArray(3)
                txbAgency.Text = ResultDataArray(4)
            End If

            txbAreaBox.Text = "SUCCESS"
            txbAreaBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbAreaBox.Text = "ERROR" + Str(StageCnt)
        txbAreaBox.BackColor = Color.Red
End_Finish:
        txbAreaBox.Refresh()
    End Sub

    Private Function AreaWriteStart() As Boolean
        AreaReadStart()

        If txbAbroad.Text = String.Empty Or txbAgency.Text = String.Empty Then
            MsgBox("빈 칸이 존재합니다.", vbExclamation, FormName)
            Return True
        End If

        Return False
    End Function

    Private Sub btnArWrite_Click(sender As Object, e As EventArgs) Handles btnArWrite.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_AreaWriteFunc()
            Case ModeList.MAXIMUM, ModeList.EFFECT, ModeList.ALTO, ModeList.LOW_PROD, ModeList.LDM
                MAXIMUM_AreaWriteFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub DUALSONIC_AreaWriteFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            If AreaWriteStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''*******    txbAbroad 발신    ********
            ''************************************

            tmpCmdMessage = "ctabroad"
            cmdMessage = "[sb" & tmpCmdMessage & " " & txbAbroad.Text & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''******  txbAgency 발신   ********
            ''************************************

            tmpCmdMessage = "ctagency"
            cmdMessage = "[sb" & tmpCmdMessage & " " & txbAgency.Text & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            txbAreaBox.Text = "SUCCESS"
            txbAreaBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbAreaBox.Text = "ERROR" + Str(StageCnt)
        txbAreaBox.BackColor = Color.Red
End_Finish:
        txbAreaBox.Refresh()
    End Sub

    Private Sub MAXIMUM_AreaWriteFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            If AreaWriteStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim ResultData As String = ReadInfo()

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim ResultDataArray() As String = ResultData.Split(",")

            If ResultDataArray.Length = 7 Then  'JOMT,2511000110,71,A,750,0,0
                ResultData = OptimumInfoWrite(ResultDataArray(0), ResultDataArray(1), ResultDataArray(2), ResultDataArray(3), ResultDataArray(4), txbAbroad.Text, txbAgency.Text)
            Else                                'jomt,2511710001,70,82,0
                ResultData = MAXInfoWrite(ResultDataArray(0), ResultDataArray(1), ResultDataArray(2), txbAbroad.Text, txbAgency.Text)
            End If

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            If ckbDBUse.Checked = True Then
                Dim TDNameStr = TDNameLoad()

                If TDNameStr = Str(ErrorList.SendError) Or TDNameStr = Str(ErrorList.ReceiveError) Then
                    MsgBox("TD 시리얼 번호를 확인할 수 없습니다. 저장하고자 하는 Serial 번호를 먼저 입력하여 주십시오.", vbExclamation, FormName)
                    StageCnt = TDNameStr
                    GoTo FAIL_Finish
                End If

                Dim DBExist As Boolean = TDNameDBLoad(TDNameStr)

                If DBExist = False Then
                    MsgBox("TD 시리얼 번호가 데이터베이스에 등록되어 있지 않습니다.", vbCritical, FormName)
                    StageCnt = ErrorList.ReceiveError
                    GoTo FAIL_Finish
                End If

                Dim QueryTable As String = UseCartridgeTbl
                If SelectedModelInfo.TDUseYn <> "Y" Then
                    QueryTable = NonCartridgeTbl
                ElseIf SelectedModelInfo.LdmUseYn = "Y" Then
                    QueryTable = UseLDMCartridgeTbl
                End If

                DBCmd = "UPDATE " & QueryTable & " SET " &
                        "Ct_Abroad = '" & txbAbroad.Text.Trim() & "', " &
                        "Ct_Agency = '" & txbAgency.Text.Trim() & "' " &
                        "WHERE PV_SN = '" & TDNameStr & "'"

                DBCommand(DBCmd)
            End If

            txbAreaBox.Text = "SUCCESS"
            txbAreaBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbAreaBox.Text = "ERROR" + Str(StageCnt)
        txbAreaBox.BackColor = Color.Red
End_Finish:
        txbAreaBox.Refresh()
    End Sub

    Private Sub btnTdRead_Click(sender As Object, e As EventArgs) Handles btnTdRead.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_TDReadFunc()
            Case ModeList.MAXIMUM, ModeList.ALTO, ModeList.EFFECT, ModeList.LOW_PROD, ModeList.LDM
                MAXIMUM_TDReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub TDReadStart()
        txbTDBox.Visible = True
        txbTDBox.Text = "RUNNING"
        txbTDBox.BackColor = Color.GreenYellow
        txbTDBox.Refresh()
    End Sub

    Private Sub DUALSONIC_TDReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError
        Try
            TDReadStart()

            MsgBox("현재 기능 없음")

            txbTDBox.Text = "기능없음"
            txbTDBox.BackColor = Color.Red
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbTDBox.Text = "ERROR" + Str(StageCnt)
        txbTDBox.BackColor = Color.Red
End_Finish:
        txbTDBox.Refresh()
    End Sub

    Private Sub MAXIMUM_TDReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            TDReadStart()

            Dim ResultData As String = TDNameLoad()

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            If ResultData.Length <> TDNameSize Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            txbTDSerial.Text = ResultData

            txbTDBox.Text = "SUCCESS"
            txbTDBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbTDBox.Text = "ERROR" + Str(StageCnt)
        txbTDBox.BackColor = Color.Red
End_Finish:
        txbTDBox.Refresh()
    End Sub

    Private Function TDWriteStart() As Boolean
        TDReadStart()

        If txbTDSerial.Text.Replace(" ", "") = "" Then
            MsgBox("빈 칸이 존재합니다.", vbExclamation, FormName)
            Return True
        End If

        If txbTDSerial.Text.Length <> TDNameSize Then
            MsgBox("TD 규격에 맞지 않는 데이터 입니다.(10자)", vbExclamation, FormName)
            Return True
        End If

        Return False
    End Function

    Private Sub btnTdWrite_Click(sender As Object, e As EventArgs) Handles btnTdWrite.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_TDWrtieFunc()
            Case ModeList.MAXIMUM, ModeList.ALTO, ModeList.EFFECT, ModeList.LOW_PROD, ModeList.LDM
                MAXIMUM_TDWrtieFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub DUALSONIC_TDWrtieFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            If TDWriteStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            MsgBox("해당 기능 없음")

            txbTDBox.Text = "SUCCESS"
            txbTDBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbTDBox.Text = "ERROR" + Str(StageCnt)
        txbTDBox.BackColor = Color.Red
End_Finish:
        txbTDBox.Refresh()
    End Sub

    Private Sub MAXIMUM_TDWrtieFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            If TDWriteStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim ResultData As String = TDDataWrtie(txbTDSerial.Text)

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            txbTDBox.Text = "SUCCESS"
            txbTDBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbTDBox.Text = "ERROR" + Str(StageCnt)
        txbTDBox.BackColor = Color.Red
End_Finish:
        txbTDBox.Refresh()
    End Sub

    Private Function TDDataWrtie(SaveData As String) As String
        tmpCmdMessage = "settdsn"
        If SelectedModelInfo.LdmUseYn = "Y" Then
            tmpCmdMessage = "setldmsn"
        End If
        cmdMessage = "[" & tmpCmdMessage & " " & SaveData & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return Str(ErrorList.SendError)
        End If

        Dim retVal As String = PollingReceiveData(1500)

        If retVal = SaveData.ToLower Or retVal = SaveData Then
            Return retVal
        Else
            Return Str(ErrorList.ReceiveError)
        End If
    End Function

    Private Sub btnFPRead_Click(sender As Object, e As EventArgs) Handles btnFPRead.Click, btnLDMFPRead.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_FPReadFunc()
            Case ModeList.MAXIMUM, ModeList.EFFECT, ModeList.LOW_PROD
                MAXIMUM_FPReadFunc()
            Case ModeList.LDM
                LDM_FPReadFunc()
            Case ModeList.ALTO
                txbFPBox.Text = "ERROR"
                txbFPBox.BackColor = Color.Red
                txbFPBox.Refresh()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub FPReadStart()
        If SelectedModelInfo.LdmUseYn = "Y" Then
            txbLDMFPBox.Visible = True
            txbLDMFPBox.Text = "RUNNING"
            txbLDMFPBox.BackColor = Color.GreenYellow
            txbLDMFPBox.Refresh()
        Else
            txbFPBox.Visible = True
            txbFPBox.Text = "RUNNING"
            txbFPBox.BackColor = Color.GreenYellow
            txbFPBox.Refresh()
        End If
    End Sub

    Private Sub DUALSONIC_FPReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            FPReadStart()

            ''************************************
            ''*******  Cart Frq Info 수신 ********
            ''************************************

            tmpCmdMessage = "cartfreq"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbFreq.Text = DataResult

            ''************************************
            ''*******  Cart PWR Info 수신 ********
            ''************************************

            tmpCmdMessage = "ctpower"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbPower.Text = Val(DataResult) / 100

            txbFPBox.Text = "SUCCESS"
            txbFPBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbFPBox.Text = "ERROR" + Str(StageCnt)
        txbFPBox.BackColor = Color.Red
End_Finish:
        txbFPBox.Refresh()
    End Sub

    Private Sub MAXIMUM_FPReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            FPReadStart()

            If SelectedModelInfo.HifuUseYn <> "Y" Then
                txbFPBox.Text = "해당안됨"
                txbFPBox.BackColor = Color.Green
                GoTo End_Finish
            End If

            Dim ResultData As String = FPDataRead()

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            txbFreq.Text = Mid(ResultData, 1, InStr(ResultData, ",") - 1)
            txbPower.Text = Val(Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)) / 100

            txbFPBox.Text = "SUCCESS"
            txbFPBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbFPBox.Text = "ERROR" + Str(StageCnt)
        txbFPBox.BackColor = Color.Red
End_Finish:
        txbFPBox.Refresh()
    End Sub


    Private Sub LDM_FPReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            FPReadStart()

            Dim ResultData As String = LDMFPDataRead()

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If
            Dim dataArray() As String = ResultData.Split(",")
            Dim freqBoxes() As System.Windows.Forms.TextBox = {txbFreq, txbFreq2, txbFreq3}
            Dim powerBoxes() As System.Windows.Forms.TextBox = {txbPower, txbPower2, txbPower3}
            For i = 0 To Math.Min(dataArray.Length - 4, freqBoxes.Length - 1)
                freqBoxes(i).Text = dataArray(i)
                powerBoxes(i).Text = (Val(dataArray(i + 3)) / 100).ToString()
            Next

            txbLDMFPBox.Text = "SUCCESS"
            txbLDMFPBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbLDMFPBox.Text = "ERROR" + Str(StageCnt)
        txbLDMFPBox.BackColor = Color.Red
End_Finish:
        txbLDMFPBox.Refresh()
    End Sub

    Private Function FPDataRead() As String
        tmpCmdMessage = "settdcnf"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return Str(ErrorList.SendError)
        End If

        Dim retVal As String = PollingReceiveData(500)

        If InStr(1, retVal, ",") > 0 Then
            Return retVal
        Else
            Return Str(ErrorList.ReceiveError)
        End If
    End Function

    Private Function LDMFPDataRead() As String
        tmpCmdMessage = "setldmcnf"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return Str(ErrorList.SendError)
        End If

        Dim retVal As String = PollingReceiveData(500)

        If InStr(1, retVal, ",") > 0 Then
            Return retVal
        Else
            Return Str(ErrorList.ReceiveError)
        End If
    End Function

    Private Sub btnFPWrite_Click(sender As Object, e As EventArgs) Handles btnFPWrite.Click, btnLDMFPWrite.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_FPWriteFunc()
            Case ModeList.MAXIMUM, ModeList.EFFECT, ModeList.LOW_PROD
                MAXIMUM_FPWriteFunc()
            Case ModeList.LDM
                LDM_FPWriteFunc()
            Case ModeList.ALTO
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Function FPWrtieStart() As Boolean
        FPReadStart()

        If txbFreq.Text.Replace(" ", "") = "" Or txbPower.Text.Replace(" ", "") = "" Then
            MsgBox("빈 칸이 존재합니다.", vbExclamation, FormName)
            Return True
        End If

        If Val(txbFreq.Text) < 6700 Or Val(txbFreq.Text) > 7200 Then
            MsgBox("주파수 범위에 어긋나는 값입니다.(6700 ~ 7200)", vbExclamation, FormName)
            Return True
        End If

        If CSng(Val(txbPower.Text)) < -0.5 Or CSng(Val(txbPower.Text)) > 0.5 Then
            MsgBox("파워 범위에 어긋나는 값입니다.(-0.5 ~ 0.5)", vbExclamation, FormName)
            Return True
        End If

        Return False

    End Function

    Private Sub DUALSONIC_FPWriteFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            FPWrtieStart()

            ''************************************
            ''*******   Cart 주파수 전송  ********
            ''************************************

            tmpCmdMessage = "cartfreq"
            cmdMessage = "[sb" & tmpCmdMessage & " " & txbFreq.Text & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''*******   Cart 파워 전송  ********
            ''************************************

            Dim tmpVal As Integer = (Val(txbPower.Text) * 100)
            tmpCmdMessage = "ctpower"
            cmdMessage = "[sb" & tmpCmdMessage & " " & tmpVal & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            txbFPBox.Text = "SUCCESS"
            txbFPBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbFPBox.Text = "ERROR" + Str(StageCnt)
        txbFPBox.BackColor = Color.Red
End_Finish:
        txbFPBox.Refresh()
    End Sub

    Private Sub MAXIMUM_FPWriteFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            FPWrtieStart()

            If SelectedModelInfo.HifuUseYn <> "Y" Then
                txbFPBox.Text = "해당안됨"
                txbFPBox.BackColor = Color.Green
                GoTo End_Finish
            End If

            Dim ResultData As Byte = FPWrtieSetting(Val(txbFreq.Text), CSng(txbPower.Text))

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            If ckbDBUse.Checked = True Then
                Dim TDNameStr = TDNameLoad()

                If TDNameStr = Str(ErrorList.SendError) Or TDNameStr = Str(ErrorList.ReceiveError) Then
                    MsgBox("TD 시리얼 번호를 확인할 수 없습니다. 저장하고자 하는 Serial 번호를 먼저 입력하여 주십시오.", vbExclamation, FormName)
                    StageCnt = TDNameStr
                    GoTo FAIL_Finish
                End If

                Dim DBExist As Boolean = TDNameDBLoad(TDNameStr)

                If DBExist = False Then
                    MsgBox("TD 시리얼 번호가 데이터베이스에 등록되어 있지 않습니다.", vbCritical, FormName)
                    StageCnt = ErrorList.ReceiveError
                    GoTo FAIL_Finish
                End If

                Dim freq = txbFreq.Text
                Dim power = txbPower.Text

                DBCmd = "UPDATE " & UseCartridgeTbl & " SET " &
                        "Ct_Frequency = " & freq & ", " &
                        "Ct_Power = " & power & " " &
                        "WHERE PV_SN = '" & TDNameStr & "'"

                DBCommand(DBCmd)
            End If

            txbFPBox.Text = "SUCCESS"
            txbFPBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbFPBox.Text = "ERROR" + Str(StageCnt)
        txbFPBox.BackColor = Color.Red
End_Finish:
        txbFPBox.Refresh()
    End Sub

    Private Sub LDM_FPWriteFunc() 'LDM 주파수 및 전력 쓰기 함수
        Dim stagecnt As Int16 = ErrorList.NonError
        Try
            If FPWrtieStart() Then
                stagecnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim resultdata As String = ldmfpwriteSetting(Val(txbFreq.Text), CSng(txbPower.Text), Val(txbFreq2.Text), CSng(txbPower2.Text), Val(txbFreq3.Text), CSng(txbPower3.Text))
            If resultdata = ErrorList.SendError Or resultdata = ErrorList.ReceiveError Then
                stagecnt = resultdata
                GoTo FAIL_Finish
            End If


            If ckbDBUse.Checked = True Then
                Dim TDNameStr = TDNameLoad()

                If TDNameStr = Str(ErrorList.SendError) Or TDNameStr = Str(ErrorList.ReceiveError) Then
                    MsgBox("LDM 시리얼 번호를 확인할 수 없습니다. 저장하고자 하는 Serial 번호를 먼저 입력하여 주십시오.", vbExclamation, FormName)
                    stagecnt = TDNameStr
                    GoTo FAIL_Finish
                End If

                Dim DBExist As Boolean = TDNameDBLoad(TDNameStr)

                If DBExist = False Then
                    MsgBox("LDM 시리얼 번호가 데이터베이스에 등록되어 있지 않습니다.", vbCritical, FormName)
                    stagecnt = ErrorList.ReceiveError
                    GoTo FAIL_Finish
                End If

                Dim freq = txbFreq.Text
                Dim power = txbPower.Text
                Dim freq2 = txbFreq2.Text
                Dim power2 = txbPower2.Text
                Dim freq3 = txbFreq3.Text
                Dim power3 = txbPower3.Text

                DBCmd = "UPDATE " & UseLDMCartridgeTbl & " SET " &
                        "Ct_Frequency = " & freq & ", " &
                        "Ct_Power = " & power & ", " &
                        "Ct_Frequency2 = " & freq2 & ", " &
                        "Ct_Power2 = " & power2 & ", " &
                        "Ct_Frequency3 = " & freq3 & ", " &
                        "Ct_Power3 = " & power3 & " " &
                        "WHERE PV_SN = '" & TDNameStr & "'"

                DBCommand(DBCmd)
            End If

            txbLDMFPBox.Text = "SUCCESS"
            txbLDMFPBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try


FAIL_Finish:
        txbLDMFPBox.Text = "ERROR" + Str(stagecnt)
        txbLDMFPBox.BackColor = Color.Red
End_Finish:
        txbLDMFPBox.Refresh()
    End Sub


    Private Function FPWrtieSetting(Frequency As Integer, Power As Single) As SByte
        Power = Power * 100

        tmpCmdMessage = "settdcnf"
        cmdMessage = "[" & tmpCmdMessage & " " & Frequency & "," & Power & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return Str(ErrorList.SendError)
        End If

        Dim retVal As String = PollingReceiveData(2500)

        If retVal <> Frequency & "," & Power Then
            Return ErrorList.ReceiveError
        Else
            Return ErrorList.NonError
        End If
    End Function

    Private Function ldmfpwriteSetting(Frequency1 As Integer, Power1 As Single, Frequency2 As Integer, Power2 As Single, Frequency3 As Integer, Power3 As Single) As SByte
        Power1 = Power1 * 100
        Power2 = Power2 * 100
        Power3 = Power3 * 100

        tmpCmdMessage = "setldmcnf"
        cmdMessage = "[" & tmpCmdMessage & " " & Frequency1 & "," & Frequency2 & "," & Frequency3 & "," & Power1 & "," & Power2 & "," & Power3 & "]" + vbCrLf
        If SendData(cmdMessage) Then
            Return Str(ErrorList.SendError)
        End If
        Dim retval As String = PollingReceiveData(2500)
        If retval <> Frequency1 & "," & Frequency2 & "," & Frequency3 & "," & Power1 & "," & Power2 & "," & Power3 Then
            Return ErrorList.ReceiveError
        Else
            Return ErrorList.NonError
        End If
    End Function



    Private Sub btnMtRead_Click(sender As Object, e As EventArgs) Handles btnMtRead.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_MtRead()
            Case ModeList.MAXIMUM, ModeList.EFFECT
                MAXIMUM_MtRead()
            Case ModeList.ALTO
                txbFPBox.Text = "ERROR"
                txbFPBox.BackColor = Color.Red
                txbFPBox.Refresh()
            Case ModeList.LDM, ModeList.LOW_PROD
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub MoterReadStart()
        txbMtBox.Visible = True
        txbMtBox.Text = "RUNNING"
        txbMtBox.BackColor = Color.GreenYellow
        txbMtBox.Refresh()
    End Sub

    Private Sub DUALSONIC_MtRead()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            MoterReadStart()

            ''************************************
            ''*******    Front 센서 수신    ******
            ''************************************
            tmpCmdMessage = "frontsen"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbMotor.Text = DataResult


            txbMtBox.Text = "SUCCESS"
            txbMtBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("출력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMtBox.Text = "ERROR" + Str(StageCnt)
        txbMtBox.BackColor = Color.Red
End_Finish:
        txbMtBox.Refresh()
    End Sub

    Private Sub MAXIMUM_MtRead()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            MoterReadStart()

            Dim ResultData As String = MoterRead()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            txbMotor.Text = Mid(ResultData, 1, InStr(1, ResultData, ",") - 1)

            txbMtBox.Text = "SUCCESS"
            txbMtBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMtBox.Text = "ERROR" + Str(StageCnt)
        txbMtBox.BackColor = Color.Red
End_Finish:
        txbMtBox.Refresh()
    End Sub

    Private Function MoterRead() As String
        tmpCmdMessage = "setmotor"
        cmdMessage = "[" & tmpCmdMessage & "]"

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If UBound(Split(retval, ",")) = 4 Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Sub btnMtWrite_Click(sender As Object, e As EventArgs) Handles btnMtWrite.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_MtWrite()
            Case ModeList.MAXIMUM, ModeList.EFFECT
                MAXIMUM_MtWrite()
            Case ModeList.ALTO
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Function MoterWrtieStart() As Boolean
        MoterReadStart()

        If txbMotor.Text.Replace(" ", "") = "" Then
            MsgBox("빈 칸이 존재합니다.", vbExclamation, FormName)
            Return True
        End If

        If Val(txbMotor.Text) > 5 Or Val(txbMotor.Text) < -5 Then
            MsgBox("모터 범위값을 벗어났습니다.", vbExclamation, FormName)
            Return True
        End If

        Return False
    End Function

    Private Sub DUALSONIC_MtWrite()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            If MoterWrtieStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''*******    txbFront 발신    ********
            ''************************************

            tmpCmdMessage = "frontsen"
            cmdMessage = "[sb" & tmpCmdMessage & " " & txbMotor.Text & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbMtBox.Text = "SUCCESS"
            txbMtBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMtBox.Text = "ERROR" + Str(StageCnt)
        txbMtBox.BackColor = Color.Red
End_Finish:
        txbMtBox.Refresh()
    End Sub

    Private Sub MAXIMUM_MtWrite()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            If MoterWrtieStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim ResultData As String = MoterSetting(Val(txbMotor.Text))

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            If ckbDBUse.Checked = True Then
                Dim TDNameStr = TDNameLoad()

                If TDNameStr = Str(ErrorList.SendError) Or TDNameStr = Str(ErrorList.ReceiveError) Then
                    MsgBox("TD 시리얼 번호를 확인할 수 없습니다. 저장하고자 하는 Serial 번호를 먼저 입력하여 주십시오.", vbExclamation, FormName)
                    StageCnt = TDNameStr
                    GoTo FAIL_Finish
                End If

                Dim DBExist As Boolean = TDNameDBLoad(TDNameStr)

                If DBExist = False Then
                    MsgBox("TD 시리얼 번호가 데이터베이스에 등록되어 있지 않습니다.", vbCritical, FormName)
                    StageCnt = ErrorList.ReceiveError
                    GoTo FAIL_Finish
                End If

                DBCmd = "UPDATE " & UseCartridgeTbl & " SET " &
                        "Ct_Motor_Lv = '" & txbMotor.Text & "' " &
                        "WHERE PV_SN = '" & TDNameStr & "'"

                DBCommand(DBCmd)
            End If

            txbMtBox.Text = "SUCCESS"
            txbMtBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMtBox.Text = "ERROR" + Str(StageCnt)
        txbMtBox.BackColor = Color.Red
End_Finish:
        txbMtBox.Refresh()
    End Sub

    Private Function MoterSetting(SetPulse As Integer) As String
        tmpCmdMessage = "setmotor"
        cmdMessage = "[" & tmpCmdMessage & " " & SetPulse & ",0,0,0,0]"

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If retval = SetPulse & ",0,0,0,0" Then
            Return ErrorList.NonError
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Sub btnShRead_Click(sender As Object, e As EventArgs) Handles btnShRead.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_ShotRead()
            Case ModeList.MAXIMUM, ModeList.EFFECT, ModeList.LDM, ModeList.LOW_PROD
                MAXIMUM_ShotRead()
            Case ModeList.ALTO
                txbFPBox.Text = "ERROR"
                txbFPBox.BackColor = Color.Red
                txbFPBox.Refresh()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub ShotReadStart()
        txbShotBox.Visible = True
        txbShotBox.Text = "RUNNING"
        txbShotBox.BackColor = Color.GreenYellow
        txbShotBox.Refresh()
    End Sub

    Private Sub DUALSONIC_ShotRead()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            ShotReadStart()

            ''************************************
            ''*******    Shot Cnt 수신    ********
            ''************************************

            tmpCmdMessage = "rshotcnt"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbShot.Text = DataResult

            ''************************************
            ''******  Max Shot Cnt 수신   ********
            ''************************************
            tmpCmdMessage = "tshotcnt"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            txbMaxShot.Text = DataResult

            txbShotBox.Text = "SUCCESS"
            txbShotBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbShotBox.Text = "ERROR" + Str(StageCnt)
        txbShotBox.BackColor = Color.Red
End_Finish:
        txbShotBox.Refresh()
    End Sub

    Private Sub MAXIMUM_ShotRead()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            ShotReadStart()

            If SelectedModelInfo.HifuUseYn <> "Y" And SelectedModelInfo.LdmUseYn <> "Y" Then
                txbShotBox.Text = "해당안됨"
                txbShotBox.BackColor = Color.Green
                GoTo End_Finish
            End If

            Dim ResultData As String = ShotReadFunc()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            txbShot.Text = ResultData

            txbShotBox.Text = "SUCCESS"
            txbShotBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbShotBox.Text = "ERROR" + Str(StageCnt)
        txbShotBox.BackColor = Color.Red
End_Finish:
        txbShotBox.Refresh()
    End Sub

    Private Function ShotReadFunc() As String
        tmpCmdMessage = "shotconf"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If IsNumeric(retval) Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Sub btnShWrite_Click(sender As Object, e As EventArgs) Handles btnShWrite.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_ShotWrite()
            Case ModeList.MAXIMUM, ModeList.EFFECT, ModeList.LDM, ModeList.LOW_PROD
                MAXIMUM_ShotWrtie()
            Case ModeList.ALTO
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Function ShotWriteStart() As Boolean
        ShotReadStart()

        If txbShot.Text.Replace(" ", "") = "" Or txbMaxShot.Text.Replace(" ", "") = "" Then
            MsgBox("빈 칸이 존재합니다.", vbExclamation, FormName)
            Return True
        End If

        Return False
    End Function

    Private Sub DUALSONIC_ShotWrite()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            ShotWriteStart()

            If Val(txbShot.Text) > Val(txbMaxShot.Text) Then
                MsgBox("현재 샷 수가 전체 샷 수보다 큽니다.", vbExclamation, FormName)
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''*******    Shot Cnt 발신    ********
            ''************************************

            tmpCmdMessage = "rshotcnt"
            cmdMessage = "[sb" & tmpCmdMessage & " " & txbShot.Text & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            ''************************************
            ''******  Max Shot Cnt 발신   ********
            ''************************************

            tmpCmdMessage = "tshotcnt"
            cmdMessage = "[sb" & tmpCmdMessage & " " & txbMaxShot.Text & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            DataResult = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            txbShotBox.Text = "SUCCESS"
            txbShotBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbShotBox.Text = "ERROR" + Str(StageCnt)
        txbShotBox.BackColor = Color.Red
End_Finish:
        txbShotBox.Refresh()
    End Sub

    Private Sub MAXIMUM_ShotWrtie()
        Dim StageCnt As Int16 = ErrorList.NonError
        Dim table As String

        If SelectedModelInfo.LdmUseYn = "Y" Then
            table = UseLDMCartridgeTbl
        Else
            table = UseCartridgeTbl
        End If

        Try
            ShotWriteStart()

            If SelectedModelInfo.HifuUseYn <> "Y" And SelectedModelInfo.LdmUseYn <> "Y" Then
                txbShotBox.Text = "해당안됨"
                txbShotBox.BackColor = Color.Green
                GoTo End_Finish
            End If

            Dim ResultData As String = ShotWriteFunc(Val(txbShot.Text))

            If ResultData = Str(ErrorList.SendError) Or ResultData = Str(ErrorList.ReceiveError) Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            If ckbDBUse.Checked = True Then
                Dim TDNameStr = TDNameLoad()

                If TDNameStr = Str(ErrorList.SendError) Or TDNameStr = Str(ErrorList.ReceiveError) Then
                    MsgBox("TD 시리얼 번호를 확인할 수 없습니다. 저장하고자 하는 Serial 번호를 먼저 입력하여 주십시오.", vbExclamation, FormName)
                    StageCnt = TDNameStr
                    GoTo FAIL_Finish
                End If

                Dim DBExist As Boolean = TDNameDBLoad(TDNameStr)

                If DBExist = False Then
                    MsgBox("TD 시리얼 번호가 데이터베이스에 등록되어 있지 않습니다.", vbCritical, FormName)
                    StageCnt = ErrorList.ReceiveError
                    GoTo FAIL_Finish
                End If

                DBCmd = "UPDATE " & table & " SET " &
                        "Ct_Remain_ShotCnt = '" & txbShot.Text & "' " &
                        "WHERE PV_SN = '" & TDNameStr & "'"

                DBCommand(DBCmd)
            End If

            txbShotBox.Text = "SUCCESS"
            txbShotBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbShotBox.Text = "ERROR" + Str(StageCnt)
        txbShotBox.BackColor = Color.Red
End_Finish:
        txbShotBox.Refresh()
    End Sub

    Private Function ShotWriteFunc(ShotData As Integer) As String
        tmpCmdMessage = "shotconf"
        cmdMessage = "[" & tmpCmdMessage & " " & ShotData & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If retval = ShotData Then
            Return ErrorList.NonError
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Sub btnDvAuth_Click(sender As Object, e As EventArgs) Handles btnDvAuth.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_DevAuth()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub DevAuthStart()
        txbDvAuthBox.Visible = True
        txbDvAuthBox.Text = "RUNNING"
        txbDvAuthBox.BackColor = Color.GreenYellow
        txbDvAuthBox.Refresh()
    End Sub

    Private Sub DUALSONIC_DevAuth()
        Dim StageCnt As Int16 = ErrorList.NonError
        Try
            DevAuthStart()

            ''************************************
            ''*******   본체 인증 발신    ********
            ''************************************

            tmpCmdMessage = "mainauth"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(10000)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            txbDvAuthBox.Text = "SUCCESS"
            txbDvAuthBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbDvAuthBox.Text = "ERROR" + Str(StageCnt)
        txbDvAuthBox.BackColor = Color.Red
End_Finish:
        txbDvAuthBox.Refresh()
    End Sub

    Private Sub btnDvRead_Click(sender As Object, e As EventArgs) Handles btnDvRead.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_DevRead()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub DevReadStart()
        txbDvInfoBox.Visible = True
        txbDvInfoBox.Text = "RUNNING"
        txbDvInfoBox.BackColor = Color.GreenYellow
        txbDvInfoBox.Refresh()
    End Sub

    Private Sub DUALSONIC_DevRead()
        Dim StageCnt As Int16 = ErrorList.NonError
        Try
            DevReadStart()

            ''************************************
            ''*******    제조사 코드 수신  *******
            ''************************************

            tmpCmdMessage = "rdstconf"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If UBound(Split(DataResult, ",")) <> 4 Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            txbDvCode.Text = Mid(DataResult, 1, InStr(DataResult, ",") - 1)
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)
            Dim BackCode As String = Mid(DataResult, 1, InStr(DataResult, ",") - 1)
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)
            Dim FrontCode As String = Mid(DataResult, 1, InStr(DataResult, ",") - 1)
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)
            txbDvAbroad.Text = Mid(DataResult, 1, InStr(DataResult, ",") - 1)
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)
            txbDvAgency.Text = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)

            nudDvYear.Text = Mid(BackCode, 1, 2)
            nudDvMonth.Text = Mid(BackCode, 3, 2)

            Dim front As String = BackCode.Substring(0, 4)
            Dim middle As String = BackCode.Substring(4, 3)
            Dim last As String = BackCode.Substring(7, 3)

            Dim swapped As String = front & last & middle

            txbDvNumber.Text = FrontCode & swapped
            nudDvSerial.Text = Mid(BackCode, 5, 6)

            txbDvInfoBox.Text = "SUCCESS"
            txbDvInfoBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbDvInfoBox.Text = "ERROR" + Str(StageCnt)
        txbDvInfoBox.BackColor = Color.Red
End_Finish:
        txbDvInfoBox.Refresh()
    End Sub

    Private Sub btnDvWrite_Click(sender As Object, e As EventArgs) Handles btnDvWrite.Click
        Select Case SelectModeType
            Case ModeList.DUALSONIC
                DUALSONIC_DevWrite()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Function DevWriteStart() As Boolean
        DevReadStart()

        If txbDvCode.Text = String.Empty Then
            txbDvCode.Text = "jomt"
        End If

        If txbDvCode.Text.Replace(" ", "") = "" Or nudDvYear.Text.Replace(" ", "") = "" Or nudDvMonth.Text.Replace(" ", "") = "" Or nudDvSerial.Text.Replace(" ", "") = "" Or txbDvAbroad.Text.Replace(" ", "") = "" Or txbDvAgency.Text.Replace(" ", "") = "" Then
            MsgBox("빈 칸이 존재합니다.", vbExclamation, FormName)
            Return True
        End If

        Return False
    End Function

    Private Sub DUALSONIC_DevWrite()
        Dim StageCnt As Int16 = ErrorList.NonError
        Const TypeNum As Char = "0"
        Try

            If DevWriteStart() Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If
            ''************************************
            ''*******   제조사코드 발신    *******
            ''************************************

            Dim MonthParsing As String = MonthSetting(nudDvMonth.Value.ToString)
            Dim SerialNum As String = nudDvYear.Value & MonthParsing & SerialNumSetting(nudDvSerial.Value)
            Dim ModelNum As String = SelectedModelInfo.Code & TypeNum

            tmpCmdMessage = "wrstconf"
            cmdMessage = "[sb" & tmpCmdMessage & " " & txbDvCode.Text & "," & SerialNum & "," & ModelNum & "," & txbDvAbroad.Text & "," & txbDvAgency.Text & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            txbDvInfoBox.Text = "SUCCESS"
            txbDvInfoBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbDvInfoBox.Text = "ERROR" + Str(StageCnt)
        txbDvInfoBox.BackColor = Color.Red
End_Finish:
        txbDvInfoBox.Refresh()
    End Sub

    Private Sub btnDuReady_Click(sender As Object, e As EventArgs) Handles btnDuReady.Click
        Dim StageCnt As Int16 = ErrorList.NonError


        Try
            DuReadyStart()

            ''************************************
            ''*******  기기 사용 정보 수신 *******
            ''************************************

            tmpCmdMessage = "rdstinfo"
            cmdMessage = "[sb" & tmpCmdMessage & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If UBound(Split(DataResult, ",")) <> 4 Then
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

            Dim TimeCnt As ULong = Val(Mid(DataResult, 1, InStr(DataResult, ",") - 1))
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)

            TimeCnt = TimeCnt / OneSecond

            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbDuFullDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbDuFullHour.Text = "0" & HourCnt
            Else
                txbDuFullHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbDuFullMin.Text = "0" & MinCnt
            Else
                txbDuFullMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbDuFullSec.Text = "0" & SecCnt
            Else
                txbDuFullSec.Text = SecCnt
            End If

            TimeCnt = Val(Mid(DataResult, 1, InStr(DataResult, ",") - 1))
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)

            TimeCnt = TimeCnt / OneSecond

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            txbDuReadyDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbDuReadyHour.Text = "0" & HourCnt
            Else
                txbDuReadyHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbDuReadyMin.Text = "0" & MinCnt
            Else
                txbDuReadyMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbDuReadySec.Text = "0" & SecCnt
            Else
                txbDuReadySec.Text = SecCnt
            End If

            txbDuFace.Text = Val(Mid(DataResult, 1, InStr(DataResult, ",") - 1))
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)

            txbDuEye.Text = Val(Mid(DataResult, 1, InStr(DataResult, ",") - 1))
            DataResult = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)

            txbDuError.Text = Mid(DataResult, InStr(DataResult, ",") + 1, DataResult.Length)


            txbDuUseBox.Text = "SUCCESS"
            txbDuUseBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbDuUseBox.Text = "ERROR" + Str(StageCnt)
        txbDuUseBox.BackColor = Color.Red
End_Finish:
        txbDuUseBox.Refresh()
    End Sub

    Private Sub DuReadyStart()
        txbDuUseBox.Visible = True
        txbDuUseBox.Text = "RUNNING"
        txbDuUseBox.BackColor = Color.GreenYellow
        txbDuUseBox.Refresh()
    End Sub

    Private Sub btnDuRest_Click(sender As Object, e As EventArgs) Handles btnDuReset.Click
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            DuReadyStart()

            If MsgBox("한 번 초기화된 사용 기록은 되돌릴 수 없습니다." & vbCrLf & "정말 초기화 하시겠습니까?", vbCritical + vbOKCancel, FormName) = MsgBoxResult.Ok Then

                tmpCmdMessage = "wrstinfo"
                cmdMessage = "[sb" & tmpCmdMessage & " 0,0,0,0,0]"

                If SendData(cmdMessage) Then
                    StageCnt = ErrorList.SendError
                    GoTo FAIL_Finish
                End If

                Dim DataResult As String = PollingReceiveData(1500)

                If DataResult = "False" Then
                    StageCnt = ErrorList.ReceiveError
                    GoTo FAIL_Finish
                End If

                If DataResult <> "success" Then
                    StageCnt = ErrorList.ResultError
                    GoTo FAIL_Finish
                End If

                txbDuUseBox.Text = "SUCCESS"
                txbDuUseBox.BackColor = Color.Green
                GoTo End_Finish

            Else
                GoTo FAIL_Finish
            End If

            txbDuUseBox.Text = "SUCCESS"
            txbDuUseBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbDuUseBox.Text = "ERROR" + Str(StageCnt)
        txbDuUseBox.BackColor = Color.Red
End_Finish:
        txbDuUseBox.Refresh()
    End Sub

    Private Sub btnDuWrite_Click(sender As Object, e As EventArgs) Handles btnDuWrite.Click
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            DuReadyStart()
            Dim writeData As String = ParseDualsonicWriteData()

            tmpCmdMessage = "wrstinfo"
            cmdMessage = "[sb" & tmpCmdMessage & " " & writeData & "]"

            If SendData(cmdMessage) Then
                StageCnt = ErrorList.SendError
                GoTo FAIL_Finish
            End If

            Dim DataResult As String = PollingReceiveData(1500)

            If DataResult = "False" Then
                StageCnt = ErrorList.ReceiveError
                GoTo FAIL_Finish
            End If

            If DataResult <> "success" Then
                StageCnt = ErrorList.ResultError
                GoTo FAIL_Finish
            End If

            txbDuUseBox.Text = "SUCCESS"
            txbDuUseBox.BackColor = Color.Green
            GoTo End_Finish

            GoTo FAIL_Finish

            txbDuUseBox.Text = "SUCCESS"
            txbDuUseBox.BackColor = Color.Green

            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbDuUseBox.Text = "Error" + Str(StageCnt)
        txbDuUseBox.BackColor = Color.Red
End_Finish:
        txbDuUseBox.Refresh()
    End Sub

    Private Function ParseDualsonicWriteData() As String
        Dim TimeCnt1 As ULong
        Dim TimeCnt2 As ULong

        Dim FullDay As Integer = Val(txbDuFullDay.Text)
        Dim FullHour As Integer = Val(txbDuFullHour.Text)
        Dim FullMin As Integer = Val(txbDuFullMin.Text)
        Dim FullSec As Integer = Val(txbDuFullSec.Text)

        TimeCnt1 = (CLng(FullDay) * OneDay) + (CLng(FullHour) * OneHour) + (CLng(FullMin) * OneMinute) + CLng(FullSec)
        TimeCnt1 *= OneSecond ' 초 단위가 아니라 밀리초 단위로 저장하고 있었다면 여기서 다시 변환

        Dim ReadyDay As Integer = Val(txbDuReadyDay.Text)
        Dim ReadyHour As Integer = Val(txbDuReadyHour.Text)
        Dim ReadyMin As Integer = Val(txbDuReadyMin.Text)
        Dim ReadySec As Integer = Val(txbDuReadySec.Text)

        TimeCnt2 = (CLng(ReadyDay) * OneDay) + (CLng(ReadyHour) * OneHour) + (CLng(ReadyMin) * OneMinute) + CLng(ReadySec)
        TimeCnt2 *= OneSecond

        Dim FaceCnt As String = txbDuFace.Text.Trim()
        Dim EyeCnt As String = txbDuEye.Text.Trim()
        Dim ErrorStr As String = txbDuError.Text.Trim()

        Return TimeCnt1.ToString() & "," & TimeCnt2.ToString() & "," & FaceCnt & "," & EyeCnt & "," & ErrorStr
    End Function

    Private Sub MAXIMUM_All_ReadStart()
        txbMxAllBox.Visible = True
        txbMxAllBox.Text = "RUNNING"
        txbMxAllBox.BackColor = Color.GreenYellow
        txbMxAllBox.Refresh()
    End Sub

    Private Sub btnMxAllRead_Click(sender As Object, e As EventArgs) Handles btnMxAllRead.Click
        Select Case SelectModeType
            Case ModeList.MAXIMUM
                MAXIMUM_UseReadFunc()
            Case ModeList.LOW_PROD, ModeList.LDM
                lowProd_UseReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub MAXIMUM_UseReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            MAXIMUM_All_ReadStart()

            Dim ResultData As String = RunTimeSearch()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim TimeCnt As ULong = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond

            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbMxAllDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbMxAllHour.Text = "0" & HourCnt
            Else
                txbMxAllHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbMxAllMin.Text = "0" & MinCnt
            Else
                txbMxAllMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbMxAllSec.Text = "0" & SecCnt
            Else
                txbMxAllSec.Text = SecCnt
            End If

            ResultData = ShotErrSearch()

            txbMxAllShot.Text = Mid(ResultData, 1, InStr(ResultData, ",") - 1)
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)
            txbMxAllErr.Text = Mid(ResultData, 1, InStr(ResultData, ",") - 1)

            txbMxAllBox.Text = "SUCCESS"
            txbMxAllBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMxAllBox.Text = "Error" + Str(StageCnt)
        txbMxAllBox.BackColor = Color.Red
End_Finish:
        txbMxAllBox.Refresh()
    End Sub

    Private Sub lowProd_UseReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            MAXIMUM_All_ReadStart()

            Dim ResultData As String = RunTimeWithShotCount() ''남은 샷수 & 전체 사용시간

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim TimeCnt As ULong = Val(Mid(ResultData, 2, InStr(ResultData, ",") + 1)) / OneSecond

            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbMxAllDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbMxAllHour.Text = "0" & HourCnt
            Else
                txbMxAllHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbMxAllMin.Text = "0" & MinCnt
            Else
                txbMxAllMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbMxAllSec.Text = "0" & SecCnt
            Else
                txbMxAllSec.Text = SecCnt
            End If


            txbMxAllShot.Text = Mid(ResultData, 1, InStr(ResultData, ",") - 1)
            lowPrdCmdFlag = True
            ResultData = ShotErrSearch()
            If ResultData.Equals("timeout") Then
                txbMxAllErr.Clear()
            Else
                ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

                txbMxAllErr.Text = Mid(ResultData, 1, InStr(ResultData, ",") - 1)
            End If

            txbMxAllBox.Text = "SUCCESS"
            txbMxAllBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMxAllBox.Text = "Error" + Str(StageCnt)
        txbMxAllBox.BackColor = Color.Red
End_Finish:
        txbMxAllBox.Refresh()
    End Sub

    Private Function RunTimeSearch() As String
        tmpCmdMessage = "rdsinfo4"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If UBound(Split(retval, ",")) = 6 Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Function RunTimeWithShotCount() As String '보급형 사용시간
        tmpCmdMessage = "rdsinfo1"

        cmdMessage = "[" & tmpCmdMessage & "]"

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If UBound(Split(retval, ",")) = 1 Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Function ShotErrSearch() As String
        tmpCmdMessage = "rdsinfo5"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If UBound(Split(retval, ",")) = 3 Then
            Return retval
        ElseIf retval.Equals("timeout") Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Function ALTOFESearch() As String
        tmpCmdMessage = "rdsinfo7"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If UBound(Split(retval, ",")) = 4 Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Function ALTOLBSearch() As String
        tmpCmdMessage = "rdsinfo8"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If UBound(Split(retval, ",")) = 4 Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Function RunTimeWrtie(SystemTime As ULong, HIFUTime As ULong, FHIFUTime As ULong, EHIFUTime As ULong, RFTime As ULong, MCTime As ULong, EPTime As ULong) As String
        tmpCmdMessage = "wrsinfo4"
        cmdMessage = "[" & tmpCmdMessage & " " & SystemTime & "," & HIFUTime & "," & FHIFUTime & "," & EHIFUTime & "," & RFTime & "," & MCTime & "," & EPTime & "]" + vbCrLf

        If SendData(cmdMessage) Then
            Return ErrorList.SendError
        End If

        Dim retval As String = PollingReceiveData(1000)

        If UBound(Split(retval, ",")) = 4 Then
            Return retval
        Else
            Return ErrorList.ReceiveError
        End If
    End Function

    Private Sub btnMxAllReset_Click(sender As Object, e As EventArgs) Handles btnMxAllReset.Click, btnMxHfReset.Click, btnMxEtReset.Click
        Select Case SelectModeType
            Case ModeList.MAXIMUM
                MAXIMUM_UseResetFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub MAXIMUM_UseResetFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            MAXIMUM_All_ReadStart()

            If MsgBox("전체 항목에 대해 초기화 진행하시겠습니까?" & vbCrLf & "초기화된 데이터는 복구가 불가능 합니다.", vbCritical + vbOKCancel, FormName) = MsgBoxResult.Ok Then

                Dim ResultData As String = RunTimeWrtie(0, 0, 0, 0, 0, 0, 0)

                If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                    StageCnt = ResultData
                    GoTo FAIL_Finish
                End If

                txbMxAllBox.Text = "SUCCESS"
                txbMxAllBox.BackColor = Color.Green
                GoTo End_Finish

            Else
                StageCnt = ErrorList.DataError
                GoTo FAIL_Finish
            End If

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMxAllBox.Text = "Error" + Str(StageCnt)
        txbMxAllBox.BackColor = Color.Red
End_Finish:
        txbMxAllBox.Refresh()
    End Sub

    Private Sub btnMxHfRead_Click(sender As Object, e As EventArgs) Handles btnMxHfRead.Click
        Select Case SelectModeType
            Case ModeList.MAXIMUM
                MAXIMUM_HF_UseReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub MAXIMUM_HF_ReadStart()
        txbMxHfBox.Visible = True
        txbMxHfBox.Text = "RUNNING"
        txbMxHfBox.BackColor = Color.GreenYellow
        txbMxHfBox.Refresh()
    End Sub

    Private Sub MAXIMUM_HF_UseReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            MAXIMUM_HF_ReadStart()

            Dim ResultData As String = RunTimeSearch()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

            Dim TimeCnt As ULong = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbMxFcDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbMxFcHour.Text = "0" & HourCnt
            Else
                txbMxFcHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbMxFcMin.Text = "0" & MinCnt
            Else
                txbMxFcMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbMxFcSec.Text = "0" & SecCnt
            Else
                txbMxFcSec.Text = SecCnt
            End If

            TimeCnt = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            txbMxEyDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbMxEyHour.Text = "0" & HourCnt
            Else
                txbMxEyHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbMxEyMin.Text = "0" & MinCnt
            Else
                txbMxEyMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbMxEySec.Text = "0" & SecCnt
            Else
                txbMxEySec.Text = SecCnt
            End If

            ResultData = ShotErrSearch()

            txbMxFcShot.Text = Mid(ResultData, 1, InStr(ResultData, ",") - 1)

            txbMxHfBox.Text = "SUCCESS"
            txbMxHfBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMxHfBox.Text = "Error" + Str(StageCnt)
        txbMxHfBox.BackColor = Color.Red
End_Finish:
        txbMxHfBox.Refresh()
    End Sub

    Private Sub MAXIMUM_ET_ReadStart()
        txbMxEtBox.Visible = True
        txbMxEtBox.Text = "RUNNING"
        txbMxEtBox.BackColor = Color.GreenYellow
        txbMxEtBox.Refresh()
    End Sub

    Private Sub btnMxEtRead_Click(sender As Object, e As EventArgs) Handles btnMxEtRead.Click
        Select Case SelectModeType
            Case ModeList.MAXIMUM
                MAXIMUM_ET_UseReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub MAXIMUM_ET_UseReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            MAXIMUM_ET_ReadStart()

            Dim ResultData As String = RunTimeSearch()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

            Dim TimeCnt As ULong = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbMxTuDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbMxTuHour.Text = "0" & HourCnt
            Else
                txbMxTuHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbMxTuMin.Text = "0" & MinCnt
            Else
                txbMxTuMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbMxTuSec.Text = "0" & SecCnt
            Else
                txbMxTuSec.Text = SecCnt
            End If

            TimeCnt = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            txbMxDuDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbMxDuHour.Text = "0" & HourCnt
            Else
                txbMxDuHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbMxDuMin.Text = "0" & MinCnt
            Else
                txbMxDuMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbMxDuSec.Text = "0" & SecCnt
            Else
                txbMxDuSec.Text = SecCnt
            End If

            TimeCnt = Val(ResultData) / OneSecond

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            txbMxCuDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbMxCuHour.Text = "0" & HourCnt
            Else
                txbMxCuHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbMxCuMin.Text = "0" & MinCnt
            Else
                txbMxCuMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbMxCuSec.Text = "0" & SecCnt
            Else
                txbMxCuSec.Text = SecCnt
            End If

            txbMxEtBox.Text = "SUCCESS"
            txbMxEtBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbMxEtBox.Text = "Error" + Str(StageCnt)
        txbMxEtBox.BackColor = Color.Red
End_Finish:
        txbMxEtBox.Refresh()
    End Sub

    Private Sub btnALAllRead_Click(sender As Object, e As EventArgs) Handles btnALAllRead.Click
        Select Case SelectModeType
            Case ModeList.ALTO
                ALTO_ALL_UseReadFunc()
            Case Else
                MsgBox("잘못된 경로입니다.", vbCritical, FormName)
        End Select
    End Sub

    Private Sub ALTO_ALL_ReadStart()
        txbALAllBox.Visible = True
        txbALAllBox.Text = "RUNNING"
        txbALAllBox.BackColor = Color.GreenYellow
        txbALAllBox.Refresh()
    End Sub

    Private Sub ALTO_ALL_UseReadFunc()
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            ALTO_ALL_ReadStart()

            Dim ResultData As String = RunTimeSearch()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim TimeCnt As ULong = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond
            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbALAllDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbALAllHour.Text = "0" & HourCnt
            Else
                txbALAllHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbALAllMin.Text = "0" & MinCnt
            Else
                txbALAllMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbALAllSec.Text = "0" & SecCnt
            Else
                txbALAllSec.Text = SecCnt
            End If

            TimeCnt = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            ResultData = ShotErrSearch()
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

            txbALAllErr.Text = Mid(ResultData, 1, InStr(ResultData, ",") - 1)

            txbALAllBox.Text = "SUCCESS"
            txbALAllBox.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbALAllBox.Text = "Error" + Str(StageCnt)
        txbALAllBox.BackColor = Color.Red
End_Finish:
        txbALAllBox.Refresh()
    End Sub

    Private Sub ALTO_Func1_ReadStart()
        txbALFuncBox1.Visible = True
        txbALFuncBox1.Text = "RUNNING"
        txbALFuncBox1.BackColor = Color.GreenYellow
        txbALFuncBox1.Refresh()
    End Sub

    Private Sub btnALFuncRead1_Click(sender As Object, e As EventArgs) Handles btnALFuncRead1.Click
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            ALTO_Func1_ReadStart()

            Dim ResultData As String = ALTOFESearch()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim TimeCnt As ULong = Val(Mid(ResultData, InStrRev(ResultData, ",") + 1, ResultData.Length)) / OneSecond
            ResultData = Mid(ResultData, 1, InStrRev(ResultData, ",") - 1)

            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbALEtDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbALEtHour.Text = "0" & HourCnt
            Else
                txbALEtHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbALEtMin.Text = "0" & MinCnt
            Else
                txbALEtMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbALEtSec.Text = "0" & SecCnt
            Else
                txbALEtSec.Text = SecCnt
            End If

            TimeCnt = Val(Mid(ResultData, InStrRev(ResultData, ",") + 1, ResultData.Length)) / OneSecond

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            txbALFtDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbALFtHour.Text = "0" & HourCnt
            Else
                txbALFtHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbALFtMin.Text = "0" & MinCnt
            Else
                txbALFtMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbALFtSec.Text = "0" & SecCnt
            Else
                txbALFtSec.Text = SecCnt
            End If

            ResultData = RunTimeSearch()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            TimeCnt = Val(Mid(ResultData, InStrRev(ResultData, ",") + 1, ResultData.Length)) / OneSecond

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            txbALCsDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbALCsHour.Text = "0" & HourCnt
            Else
                txbALCsHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbALCsMin.Text = "0" & MinCnt
            Else
                txbALCsMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbALCsSec.Text = "0" & SecCnt
            Else
                txbALCsSec.Text = SecCnt
            End If

            txbALFuncBox1.Text = "SUCCESS"
            txbALFuncBox1.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbALFuncBox1.Text = "Error" + Str(StageCnt)
        txbALFuncBox1.BackColor = Color.Red
End_Finish:
        txbALFuncBox1.Refresh()
    End Sub

    Private Sub ALTO_Func2_ReadStart()
        txbALFuncBox2.Visible = True
        txbALFuncBox2.Text = "RUNNING"
        txbALFuncBox2.BackColor = Color.GreenYellow
        txbALFuncBox2.Refresh()
    End Sub

    Private Sub btnALFuncRead2_Click(sender As Object, e As EventArgs) Handles btnALFuncRead2.Click
        Dim StageCnt As Int16 = ErrorList.NonError

        Try
            ALTO_Func2_ReadStart()

            Dim ResultData As String = ALTOLBSearch()

            If ResultData = ErrorList.SendError Or ResultData = ErrorList.ReceiveError Then
                StageCnt = ResultData
                GoTo FAIL_Finish
            End If

            Dim TimeCnt As ULong = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond
            ResultData = Mid(ResultData, InStr(ResultData, ",") + 1, ResultData.Length)

            Dim DayCnt As Integer = Truncate(TimeCnt / OneDay)
            Dim HourCnt As Int16 = Truncate((TimeCnt Mod OneDay) / OneHour)
            Dim MinCnt As Int16 = Truncate((TimeCnt Mod OneHour) / OneMinute)
            Dim SecCnt As Int16 = Truncate(TimeCnt Mod OneMinute)

            txbALLtDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbALLtHour.Text = "0" & HourCnt
            Else
                txbALLtHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbALLtMin.Text = "0" & MinCnt
            Else
                txbALLtMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbALLtSec.Text = "0" & SecCnt
            Else
                txbALLtSec.Text = SecCnt
            End If

            TimeCnt = Val(Mid(ResultData, 1, InStr(ResultData, ",") - 1)) / OneSecond

            DayCnt = Truncate(TimeCnt / OneDay)
            HourCnt = Truncate((TimeCnt Mod OneDay) / OneHour)
            MinCnt = Truncate((TimeCnt Mod OneHour) / OneMinute)
            SecCnt = Truncate(TimeCnt Mod OneMinute)

            txbALBtDay.Text = DayCnt

            If HourCnt < StrLen Then
                txbALBtHour.Text = "0" & HourCnt
            Else
                txbALBtHour.Text = HourCnt
            End If

            If MinCnt < StrLen Then
                txbALBtMin.Text = "0" & MinCnt
            Else
                txbALBtMin.Text = MinCnt
            End If

            If SecCnt < StrLen Then
                txbALBtSec.Text = "0" & SecCnt
            Else
                txbALBtSec.Text = SecCnt
            End If

            txbALFuncBox2.Text = "SUCCESS"
            txbALFuncBox2.BackColor = Color.Green
            GoTo End_Finish

        Catch ex As Exception
            MsgBox("입력 진행 도중 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            GoTo FAIL_Finish
        End Try

FAIL_Finish:
        txbALFuncBox2.Text = "Error" + Str(StageCnt)
        txbALFuncBox2.BackColor = Color.Red
End_Finish:
        txbALFuncBox2.Refresh()
    End Sub

    Private Sub ResultReset()
        txbAuthBox.Text = String.Empty
        txbAuthBox.BackColor = Color.FromArgb(0, 192, 0)
        txbAuthBox.Visible = False

        txbInfoBox.Text = String.Empty
        txbInfoBox.BackColor = Color.FromArgb(0, 192, 0)
        txbInfoBox.Visible = False

        txbAreaBox.Text = String.Empty
        txbAreaBox.BackColor = Color.FromArgb(0, 192, 0)
        txbAreaBox.Visible = False


        txbTDBox.Text = String.Empty
        txbTDBox.BackColor = Color.FromArgb(0, 192, 0)
        txbTDBox.Visible = False

        txbFPBox.Text = String.Empty
        txbFPBox.BackColor = Color.FromArgb(0, 192, 0)
        txbFPBox.Visible = False

        txbLDMFPBox.Text = String.Empty
        txbLDMFPBox.BackColor = Color.FromArgb(0, 192, 0)
        txbLDMFPBox.Visible = False

        txbMtBox.Text = String.Empty
        txbMtBox.BackColor = Color.FromArgb(0, 192, 0)
        txbMtBox.Visible = False

        txbShotBox.Text = String.Empty
        txbShotBox.BackColor = Color.FromArgb(0, 192, 0)
        txbShotBox.Visible = False

        txbDvAuthBox.Text = String.Empty
        txbDvAuthBox.BackColor = Color.FromArgb(0, 192, 0)
        txbDvAuthBox.Visible = False

        txbDvInfoBox.Text = String.Empty
        txbDvInfoBox.BackColor = Color.FromArgb(0, 192, 0)
        txbDvInfoBox.Visible = False

        txbALFuncBox1.Text = String.Empty
        txbALFuncBox1.BackColor = Color.FromArgb(0, 192, 0)
        txbALFuncBox1.Visible = False

        txbALFuncBox2.Text = String.Empty
        txbALFuncBox2.BackColor = Color.FromArgb(0, 192, 0)
        txbALFuncBox2.Visible = False

        txbALAllBox.Text = String.Empty
        txbALAllBox.BackColor = Color.FromArgb(0, 192, 0)
        txbALAllBox.Visible = False

        txbMxAllBox.Text = String.Empty
        txbMxAllBox.BackColor = Color.FromArgb(0, 192, 0)
        txbMxAllBox.Visible = False

        txbMxHfBox.Text = String.Empty
        txbMxHfBox.BackColor = Color.FromArgb(0, 192, 0)
        txbMxHfBox.Visible = False

        txbMxEtBox.Text = String.Empty
        txbMxEtBox.BackColor = Color.FromArgb(0, 192, 0)
        txbMxEtBox.Visible = False

        txbFwBox.Text = String.Empty
        txbFwBox.BackColor = Color.FromArgb(0, 192, 0)
        txbFwBox.Visible = False
    End Sub

    ''' <summary>MAX 사용 탭 선택 시 해당 탭의 결과 txb 초기화 및 숨김</summary>
    Private Sub tcMAXUse_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tcMAXUse.SelectedIndexChanged
        Select Case tcMAXUse.SelectedIndex
            Case 0
                txbMxAllBox.Text = String.Empty
                txbMxAllBox.BackColor = Color.FromArgb(0, 192, 0)
                txbMxAllBox.Visible = False
            Case 1
                txbMxHfBox.Text = String.Empty
                txbMxHfBox.BackColor = Color.FromArgb(0, 192, 0)
                txbMxHfBox.Visible = False
            Case 2
                txbMxEtBox.Text = String.Empty
                txbMxEtBox.BackColor = Color.FromArgb(0, 192, 0)
                txbMxEtBox.Visible = False
        End Select
    End Sub

    Private Sub btnEnd_Click(sender As Object, e As EventArgs) Handles btnEnd.Click
        End
    End Sub

    Private Sub txbLoadNum_KeyDown(sender As Object, e As KeyEventArgs) Handles txbLoadNum.KeyDown
        If e.KeyCode = Keys.Return Then
            btnLoadNum_Click(sender, New System.EventArgs)
        End If
    End Sub

    Private Const TDLength As Byte = 10
    Private Const OldProdLength As Byte = 12
    Private Const NewProdLength As Byte = 16

    ''DataType 이 True 일 경우 TD 데이터 / False 일 경우 제품 데이터
    Private DataType As Boolean = False

    Private Sub btnLoadNum_Click(sender As Object, e As EventArgs) Handles btnLoadNum.Click
        If String.IsNullOrWhiteSpace(txbLoadNum.Text) Then
            MsgBox("입력된 정보가 존재하지 않습니다. 검색하고자 하는 번호를 입력하여 주십시오.", vbExclamation)
        Else
            If txbLoadNum.Text.Length = TDLength Then
                DataType = True
                Dim result = TD_Search()

                If result <> True Then
                    NonTD_Search()
                End If
            ElseIf txbLoadNum.Text.Length = OldProdLength Or txbLoadNum.Text.Length = NewProdLength Then
                DataType = False
                Dim result = Product_Search()

                If result <> True Then
                    NonTDProduct_Search()
                End If
            Else
                MsgBox("TD 및 제품 번호 양식에 맞지 않습니다.", vbExclamation)
            End If
        End If
    End Sub

    Private Function TD_Search() As Boolean
        Dim DBCmd As String
        If SelectedModelInfo.LdmUseYn = "Y" Then
            DBCmd = "Select Product_SN,Ct_Manufacture_Name,Ct_SN,Ct_Model,Ct_Type,Ct_Abroad,Ct_Agency,Ct_Power,Ct_Power2,Ct_Power3,Ct_Max_ShotCnt,Ct_Motor_Lv,PV_SN,Ct_Frequency,Ct_Frequency2,Ct_Frequency3,IFNULL(Manufacture_Date,'') as Manufacture_Date FROM " & UseLDMCartridgeTbl & " WHERE PV_SN = '" & txbLoadNum.Text & "' Limit 1"
            If DBQueryReader(DBCmd) Then
                txbPDSN.Text = sqlDataQuery(0)
                txbPDCP.Text = sqlDataQuery(1)
                txbPDSerial.Text = sqlDataQuery(2)
                txbPDModel.Text = sqlDataQuery(3)
                txbPDType.Text = sqlDataQuery(4)
                txbPDCC.Text = sqlDataQuery(5)
                txbPDAC.Text = sqlDataQuery(6)
                txbMesPw.Text = sqlDataQuery(7)
                txbMesPw2.Text = sqlDataQuery(8)
                txbMesPw3.Text = sqlDataQuery(9)

                txbPDSC.Text = sqlDataQuery(10)
                txbPDMotor.Text = sqlDataQuery(11)
                txbTDSN.Text = sqlDataQuery(12)

                txbMesFreq.Text = sqlDataQuery(13)
                txbMesFreq2.Text = sqlDataQuery(14)
                txbMesFreq3.Text = sqlDataQuery(15)
                txbMesDate.Text = sqlDataQuery(16)

                Return True
            Else
                Return False
            End If
        Else
            DBCmd = "Select Product_SN,Ct_Manufacture_Name,Ct_SN,Ct_Model,Ct_Type,Ct_Abroad,Ct_Agency,Ct_Power,Ct_Max_ShotCnt,Ct_Motor_Lv,PV_SN,Ct_Frequency,IFNULL(Manufacture_Date,'') as Manufacture_Date FROM " & UseCartridgeTbl & " WHERE PV_SN = '" & txbLoadNum.Text & "' Limit 1"
            If DBQueryReader(DBCmd) Then
                txbPDSN.Text = sqlDataQuery(0)
                txbPDCP.Text = sqlDataQuery(1)
                txbPDSerial.Text = sqlDataQuery(2)
                txbPDModel.Text = sqlDataQuery(3)
                txbPDType.Text = sqlDataQuery(4)
                txbPDCC.Text = sqlDataQuery(5)
                txbPDAC.Text = sqlDataQuery(6)
                txbMesPw.Text = sqlDataQuery(7)
                txbPDSC.Text = sqlDataQuery(8)
                txbPDMotor.Text = sqlDataQuery(9)
                txbTDSN.Text = sqlDataQuery(10)
                txbMesFreq.Text = sqlDataQuery(11)
                txbMesDate.Text = sqlDataQuery(12)
                txbMesFreq2.Text = "-"
                txbMesFreq3.Text = "-"
                txbMesPw2.Text = "-"
                txbMesPw3.Text = "-"
                Return True
            Else
                Return False
            End If
        End If

    End Function

    Private Sub NonTD_Search()
        Dim DBCmd As String = "SELECT Product_SN, Ct_Manufacture_Name, Ct_SN, Ct_Model ,Ct_Type, Ct_Abroad, Ct_Agency, PV_SN,IFNULL(Manufacture_Date,'') as Manufacture_Date FROM " & NonCartridgeTbl & " WHERE PV_SN = '" & txbLoadNum.Text & "' Limit 1"

        If DBQueryReader(DBCmd) Then
            txbPDSN.Text = sqlDataQuery(0)
            txbPDCP.Text = sqlDataQuery(1)
            txbPDSerial.Text = sqlDataQuery(2)
            txbPDModel.Text = sqlDataQuery(3)
            txbPDType.Text = sqlDataQuery(4)
            txbPDCC.Text = sqlDataQuery(5)
            txbPDAC.Text = sqlDataQuery(6)
            txbMesPw.Text = ""
            txbPDSC.Text = ""
            txbPDMotor.Text = ""
            txbTDSN.Text = sqlDataQuery(7)
            txbMesFreq.Text = ""
            txbMesFreq2.Text = ""
            txbMesFreq3.Text = ""
            txbMesPw2.Text = ""
            txbMesPw3.Text = ""
            txbMesDate.Text = sqlDataQuery(8)

        Else
            MsgBox("존재하지 않는 데이터입니다.", vbExclamation)
        End If
    End Sub

    Private Function Product_Search() As Boolean
        If SelectedModelInfo.LdmUseYn = "Y" Then
            DBCmd = "Select Product_SN,Ct_Manufacture_Name,Ct_SN,Ct_Model,Ct_Type,Ct_Abroad,Ct_Agency,Ct_Power,Ct_Power2,Ct_Power3,Ct_Max_ShotCnt,Ct_Motor_Lv,PV_SN,Ct_Frequency,Ct_Frequency2,Ct_Frequency3,IFNULL(Manufacture_Date,'') as Manufacture_Date FROM " & UseLDMCartridgeTbl & " WHERE Product_SN = '" & txbLoadNum.Text & "' Limit 1"
            If DBQueryReader(DBCmd) Then
                txbPDSN.Text = sqlDataQuery(0)
                txbPDCP.Text = sqlDataQuery(1)
                txbPDSerial.Text = sqlDataQuery(2)
                txbPDModel.Text = sqlDataQuery(3)
                txbPDType.Text = sqlDataQuery(4)
                txbPDCC.Text = sqlDataQuery(5)
                txbPDAC.Text = sqlDataQuery(6)
                txbMesPw.Text = sqlDataQuery(7)
                txbMesPw2.Text = sqlDataQuery(8)
                txbMesPw3.Text = sqlDataQuery(9)

                txbPDSC.Text = sqlDataQuery(10)
                txbPDMotor.Text = sqlDataQuery(11)
                txbTDSN.Text = sqlDataQuery(12)

                txbMesFreq.Text = sqlDataQuery(13)
                txbMesFreq2.Text = sqlDataQuery(14)
                txbMesFreq3.Text = sqlDataQuery(15)

                txbMesDate.Text = sqlDataQuery(16)

                Return True
            Else
                Return False
            End If
        Else

            DBCmd = "SELECT Product_SN,Ct_Manufacture_Name,Ct_SN,Ct_Model,Ct_Type,Ct_Abroad,Ct_Agency,Ct_Power,Ct_Remain_ShotCnt,Ct_Motor_Lv,PV_SN,Ct_Frequency,IFNULL(Manufacture_Date,'') as Manufacture_Date FROM " & UseCartridgeTbl & " WHERE Product_SN = '" & txbLoadNum.Text & "' Limit 1"

            If DBQueryReader(DBCmd) Then
                txbPDSN.Text = sqlDataQuery(0)
                txbPDCP.Text = sqlDataQuery(1)
                txbPDSerial.Text = sqlDataQuery(2)
                txbPDModel.Text = sqlDataQuery(3)
                txbPDType.Text = sqlDataQuery(4)
                txbPDCC.Text = sqlDataQuery(5)
                txbPDAC.Text = sqlDataQuery(6)
                txbMesPw.Text = sqlDataQuery(7)
                txbPDSC.Text = sqlDataQuery(8)
                txbPDMotor.Text = sqlDataQuery(9)
                txbTDSN.Text = sqlDataQuery(10)
                txbMesFreq.Text = sqlDataQuery(11)
                txbMesDate.Text = sqlDataQuery(12)
                txbMesFreq2.Text = "-"
                txbMesPw2.Text = "-"
                txbMesFreq3.Text = "-"
                txbMesPw3.Text = "-"

                Return True
            Else
                Return False
            End If
        End If
    End Function

    Private Sub NonTDProduct_Search()
        Dim DBCmd As String = "SELECT Product_SN, Ct_Manufacture_Name, Ct_SN, Ct_Model,Ct_Type,Ct_Abroad,Ct_Agency,PV_SN,IFNULL(Manufacture_Date,'') as Manufacture_Date FROM " & NonCartridgeTbl & " WHERE Product_SN = '" & txbLoadNum.Text & "' Limit 1"

        If DBQueryReader(DBCmd) Then
            txbPDSN.Text = sqlDataQuery(0)
            txbPDCP.Text = sqlDataQuery(1)
            txbPDSerial.Text = sqlDataQuery(2)
            txbPDModel.Text = sqlDataQuery(3)
            txbPDType.Text = sqlDataQuery(4)
            txbPDCC.Text = sqlDataQuery(5)
            txbPDAC.Text = sqlDataQuery(6)
            txbMesPw.Text = ""
            txbPDSC.Text = ""
            txbPDMotor.Text = ""
            txbTDSN.Text = sqlDataQuery(7)
            txbMesFreq.Text = ""
            txbMesFreq2.Text = ""
            txbMesFreq3.Text = ""
            txbMesPw2.Text = ""
            txbMesPw3.Text = ""
            txbMesDate.Text = sqlDataQuery(8)

        Else
            MsgBox("존재하지 않는 데이터입니다.", vbExclamation)
        End If
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        txbPDSN.Text = "-"
        txbPDCP.Text = "-"
        txbPDSerial.Text = "-"
        txbPDModel.Text = "-"
        txbPDType.Text = "-"
        txbPDCC.Text = "-"
        txbPDAC.Text = "-"
        txbMesPw.Text = "-"
        txbPDSC.Text = "-"
        txbPDMotor.Text = "-"
        txbTDSN.Text = "-"
        txbMesFreq.Text = "-"
        txbMesFreq2.Text = "-"
        txbMesFreq3.Text = "-"
        txbMesPw.Text = "-"
        txbMesPw2.Text = "-"
        txbMesPw3.Text = "-"
        txbMesDate.Text = "-"
        txbMesDate.Text = "-"
    End Sub

    Private Sub btnwrite_Click(sender As Object, e As EventArgs) Handles btnwrite.Click
        ResetClear()
        ResultReset()

        btnIfRead_Click(sender, e)
        btnArRead_Click(sender, e)
        btnTdRead_Click(sender, e)

        If SelectModeType.Equals(ModeList.ALTO) Then
            Return
        End If

        btnFPRead_Click(sender, e)
        btnMtRead_Click(sender, e)
        btnShRead_Click(sender, e)
        btnFwRead_Click(sender, e)
    End Sub

End Class
