Imports System.Drawing.Printing
Imports System.IO
Imports System.IO.Ports
Imports System.Management
Imports System.Threading
Imports uPLibrary.Networking.M2Mqtt
Imports System.Linq
Imports System.Math
Imports System.Runtime.InteropServices.ComTypes
Imports System.Text

Public Class frmTD_JIG
    Public Const TESTER_VER As String = "3.0.1"
    Public ShowDetailLog As Boolean = False
    Public RunDemoMode As Boolean = False
    Public EnableSetup As Boolean = False
    Public cmdMessage As String
    Public rxMessage As String
    Public cmpMessage As String = "SUCCESS"
    Public tmpCmdMessage As String
    Public RunStartTick As Long
    Public RunEndTick As Long
    Public Const MeasSTART As Boolean = True
    Public Const MeasEND As Boolean = False
    Public Const SoundEnd As String = "0"
    Public Const SoundStart As String = "1"
    Public Const SoundError As String = "2"
    Public TestStep As Integer
    Public DevNum As Integer = 0
    Public DispToggle As Boolean = False
    Public Const TimeLine As Long = 500
    'Parametar 변수
    Public ParaComport As String
    Public ParaScopeID As String
    Public Const CmdNone As String = "none"
    Public Title_Text As String
    Public AutoAgingCnt As Long
    Public CkbNoRun As Boolean

    Private txtPVSN As String
    Private SettingMeasure As String

    Private IQCStart As Boolean = False
    Private CalModeOn As Boolean = False

    Public Enum CART_TYPE
        CART_NONE = 0
        PRO_FACE = 11
        PRO_EYE = 12
        PRO_BODY = 13
        LUX_FACE = 21
        LUX_EYE = 22
        LUX_BODY = 23
        MAX_SET = 30
        ALPHA_FACE = 41
        ALPHA_EYE = 42
        ALPHA_BODY = 43

        MAX_CART_TYPE
    End Enum
    Public CartType As CART_TYPE
    Public CartTypeName(CART_TYPE.MAX_CART_TYPE) As String

    Public GradeStop As Integer = 0

    Public IQCGradeName() As String = {"N/A", "IQC_ProCnt", "IQC_LuxCnt", "IQC_EyeCnt"}
    Public txbIQC_GCnt(MODEL_NAME.MAX_MODEL_NUM) As TextBox
    Public txbIQC_RCnt(RELAY_NUM.MAX_RELAY_NUM) As TextBox
    Public txbPD1_GCnt(MODEL_NAME.MAX_MODEL_NUM) As TextBox
    Public txbPD1_RCnt(RELAY_NUM.MAX_RELAY_NUM) As TextBox
    Public Structure PreCnt
        Public TotalCnt As Long
        Public OKCnt As Long
        Public NGCnt As Long
        Public ProCnt As Long
        Public LuxCnt As Long
        Public EyeCnt As Long
        Public R1Cnt As Long
        Public R2Cnt As Long
        Public R3Cnt As Long
        Public R4Cnt As Long
        Public R5Cnt As Long
        Public R6Cnt As Long
        Public R7Cnt As Long
        Public R8Cnt As Long
        Public R9Cnt As Long
        Public R10Cnt As Long
        Public ReTotalCnt As Long
        Public ReOkCnt As Long
        Public ReNgCnt As Long
    End Structure
    Public PreCnt_IQC As PreCnt
    Public PreCnt_PD1 As PreCnt

    Public ProMAX As Double = 1.83
    Public ProTYP As Double = 1.78
    Public ProMIN As Double = 1.73

    Public LuxMAX As Double = 1.72
    Public LuxTYP As Double = 1.67
    Public LuxMIN As Double = 1.62

    Public EyeMAX As Double = 1.61
    Public EyeTYP As Double = 1.56
    Public EyeMIN As Double = 1.51

    Public RELAY_C() As String
    Private RelayTxb(10) As TextBox
    Private RelayCnt() As Int16

    Public QC_Current As Double
    Public PD2_Current As Double

    Public CmdTime As Long
    Public EnableCmdTime As Boolean = False
    Public InputCnt As Long
    Public Const InputCntVal As Integer = 10

    Private IQCValueError As Double

    Private myMgr As Ivi.Visa.Interop.ResourceManager
    Private myScope As Ivi.Visa.Interop.FormattedIO488
    Public varQueryResult As Object
    Public strQueryResult As String
    Private DSOX1102_DEV1 As String = "USB0::0x2A8D::0x1797::CN56526457::0::INSTR"
    Private DSOX1102_DEV2 As String = "USB0::0x2A8D::0x178B::CN59280175::0::InStr"
    Private DSOX1102_DEV3 As String = "USB0::0x2A8D::0x1797::CN56526457::0::INSTR"

    Private MethodAVG As String = "MEASure:VAVerage?"
    Private MethodMAX As String = "MEASure:VMAX?"
    Private MethodTOP As String = "MEASure:VTOP?"

    Private cmd As String

    Private Delegate Sub PrintDelegate()
    Private PrintRunDelegate As PrintDelegate

    Public MQTClient As MqttClient
    Public mqttBroker As String = String.Empty
    Public mqttTopic As String = String.Empty

    Private Enum TestStepList
        MAX_First_OK = 16
        MAX_First_NG
        MAX_RE_OK
        MAX_RE_NG

        DUAL_First_OK = 21
        DUAL_First_NG
        DUAL_RE_OK
        DUAL_RE_NG
    End Enum
    Public Structure TestStepValue
        Public First_Ok As Int16
        Public First_Ng As Int16
        Public Re_OK As Int16
        Public RE_Ng As Int16
    End Structure
    Private TeststepSet As TestStepValue



    Private Sub print_on(printtext As String)
        Try
            Dim doc As bpac.Document = New bpac.Document

            If printer_state(txbPrintApply.Text) Then
                Dim print_address As String = txbLabel1.Text
                doc.Open(print_address)
                Dim print_name As Boolean = doc.SetPrinter(txbPrintApply.Text, True)
                doc.GetObject(txbCode1.Text).Text = printtext
                doc.GetObject(txbText1.Text).Text = printtext

                doc.StartPrint("", bpac.PrintOptionConstants.bpoDefault)
                doc.PrintOut(nudPrintCount.Value, bpac.PrintOptionConstants.bpoDefault)
                doc.EndPrint()

                doc.Close()
            Else
                MsgBox("프린터 연결이 잘못되었습니다. 전원이나 케이블을 확인하여 주십시오.", vbExclamation)
                Exit Sub
            End If

        Catch ex As Exception
            MsgBox("Print Error : " & ex.Message, vbCritical)
            Exit Sub
        End Try
    End Sub

    Private Function printer_state(printer As String) As Boolean
        Dim scope As ManagementScope = New ManagementScope("\root\cimv2")
        scope.Connect()
        Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_Printer")

        Dim printName As String = ""

        For Each printobj As ManagementObject In searcher.Get
            printName = printobj("Name").ToString
            If printName.Equals(printer) Then
                If printobj("WorkOffline").ToString.ToLower.Equals("true") Then
                    Return False
                Else
                    Return True
                End If
            End If
        Next

        Return False
    End Function

    Private Sub Print_Read_ini()
        Dim objReader As System.IO.StreamReader
        Dim configStr As String
        Dim FilePath As String = System.AppDomain.CurrentDomain.BaseDirectory() & "\Config"

        If Dir(FilePath, vbDirectory) = "" Then
            MkDir(FilePath)
        End If

        Dim FileName As String = FilePath & "\" & "TD_Select_Printer_Config" & ".ini"

        If File.Exists(FileName) Then
            objReader = New System.IO.StreamReader(FileName, True)
            configStr = objReader.ReadLine()
            objReader.Close()

            txbPrintApply.Text = Mid(configStr, 1, InStr(configStr, ",") - 1)
            configStr = Mid(configStr, InStr(configStr, ",") + 1, configStr.Length)

            txbLabel1.Text = Mid(configStr, 1, InStr(configStr, ",") - 1)
            configStr = Mid(configStr, InStr(configStr, ",") + 1, configStr.Length)

            txbCode1.Text = Mid(configStr, 1, InStr(configStr, ",") - 1)
            configStr = Mid(configStr, InStr(configStr, ",") + 1, configStr.Length)

            txbText1.Text = Mid(configStr, 1, InStr(configStr, ",") - 1)
            configStr = Mid(configStr, InStr(configStr, ",") + 1, configStr.Length)

            nudPrintCount.Text = Mid(configStr, 1, InStr(configStr, ",") - 1)
            configStr = Mid(configStr, InStr(configStr, ",") + 1, configStr.Length)

            ckbWarPnt.Checked = Mid(configStr, 1, configStr.Length)
        End If
    End Sub

    Private Function Read_ini() As Boolean
        Dim FilePath As String
        Dim FileName As String
        Dim ConifgStr As String
        Dim DevTemp As String
        Dim ShotCount As String
        Dim CartTemp As String
        Dim objReader As System.IO.StreamReader
        Dim tmpStr As String

        On Error GoTo RD_File_Error

        FilePath = System.AppDomain.CurrentDomain.BaseDirectory() & "\Config"

        '디렉토리가 없을 경우
        If Dir(FilePath, vbDirectory) = "" Then
            MkDir(FilePath)
        End If

        FileName = FilePath & "\" & "TD_Select_Config" & ".ini"

        If File.Exists(FileName) = True Then
            objReader = New System.IO.StreamReader(FileName, True)
            ConifgStr = objReader.ReadLine()
            objReader.Close()

            DevNum = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            If DevNum = "1" Then
                rdbIQC.Checked = True
            ElseIf DevNum = "2" Then
                rdbProduct1.Checked = True
            ElseIf DevNum = "3" Then
                rdbProduct2.Checked = True
            ElseIf DevNum = "4" Then
                rdbProduct3.Checked = True
            Else
                DevNum = 0
            End If

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            cboComPort.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbScopeID.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbMeasureDelay.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbTimeout.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbOntime.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbProMax.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbProTyp.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbProMin.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbLuxMax.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbLuxTyp.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbLuxMin.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbEyeMax.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbEyeTyp.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbEyeMin.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ReDim RELAY_C(10)
            RELAY_C(0) = "N/A"
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay1.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(1) = txbRelay1.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay2.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(2) = txbRelay2.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay3.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(3) = txbRelay3.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay4.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(4) = txbRelay4.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay5.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(5) = txbRelay5.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay6.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(6) = txbRelay6.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay7.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(7) = txbRelay7.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay8.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(8) = txbRelay8.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay9.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(9) = txbRelay9.Text
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbRelay10.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            RELAY_C(10) = txbRelay10.Text

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbHSCALE.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbHPOS.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbVSCALE.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbVPOS.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            tmpStr = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            If tmpStr = "AVG" Then
                rdbAvg.Checked = True
            ElseIf tmpStr = "MAX" Then
                rdbMax.Checked = True
            ElseIf tmpStr = "TOP" Then
                rdbTop.Checked = True
            End If

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbErrorFactor.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbOffsetValue.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbStartWaitDelay.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbWaitDelay.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbValueError.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbIQC_DummyShotCnt.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbMotorOnTime.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKProMax.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKProTyp.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKProMin.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKLuxMax.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKLuxTyp.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKLuxMin.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKEyeMax.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbKEyeTyp.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            txbEyeMin.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            nudmat_1.Value = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            nudmat_2.Value = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            nudmat_3.Value = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            nudmat_4.Value = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            nudmat_5.Value = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            Dim testMode As Boolean
            If UBound(Split(ConifgStr, ",")) > 0 Then
                testMode = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            Else
                testMode = ConifgStr
            End If

            ConifgStr = Mid(ConifgStr, InStr(ConifgStr, ",") + 1, Len(ConifgStr))
            If UBound(Split(ConifgStr, ",")) > 0 Then
                txbMinValue.Text = Mid(ConifgStr, 1, InStr(ConifgStr, ",") - 1)
            Else
                txbMinValue.Text = ConifgStr
            End If

            ParaComport = cboComPort.Text
            ParaScopeID = "USB0::0x2A8D::" & txbScopeID.Text & "::0::INSTR"

            If testMode = True Then
                rdbCmat_10.Checked = True
                rdbCmat_5.Checked = False
            Else
                rdbCmat_10.Checked = False
                rdbCmat_5.Checked = True
            End If
            If rdbCmat_5.Checked = True Then
                ReDim RelayCnt(5)
                RelayCnt(1) = nudmat_1.Value
                RelayCnt(2) = nudmat_2.Value
                RelayCnt(3) = nudmat_3.Value
                RelayCnt(4) = nudmat_4.Value
                RelayCnt(5) = nudmat_5.Value
            Else
                ReDim RelayCnt(10)
                For i = 1 To 10
                    RelayCnt(i) = i
                Next
            End If

            lblR1CntIQC.Text = txbRelay1.Text
            lblR1CntPD1.Text = txbRelay1.Text
            lblR1CntIQC.Refresh()
            lblR1CntPD1.Refresh()

            lblR2CntIQC.Text = txbRelay2.Text
            lblR2CntPD1.Text = txbRelay2.Text
            lblR2CntIQC.Refresh()
            lblR2CntPD1.Refresh()

            lblR3CntIQC.Text = txbRelay3.Text
            lblR3CntPD1.Text = txbRelay3.Text
            lblR3CntIQC.Refresh()
            lblR3CntPD1.Refresh()

            lblR4CntIQC.Text = txbRelay4.Text
            lblR4CntPD1.Text = txbRelay4.Text
            lblR4CntIQC.Refresh()
            lblR4CntPD1.Refresh()

            lblR5CntIQC.Text = txbRelay5.Text
            lblR5CntPD1.Text = txbRelay5.Text
            lblR5CntIQC.Refresh()
            lblR5CntPD1.Refresh()

            lblR6CntIQC.Text = txbRelay6.Text
            lblR6CntPD1.Text = txbRelay6.Text
            lblR6CntIQC.Refresh()
            lblR6CntPD1.Refresh()

            lblR7CntIQC.Text = txbRelay7.Text
            lblR7CntPD1.Text = txbRelay7.Text
            lblR7CntIQC.Refresh()
            lblR7CntPD1.Refresh()

            lblR8CntIQC.Text = txbRelay8.Text
            lblR8CntPD1.Text = txbRelay8.Text
            lblR8CntIQC.Refresh()
            lblR8CntPD1.Refresh()

            lblR9CntIQC.Text = txbRelay9.Text
            lblR9CntPD1.Text = txbRelay9.Text
            lblR9CntIQC.Refresh()
            lblR9CntPD1.Refresh()

            lblR10CntIQC.Text = txbRelay10.Text
            lblR10CntPD1.Text = txbRelay10.Text
            lblR10CntIQC.Refresh()
            lblR10CntPD1.Refresh()

            txbIQC_GCnt(1) = txbProCnt
            txbIQC_GCnt(2) = txbLuxCnt
            txbIQC_GCnt(3) = txbEyeCnt

            txbIQC_RCnt(1) = txbR1CntIQC
            txbIQC_RCnt(2) = txbR2CntIQC
            txbIQC_RCnt(3) = txbR3CntIQC
            txbIQC_RCnt(4) = txbR4CntIQC
            txbIQC_RCnt(5) = txbR5CntIQC
            txbIQC_RCnt(6) = txbR6CntIQC
            txbIQC_RCnt(7) = txbR7CntIQC
            txbIQC_RCnt(8) = txbR8CntIQC
            txbIQC_RCnt(9) = txbR9CntIQC
            txbIQC_RCnt(10) = txbR10CntIQC

            txbPD1_GCnt(1) = txbProCntPD1
            txbPD1_GCnt(2) = txbLuxCntPD1
            txbPD1_GCnt(3) = txbEyeCntPD1
            txbPD1_GCnt(4) = txbMaxCntPD1

            txbPD1_RCnt(1) = txbR1CntPD1
            txbPD1_RCnt(2) = txbR2CntPD1
            txbPD1_RCnt(3) = txbR3CntPD1
            txbPD1_RCnt(4) = txbR4CntPD1
            txbPD1_RCnt(5) = txbR5CntPD1
            txbPD1_RCnt(6) = txbR6CntPD1
            txbPD1_RCnt(7) = txbR7CntPD1
            txbPD1_RCnt(8) = txbR8CntPD1
            txbPD1_RCnt(9) = txbR9CntPD1
            txbPD1_RCnt(10) = txbR10CntPD1

            cboGrade.Items.Add("PRO")
            cboGrade.Items.Add("LUX")
            cboGrade.Items.Add("EYE")

            cboCapacitor.Items.Add(txbRelay1.Text)
            cboCapacitor.Items.Add(txbRelay2.Text)
            cboCapacitor.Items.Add(txbRelay3.Text)
            cboCapacitor.Items.Add(txbRelay4.Text)
            cboCapacitor.Items.Add(txbRelay5.Text)
            cboCapacitor.Items.Add(txbRelay6.Text)
            cboCapacitor.Items.Add(txbRelay7.Text)
            cboCapacitor.Items.Add(txbRelay8.Text)
            cboCapacitor.Items.Add(txbRelay9.Text)
            cboCapacitor.Items.Add(txbRelay10.Text)

            CartTypeName(CART_TYPE.CART_NONE) = "입력 대기"
            CartTypeName(CART_TYPE.PRO_FACE) = "PRO FACE"
            CartTypeName(CART_TYPE.PRO_EYE) = "PRO EYE"
            CartTypeName(CART_TYPE.LUX_FACE) = "LUX FACE"
            CartTypeName(CART_TYPE.LUX_EYE) = "LUX EYE"

            Return True
        Else
            MsgBox("검사기 정보가 없습니다." & vbCrLf & "검사기 설정을 진행하세요.", vbCritical, "DUALSONIC T/D Selecter Tester Manager")

            frmLogin.ShowDialog()

            If EnableSetup Then
                loadSetupWindows()
            Else
                MsgBox("권한이 있는지 확인하세요.")
                End
            End If
            EnableSetup = False

            Return False
        End If

        Exit Function

RD_File_Error:
        MsgBox("검사기 정보가 일치하지 않습니다." & vbCrLf & "검사기 설정을 재설정 해주세요.", vbCritical, "DUALSONIC T/D Selecter Tester Manager")

        frmLogin.ShowDialog()

        If EnableSetup Then
            loadSetupWindows()
        Else
            MsgBox("권한이 있는지 확인하세요.")
            End
        End If
        EnableSetup = False
        Return False

    End Function

    Private Sub Save_ini(SaveType As Boolean)
        Dim FilePath As String
        Dim FileName As String
        Dim ConfigStr As String
        Dim objWriter As System.IO.StreamWriter
        Dim tmpStr, testMode As String

        On Error GoTo WR_File_Error

        FilePath = System.AppDomain.CurrentDomain.BaseDirectory() & "\Config"

        '디렉토리가 없을 경우
        If Dir(FilePath, vbDirectory) = "" Then
            MkDir(FilePath)
        End If

        If SaveType = True Then
            FileName = FilePath & "\" & "TD_Select_Printer_Config" & ".ini"

            ConfigStr = txbPrintApply.Text & "," & txbLabel1.Text & "," & txbCode1.Text & "," & txbText1.Text & "," & nudPrintCount.Value & "," & ckbWarPnt.Checked
        Else
            FileName = FilePath & "\" & "TD_Select_Config" & ".ini"

            If rdbIQC.Checked Then
                DevNum = 1
            ElseIf rdbProduct1.Checked Then
                DevNum = 2
            ElseIf rdbProduct2.Checked Then
                DevNum = 3
            ElseIf rdbProduct3.Checked Then
                DevNum = 4
            End If

            If rdbAvg.Checked Then
                tmpStr = "AVG"
            ElseIf rdbMax.Checked = True Then
                tmpStr = "MAX"
            Else
                tmpStr = "TOP"
            End If

            If rdbCmat_10.Checked Then
                testMode = True
            Else
                testMode = False
            End If

            ConfigStr = DevNum & "," & cboComPort.Text & "," & txbScopeID.Text & "," & txbMeasureDelay.Text & "," & txbTimeout.Text & "," & txbOntime.Text & "," & txbProMax.Text & "," & txbProTyp.Text & "," & txbProMin.Text & "," & txbLuxMax.Text & "," & txbLuxTyp.Text & "," & txbLuxMin.Text & "," & txbEyeMax.Text & "," & txbEyeTyp.Text & "," & txbEyeMin.Text & "," & txbRelay1.Text & "," & txbRelay2.Text & "," & txbRelay3.Text & "," & txbRelay4.Text & "," & txbRelay5.Text & "," & txbRelay6.Text & "," & txbRelay7.Text & "," & txbRelay8.Text & "," & txbRelay9.Text & "," & txbRelay10.Text & "," & txbHSCALE.Text & "," & txbHPOS.Text & "," & txbVSCALE.Text & "," & txbVPOS.Text & "," & tmpStr & "," & txbErrorFactor.Text & "," & txbOffsetValue.Text & "," & txbStartWaitDelay.Text & "," & txbWaitDelay.Text & "," & txbValueError.Text & "," & txbIQC_DummyShotCnt.Text & "," & txbMotorOnTime.Text & "," & txbKProMax.Text & "," & txbKProTyp.Text & "," & txbKProMin.Text & "," & txbKLuxMax.Text & "," & txbKLuxTyp.Text & "," & txbKLuxMin.Text & "," & txbKEyeMax.Text & "," & txbKEyeTyp.Text & "," & txbKEyeMin.Text & "," & nudmat_1.Text & "," & nudmat_2.Text & "," & nudmat_3.Text & "," & nudmat_4.Text & "," & nudmat_5.Text & "," & testMode & "," & txbMinValue.Text
        End If

        System.IO.File.Delete(FileName)
        objWriter = New System.IO.StreamWriter(FileName, True)

        objWriter.Flush()
        objWriter.WriteLine(ConfigStr)

        objWriter.Close()
        Exit Sub

WR_File_Error:
        MsgBox("Config Save Fail!")
    End Sub

    Private Sub PrintSaveData()
        If txbLabel1.Text.Replace(" ", "") = "" Or txbCode1.Text.Replace(" ", "") = "" Or txbPrintApply.Text.Replace(" ", "") = "" Or txbText1.Text.Replace(" ", "") = "" Then
            MsgBox("바코드 인쇄 프린터 설정란에 빈 칸이 존재합니다. 빈 칸을 채워주십시오.", vbExclamation)
            Exit Sub
        End If

        Save_ini(True)
    End Sub

    Private Sub Save_CSV(SaveData As String)
        Dim FilePath As String
        Dim syear As String
        Dim smonth As String
        Dim sday As String
        Dim FileCnt As Long
        Dim runFlag As Boolean = True
        Dim FileName As String
        Dim ItemList As String = ""
        Dim objWriter As System.IO.StreamWriter

        On Error GoTo WR_File_Error

        syear = DateAndTime.Year(Now)
        smonth = DateAndTime.Month(Now)
        sday = DateAndTime.Day(Now)

        FilePath = System.AppDomain.CurrentDomain.BaseDirectory()

        If DevNum = 1 Then
            FilePath = FilePath & "LogData\IQC"
            ItemList = "Date,Time,TD_SN,TestStep,Ok_Ng,Grade,C_Value,Current,RL1_Value,RL2_Value,RL3_Value,RL4_Value,RL5_Value,RL6_Value,RL7_Value,RL8_Value,RL9_Value,RL10_Value,TD_IN_DATE,TD_Frequency,TD_Impedance,TD_Capacitance,TD_Resistance,Total_Count,OK_Count,NG_Count,PRO_Count,LUX_Count,EYE_Count,RL1_Count,RL2_Count,RL3_Count,RL4_Count,RL5_Count,RL6_Count,RL7_Count,RL8_Count,RL9_Count,RL10_Count,ReTotal_Count,ReOk_Count,ReNg_Count"
        ElseIf DevNum = 2 Then
            FilePath = FilePath & "LogData\PD1"
            ItemList = "Date,Time,TD_SN,TestStep,TD_DATE,TD_Frequency,TD_Impedance,TD_Capacitance,TD_Resistance,Grade,C_Value,Total_Count,OK_Count,NG_Count,PRO_Count,LUX_Count,EYE_Count,RL1_Count,RL2_Count,RL3_Count,RL4_Count,RL5_Count,RL6_Count,RL7_Count,RL8_Count,RL9_Count,RL10_Count"
        ElseIf DevNum = 3 Then
            FilePath = FilePath & "LogData\PD2"
        ElseIf DevNum = 4 Then
            FilePath = FilePath & "LogData\PD3"
        End If

        '디렉토리가 없을 경우
        If Dir(FilePath, vbDirectory) = "" Then
            MkDir(FilePath)
        End If

        FileName = FilePath & "\" & DateAndTime.Year(Now) & "_" & DateAndTime.Month(Now) & "_" & DateAndTime.Day(Now) & ".csv"


        If File.Exists(FileName) = True Then
            objWriter = New System.IO.StreamWriter(FileName, True)
            objWriter.WriteLine(SaveData)
        Else
            objWriter = New System.IO.StreamWriter(FileName, True)
            objWriter.WriteLine(ItemList)
            objWriter.WriteLine(SaveData)
        End If

        objWriter.Close()

        Exit Sub

WR_File_Error:
        MsgBox("Logdata save fail!")
    End Sub
    Sub cmdDisplay(strDisp As String)
        Dim dispStr As String

        dispStr = (Today + " " + TimeOfDay + " " + strDisp + vbCrLf)

        If DevNum = 1 Then
            txbCmdWindow.AppendText(dispStr)
            txbCmdWindow.Refresh()
        ElseIf DevNum = 3 Then
            txbCmdWindowP2.AppendText(dispStr)
            txbCmdWindowP2.Refresh()
        Else

        End If
    End Sub

    Private Sub FormLoadInfo()
        Dim FormWidth, FormHeight As Integer
        Me.Hide()

        'Me.WindowState = FormWindowState.Maximized

        FormWidth = Val(Me.Width)
        FormHeight = Val(Me.Height)

        If FormWidth < 1100 And FormHeight < 800 Then
            'Me.MaximumSize = New Size(FormWidth, FormHeight)
            Me.Width = FormWidth
            Me.Height = FormHeight
        Else
            'Me.WindowState = FormWindowState.Normal
            'Me.MaximumSize = New Size(1040, 784)
            Me.Width = 1040
            Me.Height = 784
        End If

        Me.Show()
        Me.Refresh()

        gpbTime.Refresh()
        lblTime.Refresh()
        gpbConfig.Refresh()
        chbUseBarcode.Refresh()
        chbAutoMeasure.Refresh()
        btnSetup.Refresh()
        btnExit.Refresh()
        gpbSpec.Refresh()
        txbPV_SN.Refresh()
        lblPV_SN.Refresh()
        txbPV_Date.Refresh()
        lblPV_Date.Refresh()
        txbPV_Freq.Refresh()
        lblPV_Freq.Refresh()
        txbPV_Z.Refresh()
        lblPV_Z.Refresh()
        txbPV_C.Refresh()
        lblPV_C.Refresh()
        txbPV_R.Refresh()
        lblPV_R.Refresh()

        cmdDisplay("DUALSONIC T/D Select JIG Start!")

    End Sub

    Private Sub loadSetupWindows()
        'Me.Size = New Size(1500, 780)
        Me.Size = New Size(1500, 880)
        EnableSetup = True
        gpbSetupWindow.Visible = True
        gpbSetupWindow.Location = New Size(1, 9)
        'gpbSetupWindow.Size = New Size(1475, 734)
        gpbSetupWindow.Size = New Size(1475, 800)
        DefaultTextColor()
    End Sub

    Private Function ParametarLoad() As Boolean
        Print_Read_ini()
        Return Read_ini()
    End Function

    Private Sub LoadComportName()
        Dim portname() As String
        Dim i As Integer

        Try
            cboComPort.Items.Clear()
            portname = SerialPort.GetPortNames

            For i = 1 To portname.Length
                cboComPort.Items.Add(portname(i - 1))
            Next i

            cboComPort.Text = "PRESS!"
        Catch ex As Exception
            MsgBox("COM 포트 에러" & vbCrLf & "", vbOK + vbCritical, "DUALSONIC T/D Selecter Tester Manager")
            ckbSerialUse.Checked = False
        End Try
    End Sub

    Private Sub OpenSerial()
        Try
            If ParaComport <> "" Then
                If cmdSerialPort.IsOpen = False Then
                    With cmdSerialPort
                        .PortName = ParaComport
                        .BaudRate = 115200
                        .DataBits = 8
                        .Parity = IO.Ports.Parity.None
                        .StopBits = IO.Ports.StopBits.One
                        .Handshake = IO.Ports.Handshake.None
                    End With
                    cmdSerialPort.Open()
                    cmdDisplay("Open comport complete!")
                End If
            Else
                MsgBox("시리얼포트를 설정해주세요!" & vbCrLf & "", vbCritical, "DUALSONIC T/D Selecter Tester Manager")
                ckbSerialUse.Checked = False
            End If
        Catch ex As Exception
            MsgBox("COM 포트 에러" & vbCrLf & "", vbOK + vbCritical, "DUALSONIC T/D Selecter Tester Manager")
            ckbSerialUse.Checked = False
        End Try
    End Sub
    Private Sub loadIQCWindows()
        gpbIQC.Visible = True
        gpbProduct1.Visible = False
        gpbProduct2.Visible = False
        gpbProduct4.Visible = False
        gpbIQC.Refresh()
        Me.Text = Title_Text & " (T/D 수입 검사 공정)"
        gpbIQC.Location = New Size(2, 230)
        gpbIQC.Size = New Size(1020, 514)
        gpbIQC.Refresh()

        chbUseBarcode.Checked = True
        chbUseBarcode.Enabled = True
        chbUseBarcode.Refresh()

        chbAutoMeasure.Checked = True
        chbAutoMeasure.Enabled = True
        chbUseBarcode.Refresh()

        btnRunIQC.Enabled = False
        btnRunIQC.Refresh()

        txbPV_SN.Focus()

        showCmdWindow()
    End Sub

    Private Sub loadProduct1Windows()
        gpbIQC.Visible = False
        gpbProduct1.Visible = True
        gpbProduct2.Visible = False
        gpbProduct4.Visible = False
        Me.Text = Title_Text & " (캐패시터 값 확인 및 C/T 주파수입력 공정)"

        gpbProduct1.Location = New Size(2, 230)
        gpbProduct1.Size = New Size(1020, 513)
        gpbProduct1.Refresh()

        txbPV_SN.Location = New Size(123, 30)
        txbPV_SN.Size = New Size(364, 76)
        txbPV_SN.Refresh()

        lblGradePD1.Location = New Size(202, 29)
        txbGradePD1.Location = New Size(406, 14)

        lblCapacitorPD1.Location = New Size(202, 136)
        txbCapacitorPD1.Location = New Size(406, 121)

        lblCartridgePD1.Location = New Size(145, 243)
        txbCartridgePD1.Location = New Size(406, 228)

        lblGradePD1.Refresh()
        txbGradePD1.Refresh()
        lblCapacitorPD1.Refresh()
        txbCapacitorPD1.Refresh()
        lblCartridgePD1.Refresh()
        txbCartridgePD1.Refresh()
        gpbCheckGrade.Refresh()

        chbUseBarcode.Checked = True
        chbUseBarcode.Enabled = True
        chbAutoMeasure.Checked = True
        chbAutoMeasure.Enabled = True
        btnRunPD1.Enabled = False

        txbPV_SN.Focus()
    End Sub

    Private Sub loadProduct2Windows()
        gpbIQC.Visible = False
        gpbProduct1.Visible = False
        gpbProduct2.Visible = True
        gpbProduct3.Visible = False
        gpbProduct4.Visible = False
        gpbProduct2.Refresh()
        Me.Text = Title_Text & " (카트리지 조립 후 성능검사 공정)"

        chbUseBarcode.Checked = False
        chbUseBarcode.Enabled = False
        chbUseBarcode.Refresh()
        chbAutoMeasure.Checked = True
        chbAutoMeasure.Enabled = True
        chbAutoMeasure.Refresh()
        gpbProduct2.Location = New Size(2, 230)
        gpbProduct2.Size = New Size(1018, 513)
        gpbProduct2.Refresh()

        ''txbPV_SN.Location = New Size(105, 30)
        ''txbPV_SN.Size = New Size(382, 76)
        ''txbPV_SN.Refresh()

        btnRunPD2.Enabled = False
        btnRunPD2.Refresh()

        txbPV_SN.Clear()
        txbPV_SN.AppendText("대기중")
        txbPV_SN.Refresh()

        txbCmdWindowP2.Focus()
        txbCmdWindowP2.Refresh()

        DispToggle = True
        cmdRunTimer.Enabled = True
    End Sub
    Private Sub initCartInfo()
        Try
            rdbFace.Checked = True
            rdbMode1.Checked = True
            If setOnTime(txbMotorOnTime.Text) Then
                MsgBox("AMP 정보 초기화 실패.")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub loadProduct3Windows()
        gpbIQC.Visible = False
        gpbProduct1.Visible = False
        gpbProduct2.Visible = False
        gpbProduct3.Visible = True
        gpbProduct4.Visible = False
        Me.Text = Title_Text & " (도트 검사 공정)"

        gpbProduct3.Location = New Size(2, 230)
        gpbProduct3.Size = New Size(1018, 513)
        gpbProduct3.Refresh()

        ''txbPV_SN.Location = New Size(123, 30)
        ''txbPV_SN.Size = New Size(364, 76)
        ''txbPV_SN.Refresh()

        lblGradePD3.Location = New Size(202, 35)
        lblGradePD3.Size = New Size(147, 77)
        lblGradePD3.Refresh()
        txbGradePD3.Location = New Size(406, 25)
        txbGradePD3.Size = New Size(384, 95)
        txbGradePD3.Refresh()

        lblCapacitorPD3.Location = New Size(202, 145)
        lblCapacitorPD3.Size = New Size(148, 77)
        lblCapacitorPD3.Refresh()
        txbCapacitorPD3.Location = New Size(406, 135)
        txbCapacitorPD3.Size = New Size(384, 95)
        txbCapacitorPD3.Refresh()

        lblCapSelectPD3.Visible = False
        pnlCapSelectPD3.Visible = False

    End Sub
    Private Sub initDevice()
        cmdDisplay("Load Tester Parameter Complete!")
        lblTime.Text = Now

        If DevNum = 1 Then

            ckbPlay.Text = "신규측정방식"
            ckbPlay.Checked = True
            ckbSerialUse.Visible = False

            loadIQCWindows()
            If LoadRegIQC() Then
                cmdDisplay(Err.Description)
                cmdDisplay("Today's measure count data check fail!")
                Return
            Else
                LoadTextIQC()
                SetDefaultText()
                cmdDisplay("Today's measure count data check complete!")
            End If
            OpenSerial()
            initScope()

        ElseIf DevNum = 2 Then

            ckbPlay.Text = "MAXIMUM"
            ckbPlay.Checked = True
            ckbSerialUse.Visible = True

            loadProduct1Windows()
            'If LoadRegPD1() Then
            '    Return
            'Else
            '    LoadTextPD1()
            '    SetDefaultText()
            'End If

            ResetRegMesPD1()

            If LoadRegPD1() Then
                Return
            Else
                If LoadLocalCount() Then
                    Return
                Else
                    LoadTextPDLocal1()
                    SetDefaultText()
                End If
            End If

            gradeReset()


        ElseIf DevNum = 3 Then

            loadProduct2Windows()
            txbCmdWindowP2.Focus()
            OpenSerial()
            initScope()

        ElseIf DevNum = 4 Then

            loadProduct3Windows()
            OpenSerial()
            initCartInfo()
        End If
        cmdDisplay("Load Windows Form Complete!")

    End Sub

    Private Sub frmTD_JIG_Disposed(sender As System.Object, e As System.EventArgs) Handles MyBase.Disposed
        Try
            If cmdSerial.Text = "닫기" Then
                cmdSerialPort.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        exit_Process()
    End Sub

    Private Sub frmTD_JIG_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try

            Me.Text = "DUALSONIC T/D MEASURING INSTRUMENT Ver" & TESTER_VER
            Title_Text = Me.Text

            SetDefaultText()

            If ParametarLoad() Then
                FormLoadInfo()
                initDevice()
            End If

            If frmMqtt.loadMqtt = False Then
                frmMqtt.txbMqttAd.Text = String.Empty
                frmMqtt.txbMqttTp.Text = String.Empty
                mqttBroker = String.Empty
                mqttTopic = String.Empty
                frmLogin.mqttStart = False
            Else

                If frmLogin.mqttConnect() Then
                    frmLogin.mqttStart = True
                Else
                    frmLogin.mqttStart = False
                End If
            End If

        Catch ex As Exception
            cmdDisplay("Form Load Error!")
        End Try
    End Sub

    Sub initScope()
        On Error GoTo VisaComError
        If ParaScopeID <> "" Then
            myMgr = New Ivi.Visa.Interop.ResourceManager
            myScope = New Ivi.Visa.Interop.FormattedIO488

            myScope.IO = myMgr.Open(ParaScopeID)
            'myScope.IO = myMgr.Open(DSOX1102_DEV1)
            myScope.IO.Clear() ' Clear the interface.
            myScope.IO.Timeout = 10000 ' Set I/O communication timeout.

            ' Initialize - start from a known state.
            Initialize()
            cmdDisplay("Open Scope device complete!")
        Else
            MsgBox("스코프 ID를 설정해주세요!" & vbCrLf & "", vbCritical, "DUALSONIC T/D Selecter Tester Manager")
        End If
        Exit Sub
VisaComError:
        MsgBox("스코프 설정을 확인해주세요!" & vbCrLf & "", vbCritical, "DUALSONIC T/D Selecter Tester Manager")
        cmdDisplay("VISA COM Error:" & Err.Description)
        'MsgBox("VISA COM Error:" + vbCrLf + Err.Description)
    End Sub

    Private Sub Initialize()
        On Error GoTo VisaComError
        ' Get and display the device's *IDN? string.
        strQueryResult = DoQueryString("*IDN?")
        Debug.Print("Identification string: " + strQueryResult)

        ' Clear status and load the default setup.
        DoCommand("*RST")
        DoCommand("*CLS")

        DoCommand(":DISPlay:ANNotation 1")
        DoCommand(":DISPlay:ANNotation:Text 'JION Meditech Tester'")
        DoCommand(":DISPlay:ANNotation:BACKground TRAN")

        DoCommand(":MEASure:SOURce CHANnel1")
        DoCommand(":CHANnel1:UNITs AMPere")
        DoCommand(":CHANnel1:COUPling DC")


        ' 트리거 설정
        DoCommand(":TRIGger:MODE EDGE")  ' 트리거 모드 에지
        DoCommand(":ACQuire:TYPE HRES")  ' 획득 타입 하이-레졸루션
        DoCommand(":TRIGger:EDGE:SLOPe POS")
        DoCommand(":TRIGger:EDGE:LEVel 0.5")
        DoCommand(":TRIGger:SWEep NORMal") '트리거 모드 노멀 / Auto


        runScopeScale()

        'DoCommand(":AutoScale")
        ''DoCommand(":CHANnel1:SCALe 1.0")
        ''DoCommand(":CHANnel1:OFFSet 1.5")
        ''DoCommand(":TIMebase:SCALe 0.001")
        ''DoCommand(":TIMebase:POSition 2.0")
        ''MsgBox(strQueryResult = DoQueryString("MEASure:VAVerage?"))
        ''MsgBox(strQueryResult = DoQueryString("MEASure:VBASe?"))
        ''MsgBox(strQueryResult = DoQueryString("MEASure:VMAX?"))
        Exit Sub
VisaComError:
        cmdDisplay("VISA COM Error:" & Err.Description)
        'MsgBox("VISA COM Error:" + vbCrLf + Err.Description)
        DoCommand("*CLS")
        'End
    End Sub

    Private Function DoCommand(command As String) As String
        On Error GoTo VisaComError

        myScope.WriteString(command)

        CheckInstrumentErrors()
        Return "Pass"
VisaComError:
        cmdDisplay("ERROR[C]: " & Err.Description)
        Return "Fail"
        'MsgBox("VISA COM Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + ", " + Err.Description, vbExclamation, "VISA COM Error")
    End Function

    Private Function DoQueryString(query As String) As String
        On Error GoTo VisaComError
        myScope.WriteString(query)
        DoQueryString = myScope.ReadString
        CheckInstrumentErrors()
        Return DoQueryString
        Exit Function
VisaComError:
        cmdDisplay("ERROR[Q]: " & Err.Description)
        Return "Fail"
        'MsgBox("VISA COM Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + ", " + Err.Description, vbExclamation, "VISA COM Error")
    End Function

    Private Sub CheckInstrumentErrors()

        On Error GoTo VisaComError
        Dim strErrVal As String
        Dim strOut As String = ""

        myScope.WriteString(":SYSTem:ERRor?") ' Query any errors data.
        strErrVal = myScope.ReadString ' Read: Errnum,"Error String".
        While Val(strErrVal) <> 0 ' End if find: 0,"No Error".
            strOut = "INST Error: " + strErrVal
            myScope.WriteString(":SYSTem:ERRor?") ' Request error message.
            strErrVal = myScope.ReadString ' Read error message.
        End While

        If Not strOut = "" Then
            cmdDisplay(strOut)
            myScope.FlushWrite(False)
            myScope.FlushRead()
        End If
        Exit Sub
VisaComError:
        ''MsgBox("VISA COM Error: " + vbCrLf + Err.Description)
        cmdDisplay("ERROR[E]: " & Err.Description)
        myScope.FlushWrite(False)
        myScope.FlushRead()
    End Sub

    Private Sub runTimeTimer_Tick(sender As Object, e As EventArgs) Handles runTimeTimer.Tick
        lblTime.Text = Now
        If PD1_STATUS = PD1_ENUM.WAIT And DevNum = 2 Then
            If txbCartridgePD1.BackColor = Color.Blue Then
                txbCartridgePD1.BackColor = Color.Yellow
            Else
                txbCartridgePD1.BackColor = Color.Blue
            End If
            txbCartridgePD1.Refresh()
        ElseIf DevNum = 3 And DispToggle Then
            If EnableSetup = False Then txbCmdWindowP2.Focus()
            If txbPV_SN.BackColor = Color.Blue Then
                txbPV_SN.BackColor = Color.Yellow
            Else
                txbPV_SN.BackColor = Color.Blue
            End If
            txbPV_SN.Refresh()
        End If
    End Sub

    Private Function SendData(SendMessage As String) As Boolean
        Try
            If cmdSerialPort.IsOpen = True Then
                'txbSerial.Text = ""
                cmdSerialPort.DiscardInBuffer()
                cmdSerialPort.DiscardOutBuffer()
                If cmdSerialPort.BytesToRead Then
                    MsgBox(cmdSerialPort.BytesToRead)
                    cmdSerialPort.DiscardInBuffer()
                End If
                cmdSerialPort.WriteTimeout = -1
                cmdSerialPort.WriteLine(SendMessage)
                CmdTime = 0
                EnableCmdTime = True
            Else
                MsgBox("통신 포트가 열리지 않았습니다.")
                ckbSerialUse.Checked = False
                Return True
            End If
        Catch ex As Exception
            Return True
        End Try
        Return False
    End Function

    Private Sub ComportSetup()
        With cmdSerialPort
            '.PortName = "COM" & Val(cboComPort.Text)
            .PortName = cboComPort.Text
            .BaudRate = 230400
            .DataBits = 8
            .Parity = IO.Ports.Parity.None
            .StopBits = IO.Ports.StopBits.One
            .Handshake = IO.Ports.Handshake.None
            ''.RtsEnable = True
            '.ReceivedBytesThreshold = 1

            'AddHandler cmdSerialPort.DataReceived, AddressOf DataReceivedHandler

        End With
    End Sub

    Private Sub exit_Process()
        Try
            cmdSerialPort.Close()
            ''cmdSerial.Text = "열기"
            ''cboComPort.Enabled = True
            ''txbAuthBox.Visible = False
            ''txbCartBox.Visible = False
            ''txbShotBox.Visible = False
            ''GroupBoxEnable(False)
            ''Save_ini()
            End
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        ''Try
        ''    Me.Close()
        ''Catch ex As InvalidOperationException
        ''    Console.WriteLine(ex.GetType().FullName)
        ''    Console.WriteLine(ex.Message)
        ''End Try

    End Sub

    Private Sub runScopeScale()
        DoCommand(":CHANnel1:SCALe " & txbVSCALE.Text)
        DoCommand(":CHANnel1:OFFSet " & txbVPOS.Text)
        DoCommand(":TIMebase:SCALe " & txbHSCALE.Text)
        DoCommand(":TIMebase:POSition " & txbHPOS.Text)
    End Sub

    Private Sub DelayMs(DelayTime As Long)
        Static Dim StartTick As Long
        Dim EndTick As Long

        StartTick = GetTickCount()

        Do Until (EndTick - StartTick) >= DelayTime
            EndTick = GetTickCount()
        Loop
    End Sub

    Private Sub RunStableTime()
        Dim TmpTime As Double
        Dim TimeLineCnt As Long
        Dim TimeLineMod As Long
        Dim dispStr As String

        cmdDisplay("Waiting for stabilization measurement.")

        TmpTime = Val(txbWaitDelay.Text)
        TimeLineMod = TmpTime Mod TimeLine

        If TmpTime >= TimeLine Then
            TimeLineCnt = (TmpTime - TimeLineMod) / TimeLine
        End If

        dispStr = (Today + " " + TimeOfDay + " " + "Start waiting time.")
        txbCmdWindowP2.AppendText(dispStr)
        txbCmdWindowP2.Refresh()

        For cnt = 1 To TimeLineCnt
            DelayMs(TimeLine)
            dispStr = (".")
            If cnt Mod 2 Then
                If txbJudge.BackColor = Color.Blue Then
                    txbJudge.BackColor = Color.Gray
                Else
                    txbJudge.BackColor = Color.Blue
                End If
                txbJudge.Refresh()
            End If
            txbCmdWindowP2.AppendText(dispStr)
            txbCmdWindowP2.Refresh()
        Next

        txbCmdWindowP2.AppendText(vbCrLf)
        txbCmdWindowP2.Refresh()

        DelayMs(TimeLineMod)
    End Sub

    Private Function MeasureTime(StartEnd As Boolean) As Long
        Static Dim nStartTick As Long
        Static Dim nEndTick As Long

        If StartEnd = MeasSTART Then
            nStartTick = GetTickCount()
            Return 0
        Else
            nEndTick = GetTickCount()
            Return (nEndTick - nStartTick)
        End If
    End Function

    Private Sub cmdSerial_Click(sender As Object, e As EventArgs) Handles cmdSerial.Click
        LoadComportName()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Try
            If cmdSerial.Text = "닫기" Then
                cmdSerialPort.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        exit_Process()
    End Sub

    Private Sub TD_JIG_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        txbPV_SN.Focus()
    End Sub

    Private Sub txbPV_SN_Click(sender As Object, e As EventArgs) Handles txbPV_SN.Click
        If DevNum = 3 Then
            gpbProduct2.Focus()
        Else
            txbPV_SN.Clear()
        End If
    End Sub

    Private Sub txbGrade_GotFocus(sender As Object, e As EventArgs) Handles txbGrade.GotFocus
        txbPV_SN.Focus()
    End Sub

    Private Sub txbCapacitor_GotFocus(sender As Object, e As EventArgs) Handles txbCapacitor.GotFocus
        If DevNum = 3 Then
            gpbProduct2.Focus()
        Else
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub txbPV_Date_GotFocus(sender As Object, e As EventArgs) Handles txbPV_Date.GotFocus
        If DevNum = 3 Then
            gpbProduct2.Focus()
        Else
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub txbPV_Freq_GotFocus(sender As Object, e As EventArgs) Handles txbPV_Freq.GotFocus
        If DevNum = 1 Then
            If chbAutoMeasure.Checked Then
                txbPV_SN.Focus()
            End If
        ElseIf DevNum = 2 Then

        ElseIf DevNum = 3 Then
            If chbAutoMeasure.Checked Then
                txbPV_SN.Focus()
            End If
        ElseIf DevNum = 4 Then
            If chbAutoMeasure.Checked Then
                txbPV_SN.Focus()
            End If
        Else

            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub txbPV_Z_GotFocus(sender As Object, e As EventArgs) Handles txbPV_Z.GotFocus
        If DevNum = 3 Then
            gpbProduct2.Focus()
        Else
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub txbPV_C_GotFocus(sender As Object, e As EventArgs) Handles txbPV_C.GotFocus
        If DevNum = 3 Then
            gpbProduct2.Focus()
        Else
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub txbPV_R_GotFocus(sender As Object, e As EventArgs) Handles txbPV_R.GotFocus
        If DevNum = 3 Then
            gpbProduct2.Focus()
        Else
            txbPV_SN.Focus()
        End If
    End Sub
    Private Sub chbUseBarcode_GotFocus(sender As Object, e As EventArgs) Handles chbUseBarcode.GotFocus
        txbPV_SN.Focus()
    End Sub

    Private Function ParsingData(CmdInclude As Boolean) As String
        Dim cmdStr As String = "0"
        Dim cmdVal(0 To 6) As String
        Dim ReturnStr As String
        Dim i As Integer

        Try
            If rxMessage <> "" Then
                cmdMessage = ""

                If Mid(rxMessage, 1, 1) = "[" Then
                    cmdMessage = Mid(rxMessage, 2, Len(rxMessage))

                    cmdStr = Mid(cmdMessage, 1, (InStr(rxMessage, " ") - 2))
                    cmdMessage = Mid(rxMessage, InStr(rxMessage, " ") + 1, Len(cmdMessage))

                    For i = 1 To 6
                        If InStr(cmdMessage, ",") <> 0 Then
                            cmdVal(i) = Mid(cmdMessage, 1, InStr(cmdMessage, ",") - 1)
                            cmdMessage = Mid(cmdMessage, InStr(cmdMessage, ",") + 1, Len(cmdMessage))
                        ElseIf InStr(cmdMessage, "]") <> 0 Then
                            cmdVal(i) = Mid(cmdMessage, 1, InStr(cmdMessage, "]") - 1)
                            'cmdMessage = Mid(cmdMessage, InStr(cmdMessage, "]") + 1, Len(cmdMessage))
                        Else
                            Exit For
                        End If
                    Next i
                    rxMessage = ""

                    If CmdInclude = True Then
                        ReturnStr = cmdStr & ","
                    End If

                    Select Case cmdStr
                        Case "ctabroad"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartauth"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "carttdsn"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "tshotcnt"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rshotcnt"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setfreq"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setmfreq"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setsvfrq"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "getrdfrq"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "runshot"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setrlsel"
                            cmdStr = (cmdVal(1) & "," & cmdVal(2))
                            Return cmdStr
                        Case "setrlone"
                            cmdStr = (cmdVal(1) & "," & cmdVal(2))
                            Return cmdStr
                        Case "rshdelay"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "runbeep"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "getctcon"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartsn"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rdctconn"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartfreq"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartmanu"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "ctmodel"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "carttype"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "sethertz"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setdtype"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setctype"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setontim"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "ctinitif"
                            cmdStr = (cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5) & "," & cmdVal(6))
                            Return cmdStr
                        Case "cartinfo"
                            cmdStr = (cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5) & "," & cmdVal(6))
                            Return cmdStr
                        Case Else
                            Return "Else"
                            rxMessage = ""
                    End Select
                End If
                rxMessage = ""
                Return True
            Else
                'txbFreq.Text = "Else"
                Return False
            End If

        Catch ex As Exception
            rxMessage = ""
            Return Err.Description
        End Try
    End Function

    Private Function PollingReceiveData(RdTimeout As Long) As String
        Try
            If cmdSerialPort.IsOpen = True Then

                'cmdDisplay("Waiting for receive data.")

                rxMessage = ""
                cmdSerialPort.ReadTimeout = RdTimeout
                rxMessage = cmdSerialPort.ReadLine()
                Return ParsingData(False)

            Else
                cmdDisplay("통신 포트가 열리지 않았습니다.")
                ckbSerialUse.Checked = False
                Return True
            End If
        Catch ex As Exception
            rxMessage = ""
            Return Err.Description
        End Try
    End Function

    Private Sub SetFrequency(cmdStr As String)
        tmpCmdMessage = "setmfreq"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)
        PollingReceiveData(300)
    End Sub
    Function SetFrequencyReturn(cmdStr As String) As Boolean
        Dim retVal As String

        tmpCmdMessage = "setmfreq"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)

        retVal = PollingReceiveData(300)

        If retVal = cmdStr Then
            Return False
        Else
            Return True
        End If
    End Function
    Function SetCartType(cmdStr As String) As Boolean
        Dim retVal As String

        tmpCmdMessage = "setctype"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)

        retVal = PollingReceiveData(500)

        If retVal = cmdStr Then
            Return False
        Else
            Return True
        End If
    End Function

    Function SetDepthType(cmdStr As String) As Boolean
        Dim retVal As String

        tmpCmdMessage = "setdtype"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)

        retVal = PollingReceiveData(500)

        If retVal = cmdStr Then
            Return False
        Else
            Return True
        End If
    End Function
    Function SetHertzType(cmdStr As String) As Boolean
        Dim retVal As String

        tmpCmdMessage = "sethertz"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)

        retVal = PollingReceiveData(500)

        If retVal = cmdStr Then
            Return False
        Else
            Return True
        End If
    End Function
    Private Function SetBeep(cmdStr As String) As Boolean
        tmpCmdMessage = "runbeep"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        If SendData(cmdMessage) Then
            Return True
        End If
        PollingReceiveData(300)
        Return False
    End Function

    Private Sub SetRunShotDelay(cmdStr As String)
        tmpCmdMessage = "rshdelay"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)
    End Sub
    Function SetRelay(ch As Integer, OnOff As Integer) As Boolean
        Dim retVal As String
        Dim sendVal As String

        sendVal = ch & "," & OnOff
        tmpCmdMessage = "setrlone"
        cmdMessage = "[" & tmpCmdMessage & " " & Str(ch) & "," & Str(OnOff) & "]" + vbCrLf
        SendData(cmdMessage)
        retVal = PollingReceiveData(300)

        If retVal = sendVal Then
            Return False
        Else
            Return True
        End If
    End Function
    Function setOnTime(cmdStr As String) As Boolean
        Dim retVal As String

        tmpCmdMessage = "setontim"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)

        retVal = PollingReceiveData(500)

        If retVal = cmdStr Then
            Return False
        Else
            Return True
        End If
    End Function
    Private Sub RunShot(cmdStr As String)
        tmpCmdMessage = "runshot"
        cmdMessage = "[" & tmpCmdMessage & " " & cmdStr & "]" + vbCrLf
        SendData(cmdMessage)
    End Sub
    Private Function RunMeasure() As String
        Dim tmpVal As Double
        Dim cnt As Long
        Dim MeasureStr As String

        If rdbAvg.Checked Then
            MeasureStr = MethodAVG
        ElseIf rdbMax.Checked Then
            MeasureStr = MethodMAX
        Else
            MeasureStr = MethodTOP
        End If

        For cnt = 1 To 3
            strQueryResult = DoQueryString(":TER?")
            If strQueryResult = 1 Then
                If DoQueryString(":OPERegister:CONDition?") = 32 Then

                    strQueryResult = DoQueryString(MeasureStr)
                    tmpVal = Format(Val(strQueryResult), "0.000")

                    If tmpVal > 10 Then
                        Return "Over"
                    Else
                        Return tmpVal
                    End If
                End If
            End If
        Next

        Return "Fail"
    End Function

    Function MesUpdateDB() As Boolean
        Dim CntCmd As String = "IQC_TotalCnt='" & txbTotalCnt.Text & "',IQC_OkCnt='" & txbOKCnt.Text & "',IQC_NgCnt='" & txbNGCnt.Text & "',IQC_ProCnt='" & txbProCnt.Text &
                               "',IQC_LuxCnt='" & txbLuxCnt.Text & "',IQC_EyeCnt='" & txbEyeCnt.Text & "',IQC_R1Cnt='" & txbR1CntIQC.Text & "',IQC_R2Cnt='" & txbR2CntIQC.Text &
                               "',IQC_R3Cnt='" & txbR3CntIQC.Text & "',IQC_R4Cnt='" & txbR4CntIQC.Text & "',IQC_R5Cnt='" & txbR5CntIQC.Text & "',IQC_R6Cnt='" & txbR6CntIQC.Text &
                               "',IQC_R7Cnt='" & txbR7CntIQC.Text & "',IQC_R8Cnt='" & txbR8CntIQC.Text & "',IQC_R9Cnt='" & txbR9CntIQC.Text & "',IQC_R10Cnt='" & txbR10CntIQC.Text &
                               "',IQC_ReTotalCnt='" & txbReTotalCnt.Text & "',IQC_ReOkCnt='" & txbReOkCnt.Text & "',IQC_ReNgCnt='" & txbReNgCnt.Text & "'"

        cmd = "update jomtMES_IQCTbl set " & CntCmd & " where TesterDate = '" & getNowDate() & "';"
        DBCommand(cmd)

        Return True
    End Function

    Private Sub TransDB_PD2()
        Dim TestStep As Integer
        Dim UpdateSetDataStr As String

        TestStep = 31

        UpdateSetDataStr = "TestStep='" & TestStep & "',P2_Date='" & getNowDate() & "',P2_Time='" & getNowTime() & "',P2_Frequency='" & txbPV_Freq.Text & "',P2_Current='" & PD2_Current & "'"
        cmd = "update jomtCartridgeTbl set " & UpdateSetDataStr & " where PV_SN ='" & txbPV_SN.Text & "';"
        DBCommand(cmd)

        cmdDisplay("Save Measure DB complete!")
    End Sub

    Private Sub UploadCntDB_IQC()
        Dim TodayDate As String
        Dim CntCmd As String = "IQC_TotalCnt='" & RegMesIQC.IQC_TotalCnt & "',IQC_OkCnt='" & RegMesIQC.IQC_OKCnt & "',IQC_NgCnt='" & RegMesIQC.IQC_NGCnt & "',IQC_ProCnt='" & RegMesIQC.IQC_GradeCnt(MODEL_NAME.PRO) &
                               "',IQC_LuxCnt='" & RegMesIQC.IQC_GradeCnt(MODEL_NAME.LUX) & "',IQC_EyeCnt='" & RegMesIQC.IQC_GradeCnt(MODEL_NAME.EYE) & "',IQC_R1Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL1) & "',IQC_R2Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL2) &
                               "',IQC_R3Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL3) & "',IQC_R4Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL4) & "',IQC_R5Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL5) & "',IQC_R6Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL6) &
                               "',IQC_R7Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL7) & "',IQC_R8Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL8) & "',IQC_R9Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL9) & "',IQC_R10Cnt='" & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL10) &
                               "',IQC_ReTotalCnt='" & RegMesIQC.IQC_ReTotalCnt & "',IQC_ReOkCnt='" & RegMesIQC.IQC_ReOkCnt & "',IQC_ReNgCnt='" & RegMesIQC.IQC_ReNgCnt & "'"
        If ShowDetailLog Then cmdDisplay("Enter the 'UploadCntDB_IQC' processor")
        TodayDate = getNowDate()
        cmd = "update jomtMES_IQCTbl set " & CntCmd & " where IQC_Date = '" & TodayDate & "';"
        DBCommand(cmd)
    End Sub

    Private Sub UploadCntDB_PD1()
        Dim TodayDate As String
        Dim CntCmd As String = "PD1_TotalCnt='" & RegMesPD1.PD1_TotalCnt & "',PD1_OkCnt='" & RegMesPD1.PD1_OKCnt & "',PD1_NgCnt='" & RegMesPD1.PD1_NGCnt & "',PD1_ProCnt='" & RegMesPD1.PD1_GradeCnt(MODEL_NAME.PRO) &
                               "',PD1_LuxCnt='" & RegMesPD1.PD1_GradeCnt(MODEL_NAME.LUX) & "',PD1_EyeCnt='" & RegMesPD1.PD1_GradeCnt(MODEL_NAME.EYE) & "',PD1_MAXCnt='" & RegMesPD1.PD1_GradeCnt(MODEL_NAME.MAX) & "',PD1_R1Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL1) & "',PD1_R2Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL2) &
                               "',PD1_R3Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL3) & "',PD1_R4Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL4) & "',PD1_R5Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL5) & "',PD1_R6Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL6) &
                               "',PD1_R7Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL7) & "',PD1_R8Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL8) & "',PD1_R9Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL9) & "',PD1_R10Cnt='" & RegMesPD1.PD1_CapCnt(RELAY_NUM.RL10) & "'"

        If ShowDetailLog Then cmdDisplay("Enter the 'UploadCntDB_PD1' processor")
        TodayDate = getNowDate()
        cmd = "update jomtMES_PD1Tbl set " & CntCmd & " where PD1_Date = '" & TodayDate & "';"
        DBCommand(cmd)
    End Sub

    Private Function SaveLocalCount() As Boolean
        Try
            Dim TodayDate As String = getNowDate()
            Dim MonthToday As String = Format(Now, "yyyy-MM")
            Dim StartForm As String = "TestDate,FullCount,OKCount,NGCount,PROCount,LuxCount,EyeCount,MaxCount,R1Count,R2Count,R3Count,R4Count,R5Count,R6Count,R7Count,R8Count,R9Count,R10Count"
            Dim FilePath As String = System.AppDomain.CurrentDomain.BaseDirectory & "\LocalCount"
            NewDir(FilePath)

            Dim FileName As String = FilePath & "\" & MonthToday & ".csv"

            Dim SaveData As String = TodayDate & "," & RegLocalPD1.PD1_TotalCnt & "," & RegLocalPD1.PD1_OKCnt & "," & RegLocalPD1.PD1_NGCnt & "," & RegLocalPD1.PD1_GradeCnt(MODEL_NAME.PRO) & "," &
            RegLocalPD1.PD1_GradeCnt(MODEL_NAME.LUX) & "," & RegLocalPD1.PD1_GradeCnt(MODEL_NAME.EYE) & "," & RegLocalPD1.PD1_GradeCnt(MODEL_NAME.MAX) & "," & RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL1) & "," &
            RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL2) & "," & RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL3) & "," & RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL4) & "," & RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL5) & "," &
            RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL6) & "," & RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL7) & "," & RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL8) & "," & RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL9) & "," &
            RegLocalPD1.PD1_CapCnt(RELAY_NUM.RL10)

            Dim StrData As String = String.Empty

            If File.Exists(FileName) Then
                StrData = File.ReadAllText(FileName, Encoding.UTF8)

                If StrData = vbCrLf Or StrData = "" Or StrData = Nothing Or StrData = String.Empty Then
                    StrData = StartForm & vbCrLf & SaveData
                Else

                    If UBound(Split(StrData, TodayDate)) > 0 Then
                        StrData = Mid(StrData, 1, InStr(StrData, TodayDate) - 1)
                        StrData = StrData & SaveData & vbCrLf
                    Else
                        StrData = StrData & SaveData & vbCrLf
                    End If
                End If
            Else
                StrData = StartForm & vbCrLf & SaveData & vbCrLf
            End If

            File.WriteAllText(FileName, StrData, Encoding.UTF8)

            Return False
        Catch ex As Exception
            MsgBox("수량을 저장하는 과정에서 오류가 발생하였습니다!" & vbCrLf & ex.Message, vbCritical)
            Return True
        End Try
    End Function

    Private Function LoadLocalCount() As Boolean
        Try
            Dim TodayDate As String = getNowDate()
            Dim MonthToday As String = Format(Now, "yyyy-MM")
            Dim FilePath As String = System.AppDomain.CurrentDomain.BaseDirectory & "\LocalCount"
            NewDir(FilePath)

            Dim FileName As String = FilePath & "\" & MonthToday & ".csv"

            Dim StrData As String

            If File.Exists(FileName) Then
                StrData = File.ReadAllText(FileName, Encoding.UTF8)

                If UBound(Split(StrData, TodayDate)) > 0 Then
                    StrData = Mid(StrData, InStr(StrData, TodayDate), InStr(StrData, vbCrLf) - 1)
                    ViewCountSet(StrData)
                Else
                    CountReset()
                End If
            Else
                CountReset()
            End If

            Return False
        Catch ex As Exception
            MsgBox("수량을 불러오는 과정에서 오류가 발생하였습니다!" & vbCrLf & ex.Message, vbCritical)
            Return True
        End Try
    End Function

    Private Sub ViewCountSet(saveData As String)
        If UBound(Split(saveData, ",")) = 17 Then
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' 날짜 삭제

            txbTotalCntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' 전체 수량 삭제
            txbOkCntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' 합격 수량 삭제
            txbNgCntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' 불량 수량 삭제
            txbProCntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' PRO 수량 삭제
            txbLuxCntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' LUX 수량 삭제
            txbEyeCntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' EYE 수량 삭제
            txbMaxCntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' MAX 수량 삭제

            txbR1CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R1 수량 삭제
            txbR2CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R2 수량 삭제
            txbR3CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R3 수량 삭제
            txbR4CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R4 수량 삭제
            txbR5CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R5 수량 삭제
            txbR6CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R6 수량 삭제
            txbR7CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R7 수량 삭제
            txbR8CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R8 수량 삭제
            txbR9CntPD1.Text = Mid(saveData, 1, InStr(saveData, ",") - 1)
            saveData = Mid(saveData, InStr(saveData, ",") + 1, saveData.Length) '' R9 수량 삭제
            txbR10CntPD1.Text = Mid(saveData, 1, saveData.Length)
        Else
            CountReset()
            MsgBox("데이터를 불러오는데 오류가 발생하였습니다.", vbCritical)
        End If
    End Sub

    Private Sub CountReset()
        txbTotalCntPD1.Text = "0"
        txbOkCntPD1.Text = "0"
        txbNgCntPD1.Text = "0"
        txbProCntPD1.Text = "0"
        txbLuxCntPD1.Text = "0"
        txbEyeCntPD1.Text = "0"
        txbMaxCntPD1.Text = "0"
        txbR1CntPD1.Text = "0"
        txbR2CntPD1.Text = "0"
        txbR3CntPD1.Text = "0"
        txbR4CntPD1.Text = "0"
        txbR5CntPD1.Text = "0"
        txbR6CntPD1.Text = "0"
        txbR7CntPD1.Text = "0"
        txbR8CntPD1.Text = "0"
        txbR9CntPD1.Text = "0"
        txbR10CntPD1.Text = "0"
    End Sub

    Private Sub NewDir(FilePath As String)

        If Dir(FilePath, vbDirectory) = "" Then
            MkDir(FilePath)
        End If
    End Sub

    Private Sub GetCntDB_IQC()
        Dim QC_Date, QC_Grade, QC_Capacitor As String
        Dim TodayDate As String
        Dim RetCnt, gCnt, pCnt, rCnt As Integer

        Try
            If ShowDetailLog Then cmdDisplay("Enter the 'GetCntDB' processor")
            ResetCntRegIQC()
            TodayDate = getNowDate()

            If RegMeasure.PreTestStep = 1 Then '정상 일때
                CntRegMesIQC.IQC_TotalCnt += 1
                If RegMeasure.MeasureOkNg = "OK" Then

                    CntRegMesIQC.IQC_OKCnt += 1

                    gCnt = GradeToValue(RegMeasure.MeasureGrade)
                    If gCnt Then CntRegMesIQC.IQC_GradeCnt(gCnt) += 1
                    If RegMeasure.MeasureValue(0) Then
                        CntRegMesIQC.IQC_GdCapCnt(gCnt, RegMeasure.MeasureValue(0)) += 1
                    End If

                    RegMeasure.MeasureTestStep = 11

                ElseIf RegMeasure.MeasureOkNg = "NG" Then
                    RegMeasure.MeasureTestStep = 12
                    CntRegMesIQC.IQC_NGCnt += 1
                End If
            ElseIf RegMeasure.PreTestStep >= 11 And RegMeasure.PreTestStep <= 19 Then
                RetCnt = DBQueryReader("select QC_Date, QC_Grade, QC_Capacitor From jomtCartridgeTbl Where PV_SN = '" & RegMeasure.MeasurePV_SN & "';")

                If RetCnt Then
                    QC_Date = sqlDataQuery(0)
                    QC_Grade = sqlDataQuery(1)
                    QC_Capacitor = sqlDataQuery(2)

                    If QC_Date = TodayDate Then

                        If RegMeasure.MeasureOkNg = "OK" Then

                            If RegMeasure.PreTestStep = 12 Or RegMeasure.PreTestStep = 14 Then '이전 불량일때
                                CntRegMesIQC.IQC_OKCnt += 1
                                CntRegMesIQC.IQC_NGCnt -= 1

                                gCnt = GradeToValue(RegMeasure.MeasureGrade)
                                If gCnt Then CntRegMesIQC.IQC_GradeCnt(gCnt) += 1
                                If RegMeasure.MeasureValue(0) Then
                                    CntRegMesIQC.IQC_GdCapCnt(gCnt, RegMeasure.MeasureValue(0)) += 1
                                End If

                            ElseIf RegMeasure.PreTestStep = 11 Or RegMeasure.PreTestStep = 13 Then '이전 양품일때

                                gCnt = GradeToValue(RegMeasure.MeasureGrade)
                                pCnt = GradeToValue(QC_Grade)
                                If RegMeasure.MeasureGrade <> QC_Grade Then
                                    If gCnt Then CntRegMesIQC.IQC_GradeCnt(gCnt) += 1
                                    If pCnt Then CntRegMesIQC.IQC_GradeCnt(pCnt) -= 1
                                End If

                                If RegMeasure.MeasureCapacitor <> QC_Capacitor Then
                                    If RegMeasure.MeasureValue(0) Then
                                        CntRegMesIQC.IQC_GdCapCnt(gCnt, RegMeasure.MeasureValue(0)) += 1
                                    End If
                                    For rCnt = 1 To 10
                                        If QC_Capacitor = RELAY_C(rCnt) Then
                                            CntRegMesIQC.IQC_GdCapCnt(pCnt, rCnt) -= 1
                                            Exit For
                                        End If
                                    Next
                                End If

                            End If
                            RegMeasure.MeasureTestStep = 11

                        ElseIf RegMeasure.MeasureOkNg = "NG" Then

                            If RegMeasure.PreTestStep = 11 Or RegMeasure.PreTestStep = 13 Then '이전 양품일때
                                CntRegMesIQC.IQC_OKCnt -= 1
                                CntRegMesIQC.IQC_NGCnt += 1

                                pCnt = GradeToValue(QC_Grade)
                                If pCnt Then CntRegMesIQC.IQC_GradeCnt(pCnt) -= 1
                                For rCnt = 1 To 10
                                    If QC_Capacitor = RELAY_C(rCnt) Then
                                        CntRegMesIQC.IQC_GdCapCnt(pCnt, rCnt) -= 1
                                        Exit For
                                    End If
                                Next
                            End If
                            RegMeasure.MeasureTestStep = 12

                        End If

                    Else  ' 재검사 측정

                        RetCnt = DBQueryReader("select QC_LastDate From jomtCartridgeTbl Where PV_SN = '" & RegMeasure.MeasurePV_SN & "';")
                        If RetCnt Then
                            RegMeasure.QC_LastDate = sqlDataQuery(0)
                        Else
                            RegMeasure.QC_LastDate = "NULL"
                        End If

                        If RegMeasure.QC_LastDate <> getNowDate() Then
                            CntRegMesIQC.IQC_ReTotalCnt += 1
                        End If

                        If RegMeasure.MeasureOkNg = "OK" Then

                            If RegMeasure.PreTestStep = 12 Or RegMeasure.PreTestStep = 14 Then '이전 불량일때
                                CntRegMesIQC.IQC_ReOkCnt += 1
                                CntRegMesIQC.IQC_ReNgCnt -= 1

                                gCnt = GradeToValue(RegMeasure.MeasureGrade)
                                If gCnt Then CntRegMesIQC.IQC_ReGradeCnt(gCnt) += 1
                                If RegMeasure.MeasureValue(0) Then
                                    CntRegMesIQC.IQC_ReGdCapCnt(gCnt, RegMeasure.MeasureValue(0)) += 1
                                End If

                            ElseIf RegMeasure.PreTestStep = 11 Or RegMeasure.PreTestStep = 13 Then '이전 양품일때

                                gCnt = GradeToValue(RegMeasure.MeasureGrade)
                                pCnt = GradeToValue(QC_Grade)
                                If RegMeasure.MeasureGrade <> QC_Grade Then
                                    If gCnt Then CntRegMesIQC.IQC_ReGradeCnt(gCnt) += 1
                                    If pCnt Then CntRegMesIQC.IQC_ReGradeCnt(pCnt) -= 1
                                End If

                                If RegMeasure.MeasureCapacitor <> QC_Capacitor Then
                                    If RegMeasure.MeasureValue(0) Then
                                        CntRegMesIQC.IQC_ReGdCapCnt(gCnt, RegMeasure.MeasureValue(0)) += 1
                                    End If
                                    For rCnt = 1 To 10
                                        If QC_Capacitor = RELAY_C(rCnt) Then
                                            CntRegMesIQC.IQC_ReGdCapCnt(pCnt, rCnt) -= 1
                                            Exit For
                                        End If
                                    Next
                                End If

                            End If
                            RegMeasure.MeasureTestStep = 13

                        ElseIf RegMeasure.MeasureOkNg = "NG" Then

                            If RegMeasure.PreTestStep = 11 Or RegMeasure.PreTestStep = 13 Then

                                CntRegMesIQC.IQC_ReOkCnt -= 1
                                CntRegMesIQC.IQC_ReNgCnt += 1

                                pCnt = GradeToValue(QC_Grade)
                                If pCnt Then CntRegMesIQC.IQC_ReGradeCnt(pCnt) -= 1
                                For rCnt = 1 To 10
                                    If QC_Capacitor = RELAY_C(rCnt) Then
                                        CntRegMesIQC.IQC_ReGdCapCnt(pCnt, rCnt) -= 1
                                        Exit For
                                    End If
                                Next
                            End If
                            RegMeasure.MeasureTestStep = 14
                        End If
                    End If
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub CalCntDB_IQC()
        If ShowDetailLog Then cmdDisplay("Enter the 'CalCntDB_IQC' processor")
        RegMesIQC.IQC_TotalCnt += CntRegMesIQC.IQC_TotalCnt
        RegMesIQC.IQC_OKCnt += CntRegMesIQC.IQC_OKCnt
        RegMesIQC.IQC_NGCnt += CntRegMesIQC.IQC_NGCnt

        For i = MODEL_NAME.PRO To MODEL_NAME.EYE
            RegMesIQC.IQC_GradeCnt(i) += CntRegMesIQC.IQC_GradeCnt(i)
        Next

        For n = RELAY_NUM.RL1 To RELAY_NUM.RL10
            For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                RegMesIQC.IQC_CapCnt(n) += CntRegMesIQC.IQC_GdCapCnt(i, n)
            Next
        Next

        RegMesIQC.IQC_ReTotalCnt += CntRegMesIQC.IQC_ReTotalCnt
        RegMesIQC.IQC_ReOkCnt += CntRegMesIQC.IQC_ReOkCnt
        RegMesIQC.IQC_ReNgCnt += CntRegMesIQC.IQC_ReNgCnt

        If CntRegMesIQC.IQC_ReOkCnt Or CntRegMesIQC.IQC_ReNgCnt Then
            If RegMeasure.QC_LastDate <> getNowDate() Then
                If RegMeasure.MeasureOkNg = "OK" Then
                    RegMesIQC.IQC_ReNgCnt += 1
                ElseIf RegMeasure.MeasureOkNg = "NG" Then
                    RegMesIQC.IQC_ReOkCnt += 1
                End If
            End If
        End If


    End Sub
    Private Sub CalRegProductIQC()
        If ShowDetailLog Then cmdDisplay("Enter the 'CalRegProductIQC' processor")

        RegMesProduct.IQC_TotalCnt += CntRegMesIQC.IQC_TotalCnt
        RegMesProduct.PV_TDCnt -= CntRegMesIQC.IQC_TotalCnt
        RegMesProduct.IQC_OkCnt += (CntRegMesIQC.IQC_OKCnt + CntRegMesIQC.IQC_ReOkCnt)
        RegMesProduct.IQC_NgCnt += (CntRegMesIQC.IQC_NGCnt + CntRegMesIQC.IQC_ReNgCnt)

        For i = MODEL_NAME.PRO To MODEL_NAME.EYE
            RegMesProduct.IQC_GradeCnt(i) += (CntRegMesIQC.IQC_GradeCnt(i) + CntRegMesIQC.IQC_ReGradeCnt(i))
        Next

        For i = MODEL_NAME.PRO To MODEL_NAME.EYE
            For n = RELAY_NUM.RL1 To RELAY_NUM.RL5
                RegMesProduct.IQC_GdCapCnt(i, n) += (CntRegMesIQC.IQC_GdCapCnt(i, n) + CntRegMesIQC.IQC_ReGdCapCnt(i, n))
            Next
        Next
    End Sub

    Private Sub LoadTextIQC()
        Try
            txbTotalCnt.Text = RegMesIQC.IQC_TotalCnt
            txbTotalCnt.Refresh()
            txbOKCnt.Text = RegMesIQC.IQC_OKCnt
            txbOKCnt.Refresh()
            txbNGCnt.Text = RegMesIQC.IQC_NGCnt
            txbNGCnt.Refresh()

            For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                txbIQC_GCnt(i).Text = RegMesIQC.IQC_GradeCnt(i)
                txbIQC_GCnt(i).Refresh()
            Next

            For i = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM - 1
                txbIQC_RCnt(i).Text = RegMesIQC.IQC_CapCnt(i)
                txbIQC_RCnt(i).Refresh()
            Next

            txbReTotalCnt.Text = RegMesIQC.IQC_ReTotalCnt
            txbReTotalCnt.Refresh()
            txbReOkCnt.Text = RegMesIQC.IQC_ReOkCnt
            txbReOkCnt.Refresh()
            txbReNgCnt.Text = RegMesIQC.IQC_ReNgCnt
            txbReNgCnt.Refresh()

        Catch ex As Exception
            cmdDisplay(Err.Description)
        End Try
    End Sub

    Private Sub LoadTextPDLocal1()
        RegLocalPD1.PD1_TotalCnt = Val(txbTotalCntPD1.Text)
        RegLocalPD1.PD1_OKCnt = Val(txbOkCntPD1.Text)
        RegLocalPD1.PD1_NGCnt = Val(txbNgCntPD1.Text)

        For i = MODEL_NAME.PRO To MODEL_NAME.MAX
            RegLocalPD1.PD1_GradeCnt(i) = txbPD1_GCnt(i).Text
        Next

        For i = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM - 1
            RegLocalPD1.PD1_CapCnt(i) = txbPD1_RCnt(i).Text
        Next
    End Sub

    Private Sub LoadTextPD1()
        Try
            txbTotalCntPD1.Text = RegMesPD1.PD1_TotalCnt
            txbTotalCntPD1.Refresh()
            txbOkCntPD1.Text = RegMesPD1.PD1_OKCnt
            txbOkCntPD1.Refresh()
            txbNgCntPD1.Text = RegMesPD1.PD1_NGCnt
            txbNgCntPD1.Refresh()

            For i = MODEL_NAME.PRO To MODEL_NAME.MAX
                txbPD1_GCnt(i).Text = RegMesPD1.PD1_GradeCnt(i)
                txbPD1_GCnt(i).Refresh()
            Next

            For i = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM - 1
                txbPD1_RCnt(i).Text = RegMesPD1.PD1_CapCnt(i)
                txbPD1_RCnt(i).Refresh()
            Next

        Catch ex As Exception
            cmdDisplay(Err.Description)
        End Try
    End Sub
    Private Sub TesterLogTransDB()
        Dim TestStep As Integer
        Dim UpdateSetDataStr As String

        TestStep = 11
        UpdateSetDataStr = "TestStep='" & TestStep & "',QC_DateTime='" & getNowDateTime() & "',QC_Frequency='" & txbPV_Freq.Text & "',QC_Current='" & RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) & "',QC_Capacitor='" & txbCapacitor.Text & "',QC_Grade='" & txbGrade.Text & "',QC_R1Value='" & RegMeasure.MeasureValue(1) & "',QC_R2Value='" & RegMeasure.MeasureValue(2) & "',QC_R3Value='" & RegMeasure.MeasureValue(3) & "',QC_R4Value='" & RegMeasure.MeasureValue(4) & "',QC_R5Value='" & RegMeasure.MeasureValue(5) & "',QC_R6Value='" & RegMeasure.MeasureValue(6) & "',QC_R7Value='" & RegMeasure.MeasureValue(7) & "',QC_R8Value='" & RegMeasure.MeasureValue(8) & "',QC_R9Value='" & RegMeasure.MeasureValue(9) & "',QC_R10Value='" & RegMeasure.MeasureValue(10) & "'"
        cmd = "update jomtCartridgeTbl set " & UpdateSetDataStr & " where PV_SN ='" + txbPV_SN.Text + "';"
        DBCommand(cmd)

        cmdDisplay("Save Measure DB complete!")
    End Sub

    Private Sub IQCValueStandard(testType As Boolean) '' true : DIT / false : J
        If testType = True Then
            ProMAX = txbProMax.Text
            ProTYP = txbProTyp.Text
            ProMIN = txbProMin.Text

            LuxMAX = txbLuxMax.Text
            LuxTYP = txbLuxTyp.Text
            LuxMIN = txbLuxMin.Text

            EyeMAX = txbEyeMax.Text
            EyeTYP = txbEyeTyp.Text
            EyeMIN = txbEyeTyp.Text
        Else
            ProMAX = txbKProMax.Text
            ProTYP = txbKProTyp.Text
            ProMIN = txbKProMin.Text

            LuxMAX = txbKLuxMax.Text
            LuxTYP = txbKLuxTyp.Text
            LuxMIN = txbKLuxMin.Text

            EyeMAX = txbKEyeMax.Text
            EyeTYP = txbKEyeTyp.Text
            EyeMIN = txbKEyeTyp.Text
        End If
    End Sub

    Private Sub RunProcessIQC()
        Try
            Dim RetStr As String
            Dim SaveLogStr As String
            Dim StatusFail As Boolean = False

            If (Len(RegMeasure.MeasurePV_SN) = LenPVSN And (Mid(RegMeasure.MeasurePV_SN, 1, 3) = NamePVSN Or Mid(RegMeasure.MeasurePV_SN, 1, 1) = NamePVSN2)) Or chbAutoMeasure.Checked = False Or CalModeOn = True And (txbPV_Freq.Text <> "N/A" And txbPV_Freq.Text <> "") Then

                If Mid(RegMeasure.MeasurePV_SN, 1, 3) = NamePVSN Then
                    IQCValueStandard(True)
                Else
                    IQCValueStandard(False)
                End If


                cmdDisplay("Start measurement.")
                SetDefaultText()

                txbGrade.BackColor = DefaultBackColor
                txbGrade.Text = "측정중"
                txbGrade.Refresh()

                txbCapacitor.BackColor = DefaultBackColor
                txbCapacitor.Text = "측정중"
                txbCapacitor.Refresh()

                btnRunIQC.Enabled = False
                btnRunIQC.Refresh()

                MeasureTime(MeasSTART)

                If SetBeep(SoundStart) Then
                    txbGrade.BackColor = DefaultBackColor
                    txbGrade.Text = "대기"
                    txbGrade.Refresh()

                    txbCapacitor.BackColor = DefaultBackColor
                    txbCapacitor.Text = "대기"
                    txbCapacitor.Refresh()

                    txbRun.Text = "통신 에러"
                    txbRun.BackColor = Color.Red
                    txbRun.Refresh()
                    Exit Sub
                End If

                myScope.IO.Clear() ' Clear the interface.
                myScope.IO.Timeout = Val(txbTimeout.Text) ' Set I/O communication timeout.

                RetStr = DoCommand("*CLS")

                If RetStr = "Fail" Then
                    StatusFail = True
                    If ShowDetailLog Then
                        txbRun.Text = "Scope Error 01" 'cmdDisplay("Scope clear failure")
                        txbRun.BackColor = Color.Red
                        txbRun.Refresh()
                    End If
                    GoTo MEASURE_END
                End If
                If ShowDetailLog Then cmdDisplay("Scope clear complete")

                SetFrequency(txbPV_Freq.Text)
                If ShowDetailLog Then cmdDisplay("Frequency : " & txbPV_Freq.Text & " kHz")

                IQCValueError = 0

                '더미 샷
                SetRelay(1, 1)

                For Cnt = 1 To Val(txbIQC_DummyShotCnt.Text)
                    If ShowDetailLog Then cmdDisplay("Run dummy measure sequence[" & Cnt & "]")
                    RetStr = DoCommand(":Single")
                    If RetStr = "Fail" Then
                        StatusFail = True
                        If ShowDetailLog Then cmdDisplay("Trigger set failure")
                        If ShowDetailLog Then
                            txbRun.Text = "Scope Error 02" 'cmdDisplay("Trigger set failure")
                            txbRun.BackColor = Color.Red
                            txbRun.Refresh()
                        End If
                        Exit For
                    End If
                    If ShowDetailLog Then cmdDisplay("Waiting for trigger")

                    If ShowDetailLog Then cmdDisplay("Run dummy shot[" & txbOntime.Text & " ms]")
                    RunShot(txbOntime.Text)
                    PollingReceiveData(300)

                    DelayMs(Val(txbOntime.Text) + 50)

                    RetStr = RunMeasure()
                    If IsNumeric(RetStr) = False Or RetStr = "Fail" Then
                        StatusFail = True
                        txbRun.Text = "Scope Error 03" 'cmdDisplay("Trigger failure")
                        txbRun.BackColor = Color.Red
                        txbRun.Refresh()
                        If ShowDetailLog Then cmdDisplay("Trigger failure")
                        Exit For
                    End If

                    Dim tmpMeasureValue As Double = Val(RetStr)

                    If tmpMeasureValue < Val(txbMinValue.Text) Then
                        StatusFail = True
                        txbRun.Text = "최소전류미달" 'cmdDisplay("Trigger failure")
                        txbRun.BackColor = Color.Red
                        txbRun.Refresh()
                        Exit For
                    End If

                    If Abs(tmpMeasureValue - IQCValueError) < Val(txbValueError.Text) Then
                        IQCValueError = tmpMeasureValue
                    Else
                        If IQCValueError = 0 Then
                            IQCValueError = tmpMeasureValue
                        Else
                            StatusFail = True
                            txbRun.Text = "연결불량" 'cmdDisplay("Trigger failure")
                            txbRun.BackColor = Color.Red
                            txbRun.Refresh()
                            Exit For
                        End If
                    End If

                    cmdDisplay("Measurement[D" & Cnt & "] : " & Format(tmpMeasureValue, "0.000") & " A")
                    DelayMs(Val(txbMeasureDelay.Text))
                Next

                '측정 샷
                ResetRegMeasureValue()

                For Cnt = 1 To RelayCnt.Length - 1
                    If ShowDetailLog Then cmdDisplay("Run measure sequence[" & Cnt & "]")
                    SetRelay(RelayCnt(Cnt), 1)
                    If ShowDetailLog Then cmdDisplay("Turn on relay[" & Cnt & "]")
                    RetStr = DoCommand(":Single")
                    If RetStr = "Fail" Then
                        StatusFail = True
                        txbRun.Text = "Scope Error 04" 'cmdDisplay("Trigger set failure")
                        txbRun.BackColor = Color.Red
                        txbRun.Refresh()
                        If ShowDetailLog Then cmdDisplay("Trigger set failure")
                        Exit For
                    End If
                    If ShowDetailLog Then cmdDisplay("Waiting for trigger")

                    If ShowDetailLog Then cmdDisplay("Run shot[" & txbOntime.Text & " ms]")
                    RunShot(txbOntime.Text)
                    PollingReceiveData(300)

                    DelayMs(Val(txbOntime.Text) + 50)

                    RetStr = RunMeasure()
                    If IsNumeric(RetStr) = False Or RetStr = "Fail" Then
                        StatusFail = True
                        txbRun.Text = "Scope Error 03" 'cmdDisplay("Trigger failure")
                        txbRun.BackColor = Color.Red
                        txbRun.Refresh()
                        If ShowDetailLog Then cmdDisplay("Trigger failure")
                        Exit For
                    End If
                    RegMeasure.MeasureValue(RelayCnt(Cnt)) = Val(RetStr)

                    If RegMeasure.MeasureValue(RelayCnt(Cnt)) < Val(txbMinValue.Text) Then
                        StatusFail = True
                        txbRun.Text = "최소전류미달" 'cmdDisplay("Trigger failure")
                        txbRun.BackColor = Color.Red
                        txbRun.Refresh()
                        Exit For
                    End If

                    If Abs(RegMeasure.MeasureValue(RelayCnt(Cnt)) - IQCValueError) < Val(txbValueError.Text) Then
                        IQCValueError = RegMeasure.MeasureValue(RelayCnt(Cnt))
                    Else
                        StatusFail = True
                        txbRun.Text = "연결불량" 'cmdDisplay("Trigger failure")
                        txbRun.BackColor = Color.Red
                        txbRun.Refresh()
                        Exit For
                    End If

                    cmdDisplay("Measurement[R" & Cnt & "] : " & Format(RegMeasure.MeasureValue(RelayCnt(Cnt)), "0.000") & " A")
                    DelayMs(Val(txbMeasureDelay.Text))
                Next

MEASURE_END:

                DoCommand(":Run")
                SetBeep(SoundEnd)
                cmdDisplay("End measurement(Run Times : " + Str(MeasureTime(MeasEND)) + " ms).")

                If ShowDetailLog Then cmdDisplay("Judgment start")

                If StatusFail Then

                    RegMeasure.MeasureOkNg = "NG"
                    RegMeasure.MeasureGrade = "FAIL"
                    txbGrade.BackColor = Color.Red
                    txbCapacitor.BackColor = Color.Red
                    txbGrade.Text = "FAIL"
                    txbCapacitor.Text = "FAIL"
                    txbGrade.Refresh()
                    txbCapacitor.Refresh()

                    Exit Sub
                Else
                    RegMeasure.MeasureValue(0) = 0

                    If Mid(RegMeasure.MeasurePV_SN, 1, 2) = "J3" Then
                        For Cnt = 1 To RelayCnt.Length - 1
                            If RegMeasure.MeasureValue(RelayCnt(Cnt)) <= EyeMAX And RegMeasure.MeasureValue(0) = 0 Then RegMeasure.MeasureValue(0) = Cnt

                            If RegMeasure.MeasureValue(0) Then
                                If RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) < RegMeasure.MeasureValue(RelayCnt(Cnt)) Then
                                    If RegMeasure.MeasureValue(RelayCnt(Cnt)) <= EyeMAX Then
                                        RegMeasure.MeasureValue(0) = RelayCnt(Cnt)
                                    End If
                                End If
                            End If
                        Next
                    Else
                        For Cnt = 1 To RelayCnt.Length - 1
                            If RegMeasure.MeasureValue(RelayCnt(Cnt)) <= ProMAX And RegMeasure.MeasureValue(0) = 0 Then RegMeasure.MeasureValue(0) = Cnt

                            If RegMeasure.MeasureValue(0) Then
                                If RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) < RegMeasure.MeasureValue(RelayCnt(Cnt)) Then
                                    If RegMeasure.MeasureValue(RelayCnt(Cnt)) <= ProMAX Then
                                        RegMeasure.MeasureValue(0) = RelayCnt(Cnt)
                                    End If
                                End If
                            End If
                        Next
                    End If

                    If RegMeasure.MeasureValue(0) = 0 Then
                        RegMeasure.MeasureOkNg = "NG"
                        RegMeasure.MeasureGrade = "OVER"
                        txbGrade.Text = RegMeasure.MeasureGrade
                        RegMeasure.MeasureValue(0) = 0
                        txbGrade.BackColor = Color.Red
                    ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= ProMIN Then
                        RegMeasure.MeasureOkNg = "OK"
                        RegMeasure.MeasureGrade = "PRO"
                        txbGrade.Text = RegMeasure.MeasureGrade
                        txbGrade.BackColor = Color.Green
                    ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= LuxMIN Then
                        RegMeasure.MeasureOkNg = "OK"
                        RegMeasure.MeasureGrade = "LUX"
                        txbGrade.Text = RegMeasure.MeasureGrade
                        txbGrade.BackColor = Color.Green
                    ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= EyeMIN Then
                        RegMeasure.MeasureOkNg = "OK"
                        RegMeasure.MeasureGrade = "EYE"
                        txbGrade.Text = RegMeasure.MeasureGrade
                        txbGrade.BackColor = Color.Green
                    Else
                        RegMeasure.MeasureOkNg = "NG"
                        RegMeasure.MeasureGrade = "UNDER"
                        txbGrade.Text = RegMeasure.MeasureGrade
                        RegMeasure.MeasureValue(0) = 0
                        txbGrade.BackColor = Color.Red
                    End If

                    txbGrade.Refresh()

                    RegMeasure.MeasureCapacitor = RELAY_C(RegMeasure.MeasureValue(0))
                    txbCapacitor.Text = RegMeasure.MeasureCapacitor

                    If RegMeasure.MeasureValue(0) Then
                        txbCapacitor.BackColor = Color.Green
                    Else
                        txbCapacitor.BackColor = Color.Red
                    End If

                    txbRun.Text = "측정완료"
                    txbRun.BackColor = Color.Green
                    txbCapacitor.Refresh()
                End If

                If CalModeOn = False Then
                    If chbAutoMeasure.Checked Then

                        txbRun.Text = "DB등록중"
                        txbRun.BackColor = Color.DarkOrange

                        GetCntDB_IQC()         'TD 측정 수량 집계
                        If UploadDB_IQC() Then  'TD 측정 Data DB 저장
                            cmdDisplay("Save Measure DB failure!")
                            txbRun.Text = "DB등록실패"
                            txbRun.BackColor = Color.DarkOrange
                        Else
                            cmdDisplay("Save Measure DB complete!")
                            txbRun.Text = "DB등록완료"
                            txbRun.BackColor = Color.Green
                        End If

                        CalCntDB_IQC()         'TD 측정 수량 계산
                        UploadCntDB_IQC()          'TD 측정 수량 및 테스트 스텝 DB 업로드
                        LoadTextIQC()          'TD 측정 수량 화면 표시 업데이트

                        If txbRun.Text = "DB등록완료" Then
                            txbRun.Text = "수량등록완료"
                            txbRun.BackColor = Color.Green
                        Else
                            txbRun.Text = "DB등록실패"
                            txbRun.BackColor = Color.Red
                        End If


                        'LoadRegProduct()       '생산 MES 수량 정보 DB 다운로드
                        'CalRegProductIQC()     '생산 MES 수량 계산
                        'UploadRegProductIQC()  '생산 MES 수량 정보 DB 업로드

                    End If

                    SaveLogStr = getNowDate() & "," & getNowTime() & "," & RegMeasure.MeasurePV_SN & "," & RegMeasure.MeasureTestStep & "," & RegMeasure.MeasureOkNg & "," & RegMeasure.MeasureGrade & "," &
                                 RegMeasure.MeasureCapacitor & "," & RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL1) & "," &
                                 RegMeasure.MeasureValue(RELAY_NUM.RL2) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL3) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL4) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL5) & "," &
                                 RegMeasure.MeasureValue(RELAY_NUM.RL6) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL7) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL8) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL9) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL10) & "," &
                                 RegTD_VALUE.PV_Date & "," & RegTD_VALUE.PV_Frequency & "," & RegTD_VALUE.PV_Impedance & "," & RegTD_VALUE.PV_Capacitance & "," & RegTD_VALUE.PV_Resistance & "," &
                                 RegMesIQC.IQC_TotalCnt & "," & RegMesIQC.IQC_OKCnt & "," & RegMesIQC.IQC_NGCnt & "," & RegMesIQC.IQC_GradeCnt(MODEL_NAME.PRO) & "," & RegMesIQC.IQC_GradeCnt(MODEL_NAME.LUX) & "," &
                                 RegMesIQC.IQC_GradeCnt(MODEL_NAME.EYE) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL1) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL2) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL3) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL4) & "," &
                                 RegMesIQC.IQC_CapCnt(RELAY_NUM.RL5) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL6) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL7) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL8) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL9) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL10) & "," &
                                 RegMesIQC.IQC_ReTotalCnt & "," & RegMesIQC.IQC_ReOkCnt & "," & RegMesIQC.IQC_ReNgCnt
                    Save_CSV(SaveLogStr)
                    Dim mqttResult As Boolean

                    If RegMeasure.MeasureOkNg = "OK" Then
                        mqttResult = True
                    Else
                        mqttBroker = False
                    End If

                    SaveLogStr = "[{""testdate"":""" & getNowDate() & """},{""testtime"":""" & getNowTime() & """},{""td_serial"":""" & RegMeasure.MeasurePV_SN & """},{""td_freq"":""" & RegTD_VALUE.PV_Frequency & """},{""td_imp"":""" & RegTD_VALUE.PV_Impedance & """},{""td_grade"":""" & RegMeasure.MeasureGrade & """},{""test_result"":""" & mqttResult & """},{""td_c"":""" & RegMeasure.MeasureCapacitor & """}]"
                    Mqtt_DataSend(SaveLogStr)
                    txbGrade.Refresh()
                    txbCapacitor.Refresh()
                End If

            Else
                If (txbPV_SN.Text = "" Or txbPV_SN.Text = "입력하세요!") And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 입력해주세요")
                ElseIf (Len(RegMeasure.MeasurePV_SN) <> LenPVSN And Len(RegMeasure.MeasurePV_SN) <> (LenPVSN - 1)) And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
                ElseIf (Mid(RegMeasure.MeasurePV_SN, 1, 3) <> NamePVSN Or Mid(RegMeasure.MeasurePV_SN, 1, 1) <> NamePVSN2) And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
                ElseIf txbPV_Freq.Text = "N/A" Or txbPV_Freq.Text = "" Then
                    MsgBox("T/D 주파수를 형식에 맞게 입력해주세요")
                End If
            End If

            btnRunIQC.Enabled = True
            btnRunIQC.Refresh()
            If chbUseBarcode.Checked Then txbPV_SN.Focus()

        Catch ResultNull As NullReferenceException
            btnRunIQC.Enabled = True
            btnRunIQC.Refresh()
            MsgBox("Mea ERR:장비와 연결되어 있지 않습니다. 연결 상태를 확인하여 주십시오", vbCritical)

        Catch ex As Exception
            btnRunIQC.Enabled = True
            btnRunIQC.Refresh()
            MsgBox("Mea ERR:" & Err.Description)
            SetBeep(SoundError)
        End Try
    End Sub

    '    Private Sub RunProcessIQC()
    '        Dim Cnt As Integer
    '        Dim StatusFail As Boolean = False
    '        Dim RetStr As String
    '        Dim SaveLogStr As String
    '        Dim tmpMeasureValue As Double

    '        Try
    '            If ((Len(RegMeasure.MeasurePV_SN) = LenPVSN Or Len(RegMeasure.MeasurePV_SN) = LenPVSN - 1) And (Mid(RegMeasure.MeasurePV_SN, 1, 3) = NamePVSN Or Mid(RegMeasure.MeasurePV_SN, 1, 1) = NamePVSN2)) Or chbAutoMeasure.Checked = False Or CalModeOn = True And (txbPV_Freq.Text <> "N/A" And txbPV_Freq.Text <> "") Then

    '                If Mid(RegMeasure.MeasurePV_SN, 1, 3) = NamePVSN Then
    '                    IQCValueStandard(True)
    '                Else
    '                    IQCValueStandard(False)
    '                End If

    '                cmdDisplay("Start measurement.")
    '                SetDefaultText()
    '                txbGrade.BackColor = DefaultBackColor
    '                txbGrade.Text = "측정중"
    '                txbGrade.Refresh()
    '                txbCapacitor.BackColor = DefaultBackColor
    '                txbCapacitor.Text = "측정중"
    '                txbCapacitor.Refresh()
    '                btnRunIQC.Enabled = False
    '                btnRunIQC.Refresh()

    '                MeasureTime(MeasSTART)
    '                If SetBeep(SoundStart) Then
    '                    txbGrade.BackColor = DefaultBackColor
    '                    txbGrade.Text = "대기"
    '                    txbGrade.Refresh()

    '                    txbCapacitor.BackColor = DefaultBackColor
    '                    txbCapacitor.Text = "대기"
    '                    txbCapacitor.Refresh()

    '                    txbRun.Text = "통신 에러"
    '                    txbRun.BackColor = Color.Red
    '                    txbRun.Refresh()
    '                    Exit Sub
    '                End If

    '                myScope.IO.Clear() ' Clear the interface.
    '                myScope.IO.Timeout = Val(txbTimeout.Text) ' Set I/O communication timeout.
    '                RetStr = DoCommand("*CLS")
    '                If RetStr = "Fail" Then
    '                    StatusFail = True
    '                    If ShowDetailLog Then
    '                        txbRun.Text = "Scope Error 01" 'cmdDisplay("Scope clear failure")
    '                        txbRun.BackColor = Color.Red
    '                        txbRun.Refresh()
    '                    End If
    '                    GoTo MEASURE_END
    '                End If
    '                If ShowDetailLog Then cmdDisplay("Scope clear complete")

    '                SetFrequency(txbPV_Freq.Text)
    '                If ShowDetailLog Then cmdDisplay("Frequency : " & txbPV_Freq.Text & " kHz")

    '                '더미 샷
    '                SetRelay(1, 1)
    '                For Cnt = 1 To Val(txbIQC_DummyShotCnt.Text)
    '                    If ShowDetailLog Then cmdDisplay("Run dummy measure sequence[" & Cnt & "]")
    '                    RetStr = DoCommand(":Single")
    '                    If RetStr = "Fail" Then
    '                        StatusFail = True
    '                        If ShowDetailLog Then cmdDisplay("Trigger set failure")
    '                        If ShowDetailLog Then
    '                            txbRun.Text = "Scope Error 02" 'cmdDisplay("Trigger set failure")
    '                            txbRun.BackColor = Color.Red
    '                            txbRun.Refresh()
    '                        End If
    '                        Exit For
    '                    End If
    '                    If ShowDetailLog Then cmdDisplay("Waiting for trigger")

    '                    If ShowDetailLog Then cmdDisplay("Run dummy shot[" & txbOntime.Text & " ms]")
    '                    RunShot(txbOntime.Text)
    '                    PollingReceiveData(300)

    '                    DelayMs(Val(txbOntime.Text) + 50)

    '                    RetStr = RunMeasure()
    '                    If RetStr = "Fail" Then
    '                        StatusFail = True
    '                        txbRun.Text = "Scope Error 03" 'cmdDisplay("Trigger failure")
    '                        txbRun.BackColor = Color.Red
    '                        txbRun.Refresh()
    '                        If ShowDetailLog Then cmdDisplay("Trigger failure")
    '                        Exit For
    '                    End If

    '                    tmpMeasureValue = Val(RetStr)
    '                    cmdDisplay("Measurement[D" & Cnt & "] : " & Format(tmpMeasureValue, "0.000") & " A")

    '                    DelayMs(Val(txbMeasureDelay.Text))
    '                Next

    '                '측정 샷
    '                ResetRegMeasureValue()

    '                For Cnt = 1 To RELAY_C.Length - 1
    '                    If ShowDetailLog Then cmdDisplay("Run measure sequence[" & Cnt & "]")
    '                    SetRelay(relayCnt(Cnt), 1)
    '                    If ShowDetailLog Then cmdDisplay("Turn on relay[" & Cnt & "]")

    '                    RetStr = DoCommand(":Single")
    '                    If RetStr = "Fail" Then
    '                        StatusFail = True
    '                        txbRun.Text = "Scope Error 04" 'cmdDisplay("Trigger set failure")
    '                        txbRun.BackColor = Color.Red
    '                        txbRun.Refresh()
    '                        If ShowDetailLog Then cmdDisplay("Trigger set failure")
    '                        Exit For
    '                    End If
    '                    If ShowDetailLog Then cmdDisplay("Waiting for trigger")

    '                    If ShowDetailLog Then cmdDisplay("Run shot[" & txbOntime.Text & " ms]")
    '                    RunShot(txbOntime.Text)
    '                    PollingReceiveData(300)

    '                    DelayMs(Val(txbOntime.Text) + 50)

    '                    RetStr = RunMeasure()
    '                    If RetStr = "Fail" Then
    '                        StatusFail = True
    '                        txbRun.Text = "Scope Error 03" 'cmdDisplay("Trigger failure")
    '                        txbRun.BackColor = Color.Red
    '                        txbRun.Refresh()
    '                        If ShowDetailLog Then cmdDisplay("Trigger failure")
    '                        Exit For
    '                    End If
    '                    RegMeasure.MeasureValue(relayCnt(Cnt)) = Val(RetStr)
    '                    tmpMeasureValue = RegMeasure.MeasureValue(relayCnt(Cnt))
    '                    cmdDisplay("Measurement[R" & Cnt & "] : " & Format(tmpMeasureValue, "0.000") & " A")

    '                    DelayMs(Val(txbMeasureDelay.Text))
    '                Next

    'MEASURE_END:
    '                DoCommand(":Run")
    '                SetBeep(SoundEnd)
    '                cmdDisplay("End measurement(Run Times : " + Str(MeasureTime(MeasEND)) + " ms).")
    '                If ShowDetailLog Then cmdDisplay("Judgment start")

    '                If StatusFail Then
    '                    RegMeasure.MeasureOkNg = "NG"
    '                    RegMeasure.MeasureGrade = "FAIL"
    '                    txbGrade.BackColor = Color.Red
    '                    txbCapacitor.BackColor = Color.Red
    '                    txbGrade.Text = "FAIL"
    '                    txbCapacitor.Text = "FAIL"
    '                    txbGrade.Refresh()
    '                    txbCapacitor.Refresh()

    '                    txbRun.Text = "측정실패"
    '                    txbRun.BackColor = Color.Red
    '                Else
    '                    RegMeasure.MeasureValue(0) = 0

    '                    For Cnt = 1 To RELAY_C.Length - 1
    '                        If RegMeasure.MeasureValue(relayCnt(Cnt)) <= ProMAX And RegMeasure.MeasureValue(0) = 0 Then RegMeasure.MeasureValue(0) = Cnt

    '                        If RegMeasure.MeasureValue(0) Then
    '                            If RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) < RegMeasure.MeasureValue(relayCnt(Cnt)) Then
    '                                If RegMeasure.MeasureValue(relayCnt(Cnt)) <= ProMAX Then
    '                                    RegMeasure.MeasureValue(0) = Cnt
    '                                End If
    '                            End If
    '                        End If
    '                    Next

    '                    If RegMeasure.MeasureValue(0) = 0 Then
    '                        RegMeasure.MeasureOkNg = "NG"
    '                        RegMeasure.MeasureGrade = "OVER"
    '                        txbGrade.Text = RegMeasure.MeasureGrade
    '                        RegMeasure.MeasureValue(0) = 0
    '                        txbGrade.BackColor = Color.Red
    '                    ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= ProMIN Then
    '                        RegMeasure.MeasureOkNg = "OK"
    '                        RegMeasure.MeasureGrade = "PRO"
    '                        txbGrade.Text = RegMeasure.MeasureGrade
    '                        txbGrade.BackColor = Color.Green
    '                    ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= LuxMIN Then
    '                        RegMeasure.MeasureOkNg = "OK"
    '                        RegMeasure.MeasureGrade = "LUX"
    '                        txbGrade.Text = RegMeasure.MeasureGrade
    '                        txbGrade.BackColor = Color.Green
    '                    ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= EyeMIN Then
    '                        RegMeasure.MeasureOkNg = "OK"
    '                        RegMeasure.MeasureGrade = "EYE"
    '                        txbGrade.Text = RegMeasure.MeasureGrade
    '                        txbGrade.BackColor = Color.Green
    '                    Else
    '                        RegMeasure.MeasureOkNg = "NG"
    '                        RegMeasure.MeasureGrade = "UNDER"
    '                        txbGrade.Text = RegMeasure.MeasureGrade
    '                        RegMeasure.MeasureValue(0) = 0
    '                        txbGrade.BackColor = Color.Red
    '                    End If
    '                    txbGrade.Refresh()

    '                    RegMeasure.MeasureCapacitor = RELAY_C(RegMeasure.MeasureValue(0))
    '                    txbCapacitor.Text = RegMeasure.MeasureCapacitor
    '                    If RegMeasure.MeasureValue(0) Then
    '                        txbCapacitor.BackColor = Color.Green
    '                    Else
    '                        txbCapacitor.BackColor = Color.Red
    '                    End If
    '                    txbRun.Text = "측정완료"
    '                    txbRun.BackColor = Color.Green
    '                    txbCapacitor.Refresh()
    '                End If

    '                If chbAutoMeasure.Checked Then

    '                    txbRun.Text = "DB등록중"
    '                    txbRun.BackColor = Color.DarkOrange

    '                    GetCntDB_IQC()         'TD 측정 수량 집계
    '                    If UploadDB_IQC() Then  'TD 측정 Data DB 저장
    '                        cmdDisplay("Save Measure DB failure!")
    '                        txbRun.Text = "DB등록실패"
    '                        txbRun.BackColor = Color.DarkOrange
    '                    Else
    '                        cmdDisplay("Save Measure DB complete!")
    '                        txbRun.Text = "DB등록완료"
    '                        txbRun.BackColor = Color.Green
    '                    End If

    '                    CalCntDB_IQC()         'TD 측정 수량 계산
    '                    UploadCntDB_IQC()          'TD 측정 수량 및 테스트 스텝 DB 업로드
    '                    LoadTextIQC()          'TD 측정 수량 화면 표시 업데이트

    '                    If txbRun.Text = "DB등록완료" Then
    '                        txbRun.Text = "수량등록완료"
    '                        txbRun.BackColor = Color.Green
    '                    Else
    '                        txbRun.Text = "DB등록실패"
    '                        txbRun.BackColor = Color.Red
    '                    End If


    '                    'LoadRegProduct()       '생산 MES 수량 정보 DB 다운로드
    '                    'CalRegProductIQC()     '생산 MES 수량 계산
    '                    'UploadRegProductIQC()  '생산 MES 수량 정보 DB 업로드

    '                End If

    '                SaveLogStr = getNowDate() & "," & getNowTime() & "," & RegMeasure.MeasurePV_SN & "," & RegMeasure.MeasureTestStep & "," & RegMeasure.MeasureOkNg & "," & RegMeasure.MeasureGrade & "," &
    '                             RegMeasure.MeasureCapacitor & "," & RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL1) & "," &
    '                             RegMeasure.MeasureValue(RELAY_NUM.RL2) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL3) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL4) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL5) & "," &
    '                             RegMeasure.MeasureValue(RELAY_NUM.RL6) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL7) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL8) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL9) & "," & RegMeasure.MeasureValue(RELAY_NUM.RL10) & "," &
    '                             RegTD_VALUE.PV_Date & "," & RegTD_VALUE.PV_Frequency & "," & RegTD_VALUE.PV_Impedance & "," & RegTD_VALUE.PV_Capacitance & "," & RegTD_VALUE.PV_Resistance & "," &
    '                             RegMesIQC.IQC_TotalCnt & "," & RegMesIQC.IQC_OKCnt & "," & RegMesIQC.IQC_NGCnt & "," & RegMesIQC.IQC_GradeCnt(MODEL_NAME.PRO) & "," & RegMesIQC.IQC_GradeCnt(MODEL_NAME.LUX) & "," &
    '                             RegMesIQC.IQC_GradeCnt(MODEL_NAME.EYE) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL1) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL2) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL3) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL4) & "," &
    '                             RegMesIQC.IQC_CapCnt(RELAY_NUM.RL5) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL6) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL7) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL8) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL9) & "," & RegMesIQC.IQC_CapCnt(RELAY_NUM.RL10) & "," &
    '                             RegMesIQC.IQC_ReTotalCnt & "," & RegMesIQC.IQC_ReOkCnt & "," & RegMesIQC.IQC_ReNgCnt
    '                Save_CSV(SaveLogStr)
    '                Dim mqttResult As Boolean

    '                If RegMeasure.MeasureOkNg = "OK" Then
    '                    mqttResult = True
    '                Else
    '                    mqttBroker = False
    '                End If

    '                SaveLogStr = "[{""testdate"":""" & getNowDate() & """},{""testtime"":""" & getNowTime() & """},{""td_serial"":""" & RegMeasure.MeasurePV_SN & """},{""td_freq"":""" & RegTD_VALUE.PV_Frequency & """},{""td_imp"":""" & RegTD_VALUE.PV_Impedance & """},{""td_grade"":""" & RegMeasure.MeasureGrade & """},{""test_result"":""" & mqttResult & """},{""td_c"":""" & RegMeasure.MeasureCapacitor & """}]"
    '                Mqtt_DataSend(SaveLogStr)
    '                txbGrade.Refresh()
    '                txbCapacitor.Refresh()
    '            Else
    '                If (txbPV_SN.Text = "" Or txbPV_SN.Text = "입력하세요!") And chbAutoMeasure.Checked Then
    '                    MsgBox("T/D SN 번호를 입력해주세요")
    '                ElseIf (Len(RegMeasure.MeasurePV_SN) <> LenPVSN And Len(RegMeasure.MeasurePV_SN) <> (LenPVSN - 1)) And chbAutoMeasure.Checked Then
    '                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
    '                ElseIf (Mid(RegMeasure.MeasurePV_SN, 1, 3) <> NamePVSN Or Mid(RegMeasure.MeasurePV_SN, 1, 1) <> NamePVSN2) And chbAutoMeasure.Checked Then
    '                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
    '                ElseIf txbPV_Freq.Text = "N/A" Or txbPV_Freq.Text = "" Then
    '                    MsgBox("T/D 주파수를 형식에 맞게 입력해주세요")
    '                End If
    '            End If

    '            btnRunIQC.Enabled = True
    '            btnRunIQC.Refresh()
    '            If chbUseBarcode.Checked Then txbPV_SN.Focus()

    '        Catch ResultNull As NullReferenceException
    '            btnRunIQC.Enabled = True
    '            btnRunIQC.Refresh()
    '            MsgBox("Mea ERR:장비와 연결되어 있지 않습니다. 연결 상태를 확인하여 주십시오", vbCritical)

    '        Catch ex As Exception
    '            btnRunIQC.Enabled = True
    '            btnRunIQC.Refresh()
    '            MsgBox("Mea ERR:" & Err.Description)
    '            SetBeep(SoundError)
    '        End Try
    '    End Sub

    Private Function CheckCartConnect() As Boolean
        Dim retVal As String

        Try
            tmpCmdMessage = "getctcon"
            cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf
            SendData(cmdMessage)

            retVal = PollingReceiveData(1000)

            If retVal <> 0 Then
                Return False
            Else
                Return True
            End If
        Catch
            Return True
        End Try
    End Function
    Private Function SetWriteFrequency() As Boolean
        Dim tmpStr As String

        tmpCmdMessage = "cartfreq"
        cmdMessage = "[" & tmpCmdMessage & " " & txbPV_Freq.Text & "]" + vbCrLf
        SendData(cmdMessage)
        tmpStr = PollingReceiveData(3000)
        If tmpStr <> txbPV_Freq.Text Then
            Return True
        End If
        Return False
    End Function
    Private Function SetWriteCartManCode() As Boolean
        Dim tmpStr As String

        tmpCmdMessage = "cartmanu"
        cmdMessage = "[" & tmpCmdMessage & " JOMT]" + vbCrLf
        SendData(cmdMessage)
        tmpStr = PollingReceiveData(10000)

        If tmpStr <> "jomt" Then
            Return True
        End If
        Return False
    End Function
    Private Function SetWriteCartModel() As Boolean
        Dim tmpStr As String

        If CartType = CART_TYPE.CART_NONE Then Return True

        tmpCmdMessage = "ctmodel"
        cmdMessage = "[" & tmpCmdMessage & " " & CartType & "]" + vbCrLf
        SendData(cmdMessage)
        tmpStr = PollingReceiveData(3000)
        If tmpStr <> CartType Then
            Return True
        End If
        Return False
    End Function
    Private Function SetWriteCartType() As Boolean
        Dim tmpStr As String
        Dim tmpCartType As Integer

        If CartType = CART_TYPE.CART_NONE Then Return True

        If CartType = CART_TYPE.PRO_EYE Or CartType = CART_TYPE.LUX_EYE Then
            tmpCartType = 1
        ElseIf CartType = CART_TYPE.PRO_FACE Or CartType = CART_TYPE.LUX_FACE Then
            tmpCartType = 2
        End If

        tmpCmdMessage = "carttype"
        cmdMessage = "[" & tmpCmdMessage & " " & tmpCartType & "]" + vbCrLf
        SendData(cmdMessage)
        tmpStr = PollingReceiveData(3000)
        If tmpStr <> tmpCartType Then
            Return True
        End If
        Return False
    End Function
    Private Function SetPV_SN() As Boolean
        Dim tmpStr As String

        tmpCmdMessage = "initcart"
        cmdMessage = "[" & tmpCmdMessage & " " & txbPV_SN.Text & "]" + vbCrLf
        SendData(cmdMessage)
        tmpStr = PollingReceiveData(10000)
        If UCase(tmpStr) <> txbPV_SN.Text Then
            Return True
        End If
        Return False
    End Function
    Public Function UploadDB_PD1() As Boolean
        Dim UpdateSetDataStr As String
        Dim P1_Model As String = "NULL"
        Dim P1_CartType As String = "NULL"

        Try
            Select Case CartType
                Case CART_TYPE.PRO_FACE
                    P1_Model = "PRO"
                    P1_CartType = "FACE"
                Case CART_TYPE.PRO_EYE
                    P1_Model = "PRO"
                    P1_CartType = "EYE"
                Case CART_TYPE.LUX_FACE
                    P1_Model = "LUX"
                    P1_CartType = "FACE"
                Case CART_TYPE.LUX_EYE
                    P1_Model = "LUX"
                    P1_CartType = "EYE"
                Case CART_TYPE.PRO_BODY
                    P1_Model = "PRO"
                    P1_CartType = "BODY"
                Case CART_TYPE.LUX_BODY
                    P1_Model = "LUX"
                    P1_CartType = "BODY"
                Case CART_TYPE.ALPHA_FACE
                    P1_Model = "ALPHA"
                    P1_CartType = "FACE"
                Case CART_TYPE.ALPHA_EYE
                    P1_Model = "ALPHA"
                    P1_CartType = "EYE"
                Case CART_TYPE.ALPHA_BODY
                    P1_Model = "ALPHA"
                    P1_CartType = "BODY"
                Case CART_TYPE.MAX_SET
                    P1_Model = "MAXIMUM"
                    P1_CartType = "SET"
            End Select

            If ShowDetailLog Then cmdDisplay("Enter the 'UploadDB_PD1' processor")
            If RegMeasure.PreTestStep <= 29 Then

                If ckbSerialUse.Checked = True Then
                    UpdateSetDataStr = "TestStep='" & RegMeasure.MeasureTestStep & "',P1_Frequency='" & RegTD_VALUE.PV_Frequency & "',P1_Model='" & P1_Model & "',P1_CartType='" & P1_CartType & "'"

                    If RegMeasure.PreTestStep < TeststepSet.Re_OK Then
                        UpdateSetDataStr = UpdateSetDataStr & ",P1_Date='" & getNowDate() & "',P1_Time='" & getNowTime() & "'"
                    End If
                Else
                    UpdateSetDataStr = "TestStep='" & RegMeasure.MeasureTestStep & "',P0_Model='" & P1_Model & "',P0_Type='" & P1_CartType & "'"

                    If RegMeasure.PreTestStep < TeststepSet.Re_OK Then
                        UpdateSetDataStr = UpdateSetDataStr & ",P0_Date='" & getNowDate() & "',P0_Time='" & getNowTime() & "'"
                    End If
                End If

                StrCmd = "update jomtCartridgeTbl set " & UpdateSetDataStr & " where PV_SN ='" & RegTD_VALUE.PV_SN & "';"
                DBCommand(StrCmd)
                Return False
            End If
            Return True
        Catch ex As Exception
            Return True
        End Try
    End Function
    Private Sub CalCntDB_PD1()
        Dim cnt As Integer

        If ShowDetailLog Then cmdDisplay("Enter the 'CalCntDB_PD1' processor")

        ResetCntRegMesPD1()

        If RegMeasure.PreTestStep < TeststepSet.First_Ok - 1 Then

            CntRegMesPD1.PD1_TotalCnt += 1
            If RegMeasure.MeasureOkNg = "OK" Then
                CntRegMesPD1.PD1_OKCnt += 1

                cnt = GradeToValue(RegMeasure.MeasureGrade)
                CntRegMesPD1.PD1_GradeCnt(cnt) += 1
                For cnt = 1 To 10
                    If RegMeasure.MeasureCapacitor = RELAY_C(cnt) Then
                        CntRegMesPD1.PD1_CapCnt(cnt) += 1
                        Exit For
                    End If
                Next
                RegMeasure.MeasureTestStep = TeststepSet.First_Ok
            Else
                CntRegMesPD1.PD1_NGCnt += 1
                RegMeasure.MeasureTestStep = TeststepSet.First_Ng
            End If

        ElseIf RegMeasure.MeasureTestStep >= TeststepSet.First_Ok Then

            If RegMeasure.MeasureOkNg = "OK" Then  '현재 OK 이면서

                RegMeasure.MeasureTestStep = TeststepSet.First_Ok

                If RegMeasure.PreTestStep = TeststepSet.First_Ok Or RegMeasure.PreTestStep = TeststepSet.Re_OK Then      '이전 OK 일때

                ElseIf RegMeasure.PreTestStep = TeststepSet.First_Ng Or RegMeasure.PreTestStep = TeststepSet.RE_Ng Then  '이전 NG 일때
                    CntRegMesPD1.PD1_OKCnt += 1
                    CntRegMesPD1.PD1_NGCnt -= 1

                    cnt = GradeToValue(RegMeasure.MeasureGrade)
                    CntRegMesPD1.PD1_GradeCnt(cnt) += 1
                    For cnt = 1 To 10
                        If RegMeasure.MeasureCapacitor = RELAY_C(cnt) Then
                            CntRegMesPD1.PD1_CapCnt(cnt) += 1
                            Exit For
                        End If
                    Next

                End If

            Else                                   '현재 NG 이면서

                RegMeasure.MeasureTestStep = TeststepSet.First_Ng

                If RegMeasure.PreTestStep = TeststepSet.First_Ok Or RegMeasure.PreTestStep = TeststepSet.Re_OK Then      '이전 OK 일때
                    CntRegMesPD1.PD1_OKCnt -= 1
                    CntRegMesPD1.PD1_NGCnt += 1

                    cnt = GradeToValue(RegMeasure.MeasureGrade)
                    CntRegMesPD1.PD1_GradeCnt(cnt) -= 1
                    For cnt = 1 To 5
                        If RegMeasure.MeasureCapacitor = RELAY_C(cnt) Then
                            CntRegMesPD1.PD1_CapCnt(cnt) -= 1
                            Exit For
                        End If
                    Next
                ElseIf RegMeasure.PreTestStep = TeststepSet.First_Ng Or RegMeasure.PreTestStep = TeststepSet.RE_Ng Then  '이전 NG 일때

                End If

            End If
        End If

        RegMesPD1.PD1_TotalCnt += CntRegMesPD1.PD1_TotalCnt
        RegMesPD1.PD1_OKCnt += CntRegMesPD1.PD1_OKCnt
        RegMesPD1.PD1_NGCnt += CntRegMesPD1.PD1_NGCnt

        RegLocalPD1.PD1_TotalCnt += CntRegMesPD1.PD1_TotalCnt
        RegLocalPD1.PD1_OKCnt += CntRegMesPD1.PD1_OKCnt
        RegLocalPD1.PD1_NGCnt += CntRegMesPD1.PD1_NGCnt

        For i = MODEL_NAME.PRO To MODEL_NAME.MAX
            RegMesPD1.PD1_GradeCnt(i) += CntRegMesPD1.PD1_GradeCnt(i)
            RegLocalPD1.PD1_GradeCnt(i) += CntRegMesPD1.PD1_GradeCnt(i)
        Next

        For i = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM - 1
            RegMesPD1.PD1_CapCnt(i) += CntRegMesPD1.PD1_CapCnt(i)
            RegLocalPD1.PD1_CapCnt(i) += CntRegMesPD1.PD1_CapCnt(i)
        Next

    End Sub
    Private Sub SaveDB_PD1()
        Dim SaveLogStr As String

        CalCntDB_PD1()         '보드 장착 수량 계산
        UploadDB_PD1()         'PD1 단계 DB 업로드
        'LoadTextPD1()          '보드 장착 수량 화면 표시 업데이트
        UploadCntDB_PD1()      '보드 장착 수량 정보 DB 업로드
        SaveLocalCount()
        LoadLocalCount()

        'LoadRegProduct()       '생산 MES 수량 정보 DB 다운로드
        'CalRegProductPD1()     '생산 MES 수량 계산
        'UploadRegProductPD1()  '생산 MES 수량 정보 DB 업로드

        SaveLogStr = getNowDate() & "," & getNowTime() & "," & txbPV_SN.Text & "," & txbPV_Date.Text & "," & txbPV_Freq.Text & "," & txbPV_Z.Text & "," & txbPV_C.Text & "," & txbPV_R.Text & "," & txbGradePD1.Text & "," & txbCapacitorPD1.Text & "," & "," & txbTotalCntPD1.Text & "," & txbOkCntPD1.Text & "," & txbNgCntPD1.Text & "," & txbProCntPD1.Text & "," & txbLuxCntPD1.Text & "," & txbEyeCntPD1.Text & "," & txbR1CntPD1.Text & "," & txbR2CntPD1.Text & "," & txbR3CntPD1.Text & "," & txbR4CntPD1.Text & "," & txbR5CntPD1.Text & txbR6CntPD1.Text & txbR7CntPD1.Text & txbR8CntPD1.Text & txbR9CntPD1.Text & txbR10CntPD1.Text
        Save_CSV(SaveLogStr)
    End Sub
    Private Sub RunProcessPD1()
        Dim tmpCmd As String

        If ckbSerialUse.Checked = False Then
            If LoadDB() Then
                Exit Sub
            End If

            If RunCartInfoReadPD1() Then  ' IQC 측정값 읽기
                btnRunPD1.Refresh()
            End If

            RegMeasure.MeasureOkNg = "OK"
            RegMeasure.MeasureGrade = "MAX"
            CartType = CART_TYPE.MAX_SET

            If ckbPlay.Checked = True Then
                SaveDB_PD1()
            End If

            btnRunPD1.Enabled = True
        Else
            Select Case PD1_STATUS

                Case PD1_ENUM.STANDBY

                    If LoadDB() Then
                        PD1_STATUS = PD1_ENUM.STANDBY
                        Exit Select
                    End If

                    If RunCartInfoReadPD1() Then  ' IQC 측정값 읽기
                        PD1_STATUS = PD1_ENUM.STANDBY
                        btnRunPD1.Enabled = True
                        btnRunPD1.Refresh()
                    Else
                        PD1_STATUS = PD1_ENUM.WAIT
                        If chbAutoMeasure.Checked Then
                            If CheckCartConnect() Then
                                btnRunPD1.Text = "카트리지 보드를 장착해주세요!"
                                btnRunPD1.Refresh()
                                txbCartridgePD1.BackColor = Color.Blue
                                txbCartridgePD1.Refresh()
                            Else
                                PD1_STATUS = PD1_ENUM.WRITE
                                btnRunPD1.Text = "카트리지 정보를 기록하는 중."
                                btnRunPD1.Refresh()
                                RunCartInitWrite()
                            End If
                        Else
                            btnRunPD1.Enabled = True
                            btnRunPD1.Focus()
                            btnRunPD1.Refresh()
                        End If
                    End If

                Case PD1_ENUM.WAIT

                    If chbAutoMeasure.Checked Then
                        tmpCmd = ParsingData(False)
                        If tmpCmd <> "0" Then
                            PD1_STATUS = PD1_ENUM.WRITE
                            btnRunPD1.Text = "카트리지 정보를 기록하는 중."
                            btnRunPD1.Refresh()
                            RunCartInitWrite()
                        End If
                    Else
                        If CheckCartConnect() Then
                            btnRunPD1.Text = "카트리지 보드를 장착해주세요!"
                            btnRunPD1.Refresh()
                            txbCartridgePD1.BackColor = Color.Blue
                            txbCartridgePD1.Refresh()
                        Else
                            PD1_STATUS = PD1_ENUM.WRITE
                            btnRunPD1.Text = "카트리지 정보를 기록하는 중."
                            btnRunPD1.Refresh()
                            RunCartInitWrite()
                        End If
                        btnRunPD1.Focus()
                    End If

                Case PD1_ENUM.WRITE
                    If chbAutoMeasure.Checked Then
                        tmpCmd = ParsingData(False)
                        If tmpCmd = "1" Or (Mid(tmpCmd, 1, 3) = "dit" And Len(tmpCmd) = 9) Or (Mid(tmpCmd, 1, 3) = "dit" And Len(tmpCmd) = 10) Or (Mid(tmpCmd, 1, 1) = "j" And Len(tmpCmd) = 10) Then
                            PD1_STATUS = PD1_ENUM.WRITE
                            btnRunPD1.Text = "카트리지 정보를 기록하는 중."
                            btnRunPD1.Refresh()
                            RunCartInitWrite()
                        End If
                    End If
            End Select

            If PD1_STATUS = PD1_ENUM.SAVE Then
                SaveDB_PD1()
                PD1_STATUS = PD1_ENUM.STANDBY
            End If
        End If

    End Sub

    Private Sub RunProcessPD2()
        Dim tmpCmd As String
        Dim RetStr As String
        Dim StatusFail As Boolean = False

        Try
            If chbAutoMeasure.Checked Then
                tmpCmd = ParsingData(False)
            Else
                tmpCmd = txbPV_SN.Text
            End If

            tmpCmd = UCase(tmpCmd)

            If (Mid(tmpCmd, 1, 3) = "DIT" And Len(tmpCmd) = 9) Or (Mid(tmpCmd, 1, 3) = "DIT" And Len(tmpCmd) = 10) Then
                DispToggle = False
                txbPV_SN.BackColor = DefaultBackColor
                txbPV_SN.Clear()
                txbPV_SN.AppendText(tmpCmd)
                LoadDB()
                LoadGrade()

                cmdDisplay("Start measurement.")
                txbJudge.BackColor = DefaultBackColor
                txbJudge.Text = "측정중"
                txbJudge.Refresh()
                txbWriteInfo.BackColor = DefaultBackColor
                txbWriteInfo.Text = "대기"
                txbWriteInfo.Refresh()

                MeasureTime(MeasSTART)

                myScope.IO.Clear() ' Clear the interface.
                myScope.IO.Timeout = Val(txbTimeout.Text) ' Set I/O communication timeout.
                RetStr = DoCommand("*CLS")
                If RetStr = "Fail" Then
                    StatusFail = True
                    GoTo MEASURE_END
                End If

                SetFrequency(txbPV_Freq.Text)

                DelayMs(Val(txbStartWaitDelay.Text))

                RunShot(txbOntime.Text)
                PollingReceiveData(300)
                RunShot(txbOntime.Text)
                PollingReceiveData(300)

                txbJudge.Text = "측정대기중"
                txbJudge.Refresh()
                RunStableTime()

                RetStr = DoCommand(":Single")
                If RetStr = "Fail" Then
                    StatusFail = True
                    GoTo MEASURE_END
                End If

                RunShot(txbOntime.Text)
                PollingReceiveData(300)

                DelayMs(Val(txbOntime.Text) + 50)

                RetStr = RunMeasure()
                If RetStr = "Fail" Then
                    StatusFail = True
                    DoCommand(":RUN")
                    GoTo MEASURE_END
                End If

                'DoCommand(":RUN")

                PD2_Current = Val(RetStr) + Val(txbOffsetValue.Text)
                cmdDisplay("Measurement : " & Format(PD2_Current, "0.000") & " A")

                TransDB_PD2()

                If (QC_Current >= (PD2_Current - Val(txbErrorFactor.Text))) And (QC_Current <= (PD2_Current + Val(txbErrorFactor.Text))) Then
                    txbJudge.Text = "OK"
                    txbJudge.BackColor = Color.Green
                    txbJudge.Refresh()
                Else
                    txbJudge.Text = "NG"
                    txbJudge.BackColor = Color.Red
                    txbJudge.Refresh()
                End If
            ElseIf tmpCmd = "0" Then
                DispToggle = True
                txbPV_SN.Clear()
                txbPV_SN.AppendText("대기중")
                txbPV_Date.Text = "N/A"
                txbPV_Freq.Text = "N/A"
                txbPV_Z.Text = "N/A"
                txbPV_C.Text = "N/A"
                txbPV_R.Text = "N/A"
                txbGradeP2.Text = "N/A"
                txbCapacitorP2.Text = "N/A"
                txbJudge.Text = "N/A"
                txbWriteInfo.Text = "N/A"
                txbPV_Date.Refresh()
                txbPV_Freq.Refresh()
                txbPV_Z.Refresh()
                txbPV_C.Refresh()
                txbPV_R.Refresh()
                txbGradeP2.Refresh()
                txbCapacitorP2.Refresh()
                txbJudge.Refresh()
                txbWriteInfo.Refresh()
            End If
            txbCmdWindowP2.Focus()
            Exit Sub
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try

MEASURE_END:
        txbJudge.Text = "NG"
        txbJudge.BackColor = Color.Red
        txbJudge.Refresh()
    End Sub
    Private Sub RunProcessPD3()
        Dim Cnt As Integer


        Try
            If (txbPV_Freq.Text = "N/A" Or txbPV_Freq.Text = "") And (txbCapacitorPD3.Text = "대기" Or txbCapacitorPD3.Text = "") Then
                MsgBox("카트리지 정보를 입력해주세요.")
                GoTo FAIL_END
            End If

            If (rdbMode1.Checked = False And rdbMode2.Checked = False And rdbMode3.Checked = False) Then
                MsgBox("카트리지 모드 정보를 입력해주세요.")
                GoTo FAIL_END
            End If

            If SetFrequencyReturn(txbPV_Freq.Text) Then
                ''txbGradePD3.Text = "FAIL"
                ''txbGradePD3.BackColor = Color.Red
                ''txbGradePD3.Refresh()
                GoTo FAIL_END
            End If

            For Cnt = 1 To 10
                If RELAY_C(Cnt) = txbCapacitorPD3.Text Then
                    If SetRelay(Cnt, 1) Then
                        ''txbCapacitorPD3.Text = "FAIL"
                        ''txbCapacitorPD3.BackColor = Color.Red
                        ''txbCapacitorPD3.Refresh()
                        GoTo FAIL_END
                    End If
                End If
            Next

            If chbAutoMeasure.Checked Then
                txbPV_SN.Focus()
            End If

            Exit Sub
        Catch ex As Exception
            txbGradePD3.Text = "FAIL"
            txbCapacitorPD3.Text = "FAIL"
            txbGradePD3.BackColor = Color.Red
            txbCapacitorPD3.BackColor = Color.Red
            txbGradePD3.Refresh()
            txbCapacitorPD3.Refresh()
        End Try

FAIL_END:
        txbGradePD3.Text = "FAIL"
        txbCapacitorPD3.Text = "FAIL"
        txbGradePD3.BackColor = Color.Red
        txbCapacitorPD3.BackColor = Color.Red
        txbGradePD3.Refresh()
        txbCapacitorPD3.Refresh()

        If chbAutoMeasure.Checked Then
            txbPV_SN.Focus()
        End If
    End Sub
    Private Function RunCartInfoReadPD1() As Boolean
        Dim tmpBackColor As Object = txbGradePD1.BackColor
        Dim RunBol As Boolean = True
        Dim OKRunSwitch As Boolean = False

        Try
            If (((Len(txbPV_SN.Text) = LenPVSN Or Len(txbPV_SN.Text) = LenPVSN - 1) And (Mid(txbPV_SN.Text, 1, 3) = NamePVSN Or Mid(txbPV_SN.Text, 1, 1) = NamePVSN2) Or chbAutoMeasure.Checked = False) And (txbPV_Freq.Text <> "N/A" And txbPV_Freq.Text <> "")) Then
                txbGradePD1.BackColor = DefaultBackColor
                txbGradePD1.Text = "로딩중"
                txbGradePD1.Refresh()
                txbCapacitorPD1.BackColor = DefaultBackColor
                txbCapacitorPD1.Text = "로딩중"
                txbCapacitorPD1.Refresh()
                txbCartridgePD1.BackColor = DefaultBackColor
                txbCartridgePD1.Text = "대기"
                txbCartridgePD1.Refresh()
                btnRunPD1.Enabled = False
                btnRunPD1.Refresh()

                If ckbSerialUse.Checked = False Then
                    If Mid(txbPV_SN.Text, 1, 4) = "DITH" Or Mid(txbPV_SN.Text, 1, 2) = "J2" Then
                        OKRunSwitch = False
                    Else
                        OKRunSwitch = True
                    End If
                Else
                    OKRunSwitch = True
                End If

                If OKRunSwitch = True Then
                    cmd = "select TestStep,QC_Grade,QC_Capacitor from jomtCartridgeTbl where PV_SN = '" + txbPV_SN.Text + "';"
                    If DBQueryReader(cmd) Then

                        RegMeasure.MeasureTestStep = sqlDataQuery(0)
                        RegMeasure.MeasureGrade = sqlDataQuery(1)
                        RegMeasure.MeasureCapacitor = sqlDataQuery(2)

                        If GradeStop Mod 3 = 2 Or GradeStop Mod 3 = 1 Then

                            If RegMeasure.MeasureGrade = "0" Then
                                txbGradePD1.Text = "미측정"
                                txbCapacitorPD1.Text = "N/A"
                                txbGradePD1.BackColor = Color.Red
                                txbCapacitorPD1.BackColor = Color.Red
                                RegMeasure.MeasureOkNg = "NG"
                            End If

                            If txbCapacitorPD1.Text = "N/A" Then
                                txbGradePD1.BackColor = Color.Red
                                txbCapacitorPD1.BackColor = Color.Red
                                RegMeasure.MeasureOkNg = "NG"
                            End If

                            If RegMeasure.MeasureOkNg <> "NG" Then
                                txbCapacitorPD1.BackColor = Color.Green
                                txbCapacitorPD1.Text = RegMeasure.MeasureCapacitor

                                RegMeasure.MeasureGrade = SettingMeasure
                                txbGradePD1.BackColor = Color.Green
                                txbGradePD1.Text = RegMeasure.MeasureGrade
                            End If

                        Else

                            If RegMeasure.MeasureGrade = "OVER" Then
                                txbGradePD1.Text = "OVER"
                                txbCapacitorPD1.Text = "사용불가"
                                txbCartridgePD1.Text = "사용불가"

                                txbCapacitorPD1.BackColor = Color.Red
                                txbGradePD1.BackColor = Color.Red
                                txbCartridgePD1.BackColor = Color.Red

                                RegMeasure.MeasureOkNg = "NG"
                                txbGradePD1.Refresh()
                                txbCapacitorPD1.Refresh()
                                txbCartridgePD1.Refresh()
                            End If

                            If RegMeasure.MeasureGrade = "0" Then
                                txbGradePD1.Text = "미측정"
                                txbCapacitorPD1.Text = "N/A"
                                txbCartridgePD1.Text = "사용불가"

                                txbCapacitorPD1.BackColor = Color.Red
                                txbGradePD1.BackColor = Color.Red
                                txbCartridgePD1.BackColor = Color.Red

                                RegMeasure.MeasureOkNg = "NG"
                                txbGradePD1.Refresh()
                                txbCapacitorPD1.Refresh()
                                txbCartridgePD1.Refresh()
                            End If

                            If txbCapacitorPD1.Text = "N/A" Then
                                txbCartridgePD1.Text = "사용불가"

                                txbCapacitorPD1.BackColor = Color.Red
                                txbGradePD1.BackColor = Color.Red
                                txbCartridgePD1.BackColor = Color.Red

                                RegMeasure.MeasureOkNg = "NG"
                                txbGradePD1.Refresh()
                                txbCapacitorPD1.Refresh()
                                txbCartridgePD1.Refresh()
                            End If

                            If RegMeasure.MeasureOkNg <> "NG" Then

                                If rdbCmat_5.Checked = True Then

                                    For run = 0 To 1
                                        Select Case RegMeasure.MeasureCapacitor
                                            Case RELAY_C(1)
                                                txbCapacitorPD1.BackColor = Color.Red
                                                txbCapacitorPD1.ForeColor = Color.Black
                                                txbCapacitorPD1.Text = "RED"
                                            Case RELAY_C(2)
                                                txbCapacitorPD1.BackColor = Color.Green
                                                txbCapacitorPD1.ForeColor = Color.Black
                                                txbCapacitorPD1.Text = "GREEN"
                                            Case RELAY_C(3)
                                                txbCapacitorPD1.BackColor = Color.Blue
                                                txbCapacitorPD1.ForeColor = Color.Black
                                                txbCapacitorPD1.Text = "BLUE"
                                            Case RELAY_C(4)
                                                txbCapacitorPD1.BackColor = Color.Black
                                                txbCapacitorPD1.ForeColor = Color.White
                                                txbCapacitorPD1.Text = "BLACK"
                                            Case RELAY_C(5)
                                                txbCapacitorPD1.BackColor = Color.SeaShell
                                                txbCapacitorPD1.ForeColor = Color.Black
                                                txbCapacitorPD1.Text = "WHITE"
                                            Case Else
                                                If cValueChange() Then
                                                    RunBol = False
                                                End If
                                        End Select

                                        If RunBol = True Then
                                            Exit For
                                        Else
                                            RunBol = True
                                        End If
                                    Next

                                    If RunBol = True Then
                                        If RegMeasure.MeasureGrade <> "OVER" And RegMeasure.MeasureGrade <> "UNDER" And RegMeasure.MeasureGrade <> "EYE" Then
                                            txbGradePD1.Text = RegMeasure.MeasureGrade
                                            txbGradePD1.BackColor = Color.Green
                                        Else
                                            txbGradePD1.Text = RegMeasure.MeasureGrade
                                            txbCapacitorPD1.Text = "N/A"
                                            txbGradePD1.BackColor = Color.Red
                                            txbCapacitorPD1.BackColor = Color.Red
                                        End If
                                    Else
                                        txbGradePD1.Text = "N/A"
                                        txbCapacitorPD1.Text = "N/A"
                                        txbGradePD1.BackColor = Color.Red
                                        txbCapacitorPD1.BackColor = Color.Red
                                    End If

                                Else
                                    txbGradePD1.BackColor = Color.Green
                                    txbCapacitorPD1.BackColor = Color.Green
                                    txbGradePD1.Text = RegMeasure.MeasureGrade
                                    txbCapacitorPD1.Text = RegMeasure.MeasureCapacitor
                                End If

                            End If

                        End If

                        txbGradePD1.Refresh()
                        txbCapacitorPD1.Refresh()

                    Else
                        txbGradePD1.Text = "N/A"
                        txbCapacitorPD1.Text = "N/A"
                        txbGradePD1.BackColor = Color.Red
                        txbCapacitorPD1.BackColor = Color.Red
                        txbGradePD1.Refresh()
                        txbCapacitorPD1.Refresh()
                    End If
                Else
                    txbGradePD1.Text = "사용불가"
                    txbCapacitorPD1.Text = "N/A"
                    txbCartridgePD1.Text = "사용불가"

                    txbCapacitorPD1.BackColor = Color.Red
                    txbGradePD1.BackColor = Color.Red
                    txbCartridgePD1.BackColor = Color.Red

                    RegMeasure.MeasureOkNg = "NG"
                    txbGradePD1.Refresh()
                    txbCapacitorPD1.Refresh()
                    txbCartridgePD1.Refresh()
                End If


            Else
                If (txbPV_SN.Text = "" Or txbPV_SN.Text = "입력하세요!") And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 입력해주세요")
                ElseIf (Len(txbPV_SN.Text) <> LenPVSN And Len(txbPV_SN.Text) <> (LenPVSN - 1)) And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
                ElseIf Mid(txbPV_SN.Text, 1, 3) <> NamePVSN And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
                End If
                Return False
            End If

            If chbUseBarcode.Checked Then txbPV_SN.Focus()
            If txbGradePD1.Text = "N/A" Or txbCapacitorPD1.Text = "N/A" Or RegMeasure.MeasureOkNg = "NG" Then
                Return True
            Else
                Return False
            End If

        Catch e As Exception
            MsgBox(e, vbCritical)
            SetBeep(SoundError)
            Return True
        End Try
    End Function

    Private Function cValueChange() As Boolean
        ReDim RegMeasure.MeasureValue(10)
        RegMeasure.MeasureValue(0) = 0

        If Mid(txtPVSN, 1, 3) = NamePVSN Then
            IQCValueStandard(True)
        Else
            IQCValueStandard(False)
        End If

        cmd = "SELECT QC_R1Value,QC_R2Value,QC_R3Value,QC_R4Value,QC_R5Value,QC_R6Value,QC_R7Value,QC_R8Value,QC_R9Value,QC_R10Value FROM jomtCartridgeTbl WHERE PV_SN = '" + txbPV_SN.Text + "'"

        If DBQueryReader(cmd) Then
            For i = 1 To sqlDataQuery.Length
                RegMeasure.MeasureValue(i) = sqlDataQuery(i - 1)
            Next

            For Cnt = 1 To 10
                If Cnt = nudmat_1.Value Or Cnt = nudmat_2.Value Or Cnt = nudmat_3.Value Or Cnt = nudmat_4.Value Or Cnt = nudmat_5.Value Then
                    If RegMeasure.MeasureValue(Cnt) <= ProMAX And RegMeasure.MeasureValue(0) = 0 Then RegMeasure.MeasureValue(0) = Cnt

                    If RegMeasure.MeasureValue(0) Then
                        If RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) < RegMeasure.MeasureValue(Cnt) Then
                            If RegMeasure.MeasureValue(Cnt) <= ProMAX Then
                                RegMeasure.MeasureValue(0) = Cnt
                            End If
                        End If
                    End If
                End If
            Next

            If RegMeasure.MeasureValue(0) = 0 Then
                RegMeasure.MeasureGrade = "OVER"
            ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= ProMIN Then
                RegMeasure.MeasureGrade = "PRO"
            ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= LuxMIN Then
                RegMeasure.MeasureGrade = "LUX"
            ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= EyeMIN Then
                RegMeasure.MeasureGrade = "EYE"
            Else
                RegMeasure.MeasureGrade = "UNDER"
                RegMeasure.MeasureValue(0) = 0
            End If

            If RegMeasure.MeasureValue(0) <> 0 Then
                RegMeasure.MeasureCapacitor = RelayTxb(RegMeasure.MeasureValue(0)).Text
                Return True
            Else
                Return False
            End If

        Else
            Return False
        End If
    End Function

    Private Function RunCartInfoReadPD3() As Boolean
        Dim tmpBackColor As Object = txbGradePD1.BackColor

        Try
            If (((Len(txbPV_SN.Text) = LenPVSN Or Len(txbPV_SN.Text) = LenPVSN - 1) And Mid(txbPV_SN.Text, 1, 3) = NamePVSN) Or chbAutoMeasure.Checked = False) And (txbPV_Freq.Text <> "N/A" And txbPV_Freq.Text <> "") Then
                txbGradePD3.BackColor = DefaultBackColor
                txbGradePD3.Text = "로딩중"
                txbGradePD3.Refresh()
                txbCapacitorPD3.BackColor = DefaultBackColor
                txbCapacitorPD3.Text = "로딩중"
                txbCapacitorPD3.Refresh()

                cmd = "select TestStep,QC_Grade,QC_Capacitor from jomtCartridgeTbl where PV_SN = '" + txbPV_SN.Text + "';"
                If DBQueryReader(cmd) Then
                    RegMeasure.MeasureTestStep = sqlDataQuery(0)
                    RegMeasure.MeasureGrade = sqlDataQuery(1)
                    RegMeasure.MeasureCapacitor = sqlDataQuery(2)
                    txbGradePD3.BackColor = Color.Green
                    txbCapacitorPD3.BackColor = Color.Green
                    txbGradePD3.Text = RegMeasure.MeasureGrade
                    txbCapacitorPD3.Text = RegMeasure.MeasureCapacitor

                    txbGradePD3.Refresh()
                    txbCapacitorPD3.Refresh()
                Else
                    txbGradePD3.Text = "N/A"
                    txbCapacitorPD3.Text = "N/A"
                    txbGradePD3.BackColor = Color.Red
                    txbCapacitorPD3.BackColor = Color.Red
                    txbGradePD3.Refresh()
                    txbCapacitorPD3.Refresh()
                End If

            Else
                If (txbPV_SN.Text = "" Or txbPV_SN.Text = "입력하세요!") And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 입력해주세요")
                ElseIf (Len(txbPV_SN.Text) <> LenPVSN And Len(txbPV_SN.Text) <> (LenPVSN - 1)) And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
                ElseIf Mid(txbPV_SN.Text, 1, 3) <> NamePVSN And chbAutoMeasure.Checked Then
                    MsgBox("T/D SN 번호를 형식에 맞게 입력해주세요")
                End If
                Return False
            End If

            If chbUseBarcode.Checked Then txbPV_SN.Focus()

            If txbGradePD3.Text = "N/A" Or txbCapacitorPD3.Text = "N/A" Then
                Return True
            Else
                Return False
            End If

        Catch
            SetBeep(SoundError)
            Return True
        End Try
    End Function
    Public Sub SetCartModelType()

        If ckbUseModelSelect.Checked Then
            frmModelSelect.ShowDialog()
        Else
            If ckbUseGradeChange.Checked Then
                If CartType = CART_TYPE.PRO_FACE Then
                    If RegMeasure.MeasureGrade = "LUX" Or RegMeasure.MeasureGrade = "EYE" Then
                        frmModelSelect.ShowDialog()
                    End If
                ElseIf CartType = CART_TYPE.LUX_FACE Then
                    If RegMeasure.MeasureGrade = "EYE" Then
                        frmModelSelect.ShowDialog()
                    End If
                End If
            End If
        End If

    End Sub

    Public Function TD_SNSearch() As String
        tmpCmdMessage = "carttdsn"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf
        SendData(cmdMessage)

        Return PollingReceiveData(3000)
    End Function

    Public Sub RunCartInitWrite()
        Dim FailFlag As Boolean = False

        Try
            If (txbGradePD1.Text = "FAIL" Or txbGradePD1.Text = "N/A") Or (txbCapacitorPD1.Text = "FAIL" Or txbCapacitorPD1.Text = "N/A") Then
                txbCartridgePD1.Text = "N/A"
                txbCartridgePD1.Refresh()
                MsgBox("T/D 정보를 확인 해주세요")
                PD1_STATUS = PD1_ENUM.STANDBY
                Exit Sub
            End If

            txbCartridgePD1.Text = "쓰기중"
            txbCartridgePD1.BackColor = DefaultBackColor
            btnRunPD1.Enabled = False
            txbCartridgePD1.Refresh()

            Dim tdSnData As String = TD_SNSearch()

            'If UCase(Mid(tdSnData, 1, 3)) = "DIT" Or UCase(Mid(tdSnData, 1, 1)) = "J" Then
            '    txbCartridgePD1.BackColor = Color.Yellow
            '    txbCartridgePD1.Text = "정보입력중"
            '    txbCartridgePD1.Refresh()

            '    If SetInfo() Then
            '        FailFlag = True
            '        txbCartridgePD1.BackColor = Color.OrangeRed
            '        GoTo MEASURE_END
            '    End If
            'Else
            '    If SetAuth() Then
            '        FailFlag = True
            '        txbCartridgePD1.BackColor = Color.OrangeRed
            '        GoTo MEASURE_END
            '    End If

            '    txbCartridgePD1.BackColor = Color.Yellow
            '    txbCartridgePD1.Text = "정보입력중"
            '    txbCartridgePD1.Refresh()

            '    If SetInfo() Then
            '        FailFlag = True
            '        txbCartridgePD1.BackColor = Color.OrangeRed
            '        GoTo MEASURE_END
            '    End If
            'End If

            If UCase(Mid(tdSnData, 1, 3)) <> "DIT" Or UCase(Mid(tdSnData, 1, 1)) <> "J" Then

                If SetAuth() Then
                    FailFlag = True
                    txbCartridgePD1.BackColor = Color.OrangeRed
                    GoTo MEASURE_END
                End If

                txbCartridgePD1.BackColor = Color.Yellow
                txbCartridgePD1.Text = "정보입력중"
                txbCartridgePD1.Refresh()

                If SetInfo() Then
                    FailFlag = True
                    txbCartridgePD1.BackColor = Color.OrangeRed
                    GoTo MEASURE_END
                End If
            Else
                txbCartridgePD1.BackColor = Color.Yellow
                txbCartridgePD1.Text = "정보입력중"
                txbCartridgePD1.Refresh()

                If SetInfo() Then
                    FailFlag = True
                    txbCartridgePD1.BackColor = Color.OrangeRed
                    GoTo MEASURE_END
                End If
            End If

MEASURE_END:
            If FailFlag Then
                RegMeasure.MeasureOkNg = "NG"
                txbCartridgePD1.Text = "FAIL"
                txbCartridgePD1.Refresh()
                btnRunPD1.Text = "재실행"
                btnRunPD1.Visible = True
                btnRunPD1.Enabled = True
                btnRunPD1.Focus()
                btnRunPD1.Refresh()
                PD1_STATUS = PD1_ENUM.WAIT
            Else
                RegMeasure.MeasureOkNg = "OK"
                txbCartridgePD1.BackColor = Color.Green
                txbCartridgePD1.Text = "OK"

                txbCartridgePD1.Refresh()
                btnRunPD1.Text = "실행"
                btnRunPD1.Visible = True
                If chbAutoMeasure.Checked And chbUseBarcode.Checked Then
                    btnRunPD1.Enabled = False
                Else
                    btnRunPD1.Enabled = True
                End If
                btnRunPD1.Refresh()
                PD1_STATUS = PD1_ENUM.SAVE
            End If
        Catch
            RegMeasure.MeasureOkNg = "NG"
            txbCartridgePD1.Text = "FAIL"
            txbCartridgePD1.BackColor = Color.PaleVioletRed
            txbCartridgePD1.Refresh()
            btnRunPD1.Text = "재실행"
            btnRunPD1.Visible = True
            btnRunPD1.Enabled = True
            btnRunPD1.Focus()
            btnRunPD1.Refresh()
            PD1_STATUS = PD1_ENUM.WAIT
            SetBeep(SoundError)
        End Try
    End Sub

    Private Function SetAuth() As Boolean
        Dim tmpStr As String

        tmpCmdMessage = "cartauth"
        cmdMessage = "[" & tmpCmdMessage & "]" + vbCrLf
        SendData(cmdMessage)
        tmpStr = PollingReceiveData(5000)

        If Mid(tmpStr, 1, 7) = "success" Then
            Return False
        End If

        Return True
    End Function

    Public Function SetInfo() As Boolean
        Dim retVal, power, tShot, rShot As String
        power = 0

        If Mid(txbPV_SN.Text, 1, 4) = "DITH" Or Mid(txbPV_SN.Text, 1, 2) = "J2" Then

            If rdb20mm.Checked = True Then
                If txbGradePD1.Text = "PRO" Then
                    CartType = CART_TYPE.PRO_BODY
                    tShot = 30000
                    rShot = 30000
                Else
                    tShot = 20000
                    rShot = 20000
                End If
            Else
                If txbGradePD1.Text = "PRO" Then
                    CartType = CART_TYPE.ALPHA_BODY
                    tShot = 50000
                    rShot = 50000
                Else
                    Return True
                End If
            End If
        Else

            If rdb20mm.Checked = True Then
                If txbGradePD1.Text = "PRO" Then
                    CartType = CART_TYPE.PRO_FACE
                    tShot = 6000
                    rShot = 6000
                Else
                    CartType = CART_TYPE.PRO_EYE
                    tShot = 60000
                    rShot = 60000
                End If
            Else
                If txbGradePD1.Text = "PRO" Then
                    CartType = CART_TYPE.ALPHA_FACE
                    tShot = 20000
                    rShot = 20000
                Else
                    CartType = CART_TYPE.ALPHA_EYE
                    tShot = 90000
                    rShot = 90000
                End If
            End If

        End If

        tmpCmdMessage = "cartinfo"
        cmdMessage = "[" & tmpCmdMessage & " " & txbPV_SN.Text & "," & CartType & "," & txbPV_Freq.Text & "," & power & "," & tShot & "," & rShot & "]" + vbCrLf
        SendData(cmdMessage)

        retVal = PollingReceiveData(5000)

        If UCase(Mid(retVal, 1, InStr(1, retVal, ",") - 1)) <> txbPV_SN.Text Then
            Return True
        End If

        If Mid(retVal, 12, 2) = CartType.ToString Or Mid(retVal, 11, 2) = CartType.ToString Then
            Return True
        End If

        If Mid(retVal, 17, 4) = txbPV_Freq.Text Or Mid(retVal, 16, 4) = txbPV_Freq.Text Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub txbPV_SN_TextChanged(sender As Object, e As EventArgs) Handles txbPV_SN.TextChanged
        Dim tmpStr As String

        If DevNum = 1 Or DevNum = 2 Or DevNum = 4 Then
            If InputCnt = 0 And chbUseBarcode.Checked = True Then
                tmpStr = txbPV_SN.Text
                If (Len(tmpStr) > 1) Then
                    tmpStr = Mid(tmpStr, Len(tmpStr), 1)
                    txbPV_SN.Clear()
                    txbPV_SN.AppendText(tmpStr)
                End If
                SetInputCnt()
            End If
        ElseIf DevNum = 3 Then
            If chbAutoMeasure.Checked Then
                txbCmdWindowP2.Focus()
            Else
                txbPV_SN.Focus()
            End If
        Else

        End If
    End Sub

    Sub SetInputCnt()
        InputCnt = InputCntVal
        InputTimer.Enabled = True
    End Sub

    Function LoadDB() As Boolean
        Try
            ResetRegMeasure()
            RegMeasure.MeasurePV_SN = txtPVSN
            If LoadRegTD(RegMeasure.MeasurePV_SN) Then
                txbPV_Date.Text = "N/A"
                txbPV_Date.ForeColor = Color.Red
                txbPV_Freq.Text = "N/A"
                txbPV_Freq.ForeColor = Color.Red
                txbPV_Z.Text = "N/A"
                txbPV_Z.ForeColor = Color.Red
                txbPV_C.Text = "N/A"
                txbPV_C.ForeColor = Color.Red
                txbPV_R.Text = "N/A"
                txbPV_R.ForeColor = Color.Red
                cmdDisplay("T/D(SN:" + txtPVSN + ") Database loading failed.")
                txbPV_Date.Refresh()
                txbPV_Freq.Refresh()
                txbPV_Z.Refresh()
                txbPV_C.Refresh()
                txbPV_R.Refresh()
                Return True
            Else
                txbPV_Date.Text = RegTD_VALUE.PV_Date
                txbPV_Date.ForeColor = Color.Black
                txbPV_Freq.Text = RegTD_VALUE.PV_Frequency
                txbPV_Freq.ForeColor = Color.Black
                txbPV_Z.Text = RegTD_VALUE.PV_Impedance
                txbPV_Z.ForeColor = Color.Black
                txbPV_C.Text = RegTD_VALUE.PV_Capacitance
                txbPV_C.ForeColor = Color.Black
                txbPV_R.Text = RegTD_VALUE.PV_Resistance
                txbPV_R.ForeColor = Color.Black
                cmdDisplay("T/D(SN:" + RegTD_VALUE.PV_SN + ") DB loading succeeded.")
                txbPV_Date.Refresh()
                txbPV_Freq.Refresh()
                txbPV_Z.Refresh()
                txbPV_C.Refresh()
                txbPV_R.Refresh()
                Return False
            End If
        Catch ex As Exception
            Return True
        End Try
    End Function

    Sub LoadGrade()
        If (Len(txbPV_SN.Text) = LenPVSN Or Len(txbPV_SN.Text) = LenPVSN - 1) And Mid(txbPV_SN.Text, 1, 3) = NamePVSN Then
            txbGradeP2.Clear()
            txbCapacitorP2.Clear()

            cmd = "select TestStep,QC_Grade,QC_Capacitor,QC_Current from jomtCartridgeTbl where PV_SN = '" + txbPV_SN.Text + "';"
            If DBQueryReader(cmd) Then
                TestStep = Val(Mid(sqlDataQuery(0), 1, InStr(sqlDataQuery(0), " ")))
                txbGradeP2.Text = sqlDataQuery(1)
                txbCapacitorP2.Text = sqlDataQuery(2)
                QC_Current = Val(sqlDataQuery(3))
                'cmdDisplay("T/D(SN:" + txbPV_SN.Text + ") DB loading succeeded.")
            Else
                txbGradeP2.Text = "N/A"
                txbCapacitorP2.Text = "N/A"
                'cmdDisplay("T/D(SN:" + txbPV_SN.Text + ") Database loading failed.")
            End If

            txbGradeP2.Refresh()
            txbCapacitorP2.Refresh()
        Else
            ' cmdDisplay("T/D(SN:" + txbPV_SN.Text + ") Database loading failed.")
        End If
    End Sub
    Public Sub runSetupWindow()
        If EnableSetup Then
            loadSetupWindows()
        Else
            MsgBox("권한이 있는지 확인하세요.")
        End If
        frmLogin.Hide()
    End Sub


    Private Sub btnSetup_Click(sender As Object, e As EventArgs) Handles btnSetup.Click
        frmLogin.ShowDialog()
    End Sub

    Private Sub btnSetupExit_Click(sender As Object, e As EventArgs) Handles btnSetupExit.Click
        If ParametarLoad() Then
            initDevice()
            OpenSerial()
            gpbSetupWindow.Visible = False
            DefaultTextColor()
            btnSetup.Enabled = True

            Me.Size = New Size(1040, 784)
        Else
            End
        End If
    End Sub

    Private Sub btnSetupSave_Click(sender As Object, e As EventArgs) Handles btnSetupSave.Click
        Dim retVal As Integer

        retVal = MsgBox("설정을 저장하시겠습니까?", vbYesNo + vbExclamation, "DUALSONIC Configuation")

        If retVal = 6 Then

            Dim valueList As List(Of String) = New List(Of String)() From {nudmat_1.Value, nudmat_2.Value, nudmat_3.Value, nudmat_4.Value, nudmat_5.Value}

            If valueList.Count <> valueList.Distinct().Count Then
                MsgBox("C값 매칭 데이터 중 중복값이 존재합니다.", vbExclamation)
                Exit Sub
            End If

            Save_ini(False)
            PrintSaveData()
            DefaultTextColor()
        End If

    End Sub

    Private Sub txbMeasureDelay_TextChanged(sender As Object, e As EventArgs) Handles txbMeasureDelay.TextChanged
        txbMeasureDelay.ForeColor = Color.Red
    End Sub

    Private Sub txbTimeout_TextChanged(sender As Object, e As EventArgs) Handles txbTimeout.TextChanged
        txbTimeout.ForeColor = Color.Red
    End Sub

    Private Sub txbOntime_TextChanged(sender As Object, e As EventArgs) Handles txbOntime.TextChanged
        txbOntime.ForeColor = Color.Red
    End Sub

    Private Sub txbHSCALE_TextChanged(sender As Object, e As EventArgs) Handles txbHSCALE.TextChanged
        txbHSCALE.ForeColor = Color.Red
    End Sub

    Private Sub txbHPOS_TextChanged(sender As Object, e As EventArgs) Handles txbHPOS.TextChanged
        txbHPOS.ForeColor = Color.Red
    End Sub

    Private Sub txbVSCALE_TextChanged(sender As Object, e As EventArgs) Handles txbVSCALE.TextChanged
        txbVSCALE.ForeColor = Color.Red
    End Sub

    Private Sub txbVPOS_TextChanged(sender As Object, e As EventArgs) Handles txbVPOS.TextChanged
        txbVPOS.ForeColor = Color.Red
    End Sub

    Private Sub txbProMax_TextChanged(sender As Object, e As EventArgs) Handles txbProMax.TextChanged
        txbProMax.ForeColor = Color.Red
    End Sub

    Private Sub txbProTyp_TextChanged(sender As Object, e As EventArgs) Handles txbProTyp.TextChanged
        txbProTyp.ForeColor = Color.Red
    End Sub

    Private Sub txbProMin_TextChanged(sender As Object, e As EventArgs) Handles txbProMin.TextChanged
        txbProMin.ForeColor = Color.Red
    End Sub

    Private Sub txbLuxMax_TextChanged(sender As Object, e As EventArgs) Handles txbLuxMax.TextChanged
        txbLuxMax.ForeColor = Color.Red
    End Sub

    Private Sub txbLuxTyp_TextChanged(sender As Object, e As EventArgs) Handles txbLuxTyp.TextChanged
        txbLuxTyp.ForeColor = Color.Red
    End Sub

    Private Sub txbLuxMin_TextChanged(sender As Object, e As EventArgs) Handles txbLuxMin.TextChanged
        txbLuxMin.ForeColor = Color.Red
    End Sub

    Private Sub txbEyeMax_TextChanged(sender As Object, e As EventArgs) Handles txbEyeMax.TextChanged
        txbEyeMax.ForeColor = Color.Red
    End Sub

    Private Sub txbEyeTyp_TextChanged(sender As Object, e As EventArgs) Handles txbEyeTyp.TextChanged
        txbEyeTyp.ForeColor = Color.Red
    End Sub

    Private Sub txbEyeMin_TextChanged(sender As Object, e As EventArgs) Handles txbEyeMin.TextChanged
        txbEyeMin.ForeColor = Color.Red
    End Sub

    Private Sub txbRelay1_TextChanged(sender As Object, e As EventArgs) Handles txbRelay1.TextChanged
        txbRelay1.ForeColor = Color.Red
    End Sub

    Private Sub txbRelay2_TextChanged(sender As Object, e As EventArgs) Handles txbRelay2.TextChanged
        txbRelay2.ForeColor = Color.Red
    End Sub

    Private Sub txbRelay3_TextChanged(sender As Object, e As EventArgs) Handles txbRelay3.TextChanged
        txbRelay3.ForeColor = Color.Red
    End Sub

    Private Sub txbRelay4_TextChanged(sender As Object, e As EventArgs) Handles txbRelay4.TextChanged
        txbRelay4.ForeColor = Color.Red
    End Sub

    Private Sub txbRelay5_TextChanged(sender As Object, e As EventArgs) Handles txbRelay5.TextChanged
        txbRelay5.ForeColor = Color.Red
    End Sub
    Private Sub txbRelay6_TextChanged(sender As Object, e As EventArgs) Handles txbRelay6.TextChanged
        txbRelay6.ForeColor = Color.Red
    End Sub
    Private Sub txbRelay7_TextChanged(sender As Object, e As EventArgs) Handles txbRelay7.TextChanged
        txbRelay7.ForeColor = Color.Red
    End Sub
    Private Sub txbRelay8_TextChanged(sender As Object, e As EventArgs) Handles txbRelay8.TextChanged
        txbRelay8.ForeColor = Color.Red
    End Sub
    Private Sub txbRelay9_TextChanged(sender As Object, e As EventArgs) Handles txbRelay9.TextChanged
        txbRelay9.ForeColor = Color.Red
    End Sub
    Private Sub txbRelay10_TextChanged(sender As Object, e As EventArgs) Handles txbRelay10.TextChanged
        txbRelay10.ForeColor = Color.Red
    End Sub
    Private Sub txbScopeID_TextChanged(sender As Object, e As EventArgs) Handles txbScopeID.TextChanged
        txbScopeID.ForeColor = Color.Red
    End Sub
    Private Sub rdbIQC_CheckedChanged(sender As Object, e As EventArgs) Handles rdbIQC.CheckedChanged
        rdbIQC.ForeColor = Color.Red
        rdbProduct1.ForeColor = Color.Black
        rdbProduct2.ForeColor = Color.Black
        rdbProduct3.ForeColor = Color.Black
    End Sub
    Private Sub rdbProduct1_CheckedChanged(sender As Object, e As EventArgs) Handles rdbProduct1.CheckedChanged
        rdbIQC.ForeColor = Color.Black
        rdbProduct1.ForeColor = Color.Red
        rdbProduct2.ForeColor = Color.Black
        rdbProduct3.ForeColor = Color.Black
    End Sub
    Private Sub rdbProduct2_CheckedChanged(sender As Object, e As EventArgs) Handles rdbProduct2.CheckedChanged
        rdbIQC.ForeColor = Color.Black
        rdbProduct1.ForeColor = Color.Black
        rdbProduct2.ForeColor = Color.Red
        rdbProduct3.ForeColor = Color.Black
    End Sub
    Private Sub rdbProduct3_CheckedChanged(sender As Object, e As EventArgs) Handles rdbProduct3.CheckedChanged
        rdbIQC.ForeColor = Color.Black
        rdbProduct1.ForeColor = Color.Black
        rdbProduct2.ForeColor = Color.Black
        rdbProduct3.ForeColor = Color.Red
    End Sub
    Private Sub rdbAvg_CheckedChanged(sender As Object, e As EventArgs) Handles rdbAvg.CheckedChanged
        rdbAvg.ForeColor = Color.Red
        rdbMax.ForeColor = Color.Black
        rdbTop.ForeColor = Color.Black
    End Sub

    Private Sub rdbMax_CheckedChanged(sender As Object, e As EventArgs) Handles rdbMax.CheckedChanged
        rdbAvg.ForeColor = Color.Black
        rdbMax.ForeColor = Color.Red
        rdbTop.ForeColor = Color.Black
    End Sub

    Private Sub rdbTop_CheckedChanged(sender As Object, e As EventArgs) Handles rdbTop.CheckedChanged
        rdbAvg.ForeColor = Color.Black
        rdbMax.ForeColor = Color.Black
        rdbTop.ForeColor = Color.Red
    End Sub
    Private Sub DefaultTextColor()
        rdbIQC.ForeColor = Color.Black
        rdbProduct1.ForeColor = Color.Black
        rdbProduct2.ForeColor = Color.Black
        rdbProduct3.ForeColor = Color.Black
        txbMeasureDelay.ForeColor = Color.Black
        txbTimeout.ForeColor = Color.Black
        txbOntime.ForeColor = Color.Black
        txbHSCALE.ForeColor = Color.Black
        txbHPOS.ForeColor = Color.Black
        txbVSCALE.ForeColor = Color.Black
        txbVPOS.ForeColor = Color.Black
        txbProMax.ForeColor = Color.Black
        txbProTyp.ForeColor = Color.Black
        txbProMin.ForeColor = Color.Black
        txbLuxMax.ForeColor = Color.Black
        txbLuxTyp.ForeColor = Color.Black
        txbLuxMin.ForeColor = Color.Black
        txbEyeMax.ForeColor = Color.Black
        txbEyeTyp.ForeColor = Color.Black
        txbEyeMin.ForeColor = Color.Black
        txbRelay1.ForeColor = Color.Black
        txbRelay2.ForeColor = Color.Black
        txbRelay3.ForeColor = Color.Black
        txbRelay4.ForeColor = Color.Black
        txbRelay5.ForeColor = Color.Black
        txbRelay6.ForeColor = Color.Black
        txbRelay7.ForeColor = Color.Black
        txbRelay8.ForeColor = Color.Black
        txbRelay9.ForeColor = Color.Black
        txbRelay10.ForeColor = Color.Black
        txbScopeID.ForeColor = Color.Black
        rdbAvg.ForeColor = Color.Black
        rdbMax.ForeColor = Color.Black
        rdbTop.ForeColor = Color.Black
        rdbDev1.ForeColor = Color.Black
        rdbDev2.ForeColor = Color.Black
        rdbDev3.ForeColor = Color.Black
        txbErrorFactor.ForeColor = Color.Black
        txbOffsetValue.ForeColor = Color.Black
        txbWaitDelay.ForeColor = Color.Black
        txbStartWaitDelay.ForeColor = Color.Black
        txbValueError.ForeColor = Color.Black
        txbMotorOnTime.ForeColor = Color.Black
    End Sub

    Private Sub rdbDev1_CheckedChanged(sender As Object, e As EventArgs) Handles rdbDev1.CheckedChanged
        txbScopeID.Text = "0x1797::CN56526457"
        rdbDev1.ForeColor = Color.Red
        rdbDev2.ForeColor = Color.Black
        rdbDev3.ForeColor = Color.Black
    End Sub

    Private Sub rdbDev2_CheckedChanged(sender As Object, e As EventArgs) Handles rdbDev2.CheckedChanged
        txbScopeID.Text = "0x178B::CN59280175"
        rdbDev1.ForeColor = Color.Black
        rdbDev2.ForeColor = Color.Red
        rdbDev3.ForeColor = Color.Black
    End Sub

    Private Sub rdbDev3_CheckedChanged(sender As Object, e As EventArgs) Handles rdbDev3.CheckedChanged
        txbScopeID.Text = "0x178B::CN59280334"
        rdbDev1.ForeColor = Color.Black
        rdbDev2.ForeColor = Color.Black
        rdbDev3.ForeColor = Color.Red
    End Sub

    Private Sub btnSetupLoad_Click(sender As Object, e As EventArgs) Handles btnSetupLoad.Click
        Print_Read_ini()
        Read_ini()
    End Sub

    Private Sub RunProcess()
        showCmdWindow()
        Try
            If DevNum = 1 Then

                If chbAutoMeasure.Checked And CalModeOn = False Then
                    If ckbPlay.Checked = True Then
                        If txtPVSN.ToUpper = "[START]" Then
                            If RegMeasure.MeasurePV_SN = Nothing Then
                                MsgBox("측정하려는 TD의 데이터가 존재하지 않습니다. TD 시리얼 번호를 스캔하여 주십시오", vbExclamation, "DUALSONIC_TD_IQC_JIG")
                                Return
                            Else
                                txbRun.Text = "측정중"
                                txbRun.BackColor = Color.Khaki
                            End If
                        Else
                            SetDefaultText()

                            If Mid(txbPV_SN.Text, 1, 1).ToLower = "i" Or Mid(txbPV_SN.Text, 1, 1).ToLower = "h" Or Mid(txbPV_SN.Text, 1, 1).ToLower = "s" Then
                                txtPVSN = MySqlModule.NamePVSN & txbPV_SN.Text
                            Else
                                txtPVSN = txbPV_SN.Text
                            End If

                            If LoadDB() Then ' 자동측정 진행설명(DB로드 한 후 측정한 값 DB 저장)
                                RegMeasure.MeasurePV_SN = Nothing
                                txbRun.Text = "TD 정보 에러"
                                txbRun.BackColor = Color.Red
                                btnRunIQC.Enabled = False
                                txbPV_SN.Focus()
                            Else
                                txbRun.Text = "TD 정보 확인"
                                txbRun.BackColor = Color.Green
                                btnRunIQC.Enabled = True
                                txbPV_SN.Focus()
                            End If

                            Return

                        End If

                    Else
                        SetDefaultText()

                        If Mid(txbPV_SN.Text, 1, 1).ToLower = "i" Then
                            txtPVSN = MySqlModule.NamePVSN & Mid(txbPV_SN.Text, 2, txbPV_SN.Text.Length)
                        Else
                            txtPVSN = txbPV_SN.Text
                        End If

                        If LoadDB() Then ' 자동측정 진행설명(DB로드 한 후 측정한 값 DB 저장)
                            RegMeasure.MeasurePV_SN = Nothing
                            txbPV_SN.Focus()
                            Return
                        End If
                    End If
                End If
                RunProcessIQC()

            ElseIf DevNum = 2 Then
                PD1_STATUS = PD1_ENUM.STANDBY
                txtPVSN = txbPV_SN.Text
                SetDefaultText()
                RunProcessPD1()

            ElseIf DevNum = 3 Then

                ''LoadDB()
                ''If chbAutoMeasure.Checked Then
                ''    RunProcessPD1()
                ''End If

            ElseIf DevNum = 4 Then
                If chbAutoMeasure.Checked Then
                    LoadDB()
                    RunCartInfoReadPD3()
                End If
                RunProcessPD3()
            End If
        Catch nullable As NullReferenceException
            Exit Sub
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical)
        End Try

    End Sub
    Private Sub InputTimer_Tick(sender As Object, e As EventArgs) Handles InputTimer.Tick
        If InputCnt Then
            InputCnt -= 1
        Else
            InputTimer.Enabled = False
            If chbAutoMeasure.Checked Then
                PD1_STATUS = PD1_ENUM.STANDBY
                txtPVSN = Nothing
                txtPVSN = txbPV_SN.Text
                btnRunIQC_Click(sender, e)
            Else
                btnRunPD1.Focus()
                btnRunPD1.Refresh()
            End If
        End If
    End Sub

    Private Sub chbAutoMeasure_CheckedChanged(sender As Object, e As EventArgs) Handles chbAutoMeasure.CheckedChanged
        Dim tmpStr As String

        Select Case DevNum
            Case 1
                If chbAutoMeasure.Checked Then
                    btnRunIQC.Enabled = False
                    'btnRunIQC.Location = New Size(513, 410)
                    'btnRunIQC.Size = New Size(505, 95)
                    btnRunIQC.Refresh()

                    btnCal.Visible = True
                    btnCal.Enabled = True
                    btnRunIQCDB.Visible = False
                    btnRunIQCDB.Refresh()

                    chbUseBarcode.Checked = True
                    chbUseBarcode.Refresh()

                    txbPV_SN.BackColor = Color.White
                    txbPV_SN.Refresh()
                    txbPV_Freq.Text = "N/A"
                    txbPV_Freq.BackColor = Color.White
                    txbPV_Freq.Refresh()
                Else
                    btnRunIQC.Visible = True
                    btnRunIQC.Enabled = True
                    'btnRunIQC.Location = New Size(768, 410)
                    'btnRunIQC.Size = New Size(250, 95)
                    btnRunIQC.Refresh()

                    btnRunIQCDB.Visible = True
                    btnRunIQCDB.Enabled = True
                    btnCal.Visible = False
                    btnCal.Enabled = False
                    'btnRunIQCDB.Location = New Size(513, 410)
                    'btnRunIQCDB.Size = New Size(250, 95)
                    btnRunIQCDB.Refresh()

                    txbPV_SN.BackColor = Color.LightYellow
                    txbPV_SN.Refresh()
                    txbPV_Freq.Text = ""
                    txbPV_Freq.BackColor = Color.LightYellow
                    txbPV_Freq.Refresh()
                End If
            Case 2
                If chbAutoMeasure.Checked Then
                    If chbUseBarcode.Checked Then btnRunPD1.Enabled = False
                Else
                    btnRunPD1.Enabled = True
                End If
            Case 3
                If chbAutoMeasure.Checked Then
                    btnRunPD2.Enabled = False
                    DispToggle = True
                    txbPV_SN.Text = "대기중"
                    txbPV_SN.Refresh()
                Else
                    btnRunPD2.Enabled = True
                    txbPV_SN.BackColor = DefaultBackColor
                    DispToggle = False
                End If
            Case 4
                If chbAutoMeasure.Checked Then
                    lblGradePD3.Location = New Size(202, 35)
                    lblGradePD3.Size = New Size(147, 77)
                    lblGradePD3.Text = "등급"
                    lblGradePD3.Refresh()
                    txbGradePD3.Location = New Size(406, 25)
                    txbGradePD3.Size = New Size(384, 95)
                    txbGradePD3.Refresh()

                    lblCapacitorPD3.Location = New Size(202, 145)
                    lblCapacitorPD3.Size = New Size(148, 77)
                    lblCapacitorPD3.Refresh()
                    txbCapacitorPD3.Location = New Size(406, 135)
                    txbCapacitorPD3.Size = New Size(384, 95)
                    txbCapacitorPD3.Refresh()

                    lblCapSelectPD3.Visible = False
                    pnlCapSelectPD3.Visible = False

                    txbGradePD3.Text = "대기"
                    txbGradePD3.Refresh()
                    txbCapacitorPD3.Text = "대기"
                    txbCapacitorPD3.Refresh()
                    txbPV_Freq.Text = "N/A"
                    txbPV_Freq.BackColor = Color.White
                    txbPV_Freq.Refresh()
                    If chbUseBarcode.Checked = True Then
                        btnRunPD3.Enabled = False
                    Else
                        btnRunPD3.Enabled = True
                    End If
                Else
                    lblGradePD3.Location = New Size(10, 40)
                    lblGradePD3.Size = New Size(204, 77)
                    lblGradePD3.Text = "주파수"
                    lblGradePD3.Refresh()
                    txbGradePD3.Location = New Size(200, 30)
                    txbGradePD3.Size = New Size(320, 95)
                    txbGradePD3.Refresh()

                    lblCapacitorPD3.Location = New Size(535, 40)
                    lblCapacitorPD3.Size = New Size(148, 77)
                    lblCapacitorPD3.Refresh()
                    txbCapacitorPD3.Location = New Size(685, 30)
                    txbCapacitorPD3.Size = New Size(320, 95)
                    txbCapacitorPD3.Refresh()

                    lblCapSelectPD3.Visible = True
                    pnlCapSelectPD3.Visible = True

                    tmpStr = txbGradePD3.Text
                    txbPV_Freq.Text = ""
                    txbGradePD3.Text = tmpStr
                    txbGradePD3.Refresh()
                    txbPV_Freq.BackColor = Color.LightYellow
                    txbPV_Freq.Refresh()
                    btnRunPD3.Enabled = True
                End If
        End Select

        If chbUseBarcode.Checked And chbUseBarcode.Enabled Then
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub chbUseBarcode_CheckedChanged(sender As Object, e As EventArgs) Handles chbUseBarcode.CheckedChanged

        If DevNum = 1 Or DevNum = 2 Or DevNum = 3 Or DevNum = 4 Then
            If chbUseBarcode.Checked And chbAutoMeasure.Checked Then
                txbPV_SN.Focus()
                btnRunIQC.Enabled = False
                btnRunPD3.Enabled = False
            Else
                btnRunIQC.Enabled = True
                btnRunPD3.Enabled = True
            End If
        End If
    End Sub

    Private Sub btnRunIQC_Click(sender As Object, e As EventArgs) Handles btnRunIQC.Click

        txbGrade.Text = "대기"
        txbCapacitor.Text = "대기"
        txbGrade.BackColor = DefaultBackColor
        txbCapacitor.BackColor = DefaultBackColor

        If CalModeOn = True Then
            CalModeOn = False
            btnRunIQC.Text = "검사시작"
            txbCmdWindow.Visible = False
            gbTestWindow.Visible = True

            If chbAutoMeasure.Checked = True And chbUseBarcode.Checked = True Then
                btnRunIQC.Enabled = False
            Else
                btnRunIQC.Enabled = True
            End If

        Else

            If RegMeasure.MeasurePV_SN <> Nothing Then
                If txbPV_SN.Text = "[START]" Then

                    If ckbWarPnt.Checked = True Then
                        Dim RunThread As Thread = New Thread(New ParameterizedThreadStart(AddressOf print_on))
                        RunThread.IsBackground = True
                        RunThread.Start(RegMeasure.MeasurePV_SN)
                    End If

                    RunProcess()
                    RegMeasure.MeasurePV_SN = Nothing

                Else
                    RunProcess()
                End If
            Else
                RunProcess()
            End If
        End If

        showCmdWindow()

    End Sub

    Private Sub btnRunPD1_Click(sender As Object, e As EventArgs) Handles btnRunPD1.Click
        RunProcess()
    End Sub

    Private Sub btnRunP2_Click(sender As Object, e As EventArgs) Handles btnRunPD2.Click
        RunProcessPD2()
    End Sub


    Private Sub cmdRunTimer_Tick(sender As Object, e As EventArgs) Handles cmdRunTimer.Tick
        Try
            If cmdSerialPort.IsOpen = True Then
                If cmdSerialPort.BytesToRead <> 0 Then
                    cmdSerialPort.ReadTimeout = 0
                    rxMessage = cmdSerialPort.ReadLine()
                    If rxMessage <> "" Then
                        If DevNum = 2 And chbAutoMeasure.Checked Then
                            If Mid(rxMessage, 2, 10) <> "cartconn 0" And (PD1_STATUS = PD1_ENUM.WAIT Or PD1_STATUS = PD1_ENUM.WRITE) Then
                                RunProcessPD1()
                            End If
                        ElseIf DevNum = 3 And chbAutoMeasure.Checked Then
                            RunProcessPD2()
                        ElseIf DevNum = 4 Then
                            If Mid(rxMessage, 2, 8) = "setctype" Then
                                If Mid(rxMessage, 11, 1) = "1" Then
                                    CkbNoRun = True
                                    rdbEye.Checked = True
                                ElseIf Mid(rxMessage, 11, 1) = "2" Then
                                    CkbNoRun = True
                                    rdbFace.Checked = True
                                End If
                            ElseIf Mid(rxMessage, 2, 8) = "setdtype" Then
                                'DUAL : 1 , 3.0 : 2 , 4.5 : 3
                                If Mid(rxMessage, 11, 1) = "1" Then
                                    CkbNoRun = True
                                    rdbMode3.Checked = True
                                ElseIf Mid(rxMessage, 11, 1) = "2" Then
                                    CkbNoRun = True
                                    rdbMode1.Checked = True
                                ElseIf Mid(rxMessage, 11, 1) = "3" Then
                                    CkbNoRun = True
                                    rdbMode2.Checked = True
                                End If
                            ElseIf Mid(rxMessage, 2, 8) = "sethertz" Then
                                If Mid(rxMessage, 11, 1) = "1" Then
                                    CkbNoRun = True
                                    rdbMode1.Checked = True
                                ElseIf Mid(rxMessage, 11, 1) = "2" Then
                                    CkbNoRun = True
                                    rdbMode2.Checked = True
                                ElseIf Mid(rxMessage, 11, 1) = "3" Then
                                    CkbNoRun = True
                                    rdbMode3.Checked = True
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            'MsgBox(Err.Description)
        End Try
    End Sub

    Private Sub txbGradePD1_GotFocus(sender As Object, e As EventArgs) Handles txbGradePD1.GotFocus
        txbPV_SN.Focus()
    End Sub

    Private Sub txbCapacitorPD1_GotFocus(sender As Object, e As EventArgs) Handles txbCapacitorPD1.GotFocus
        txbPV_SN.Focus()
    End Sub

    Private Sub txbCartridgePD1_GotFocus(sender As Object, e As EventArgs) Handles txbCartridgePD1.GotFocus
        txbPV_SN.Focus()
    End Sub

    Private Sub btnRunIQCDB_Click(sender As Object, e As EventArgs) Handles btnRunIQCDB.Click
        LoadDB()
    End Sub

    Private Sub txbErrorFactor_TextChanged(sender As Object, e As EventArgs) Handles txbErrorFactor.TextChanged
        txbErrorFactor.ForeColor = Color.Red
    End Sub

    Private Sub txbOffsetValue_TextChanged(sender As Object, e As EventArgs) Handles txbOffsetValue.TextChanged
        txbOffsetValue.ForeColor = Color.Red
    End Sub

    Private Sub txbStartWaitDelay_TextChanged(sender As Object, e As EventArgs) Handles txbStartWaitDelay.TextChanged
        txbStartWaitDelay.ForeColor = Color.Red
    End Sub

    Private Sub txbWaitDelay_TextChanged(sender As Object, e As EventArgs) Handles txbWaitDelay.TextChanged
        txbWaitDelay.ForeColor = Color.Red
    End Sub

    Function GradeToValue(TmpGrade As String) As Integer
        If TmpGrade = "PRO" Then
            Return MODEL_NAME.PRO
        ElseIf TmpGrade = "LUX" Then
            Return MODEL_NAME.LUX
        ElseIf TmpGrade = "EYE" Then
            Return MODEL_NAME.EYE
        ElseIf TmpGrade = "MAX" Then
            Return MODEL_NAME.MAX
        Else
            Return 0
        End If
    End Function
    Private Sub SetDefaultText()
        'IQC
        txbTotalCnt.ForeColor = DefaultForeColor
        txbOKCnt.ForeColor = DefaultForeColor
        txbNGCnt.ForeColor = DefaultForeColor
        txbProCnt.ForeColor = DefaultForeColor
        txbLuxCnt.ForeColor = DefaultForeColor
        txbEyeCnt.ForeColor = DefaultForeColor
        txbR1CntIQC.ForeColor = DefaultForeColor
        txbR2CntIQC.ForeColor = DefaultForeColor
        txbR3CntIQC.ForeColor = DefaultForeColor
        txbR4CntIQC.ForeColor = DefaultForeColor
        txbR5CntIQC.ForeColor = DefaultForeColor
        txbR6CntIQC.ForeColor = DefaultForeColor
        txbR7CntIQC.ForeColor = DefaultForeColor
        txbR8CntIQC.ForeColor = DefaultForeColor
        txbR9CntIQC.ForeColor = DefaultForeColor
        txbR10CntIQC.ForeColor = DefaultForeColor
        txbReTotalCnt.ForeColor = DefaultForeColor
        txbReOkCnt.ForeColor = DefaultForeColor
        txbReNgCnt.ForeColor = DefaultForeColor

        txbTotalCnt.Refresh()
        txbOKCnt.Refresh()
        txbNGCnt.Refresh()
        txbProCnt.Refresh()
        txbLuxCnt.Refresh()
        txbEyeCnt.Refresh()
        txbR1CntIQC.Refresh()
        txbR2CntIQC.Refresh()
        txbR3CntIQC.Refresh()
        txbR4CntIQC.Refresh()
        txbR5CntIQC.Refresh()
        txbR6CntIQC.Refresh()
        txbR7CntIQC.Refresh()
        txbR8CntIQC.Refresh()
        txbR9CntIQC.Refresh()
        txbR10CntIQC.Refresh()
        txbReTotalCnt.Refresh()
        txbReOkCnt.Refresh()
        txbReNgCnt.Refresh()

        'PD1 txbOkCntPD1
        txbTotalCntPD1.ForeColor = Color.Black
        txbOkCntPD1.ForeColor = Color.Black
        txbNgCntPD1.ForeColor = Color.Black
        txbR1CntPD1.ForeColor = Color.Black
        txbR2CntPD1.ForeColor = Color.Black
        txbR3CntPD1.ForeColor = Color.Black
        txbR4CntPD1.ForeColor = Color.Black
        txbR5CntPD1.ForeColor = Color.Black
        txbR6CntPD1.ForeColor = Color.Black
        txbR7CntPD1.ForeColor = Color.Black
        txbR8CntPD1.ForeColor = Color.Black
        txbR9CntPD1.ForeColor = Color.Black
        txbR10CntPD1.ForeColor = Color.Black
        txbProCntPD1.ForeColor = Color.Black
        txbLuxCntPD1.ForeColor = Color.Black
        txbEyeCntPD1.ForeColor = Color.Black

        txbTotalCntPD1.Refresh()
        txbOkCntPD1.Refresh()
        txbNgCntPD1.Refresh()
        txbR1CntPD1.Refresh()
        txbR2CntPD1.Refresh()
        txbR3CntPD1.Refresh()
        txbR4CntPD1.Refresh()
        txbR5CntPD1.Refresh()
        txbR6CntPD1.Refresh()
        txbR7CntPD1.Refresh()
        txbR8CntPD1.Refresh()
        txbR9CntPD1.Refresh()
        txbR10CntPD1.Refresh()
        txbProCntPD1.Refresh()
        txbLuxCntPD1.Refresh()
        txbEyeCntPD1.Refresh()

        RelayTxb(1) = txbRelay1
        RelayTxb(2) = txbRelay2
        RelayTxb(3) = txbRelay3
        RelayTxb(4) = txbRelay4
        RelayTxb(5) = txbRelay5
        RelayTxb(6) = txbRelay6
        RelayTxb(7) = txbRelay7
        RelayTxb(8) = txbRelay8
        RelayTxb(9) = txbRelay9
        RelayTxb(10) = txbRelay10
    End Sub
    Private Sub txbTotalCnt_TextChanged(sender As Object, e As EventArgs) Handles txbTotalCnt.TextChanged
        If txbTotalCnt.Text > PreCnt_IQC.TotalCnt Then
            txbTotalCnt.ForeColor = Color.Red
        ElseIf txbTotalCnt.Text < PreCnt_IQC.TotalCnt Then
            txbTotalCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.TotalCnt = txbTotalCnt.Text
        txbTotalCnt.Refresh()
    End Sub

    Private Sub txbOKCnt_TextChanged(sender As Object, e As EventArgs) Handles txbOKCnt.TextChanged
        If Val(txbOKCnt.Text) > PreCnt_IQC.OKCnt Then
            txbOKCnt.ForeColor = Color.Red
        ElseIf Val(txbOKCnt.Text) < PreCnt_IQC.OKCnt Then
            txbOKCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.OKCnt = Val(txbOKCnt.Text)
        txbOKCnt.Refresh()
    End Sub

    Private Sub txbNGCnt_TextChanged(sender As Object, e As EventArgs) Handles txbNGCnt.TextChanged
        If Val(txbNGCnt.Text) > PreCnt_IQC.NGCnt Then
            txbNGCnt.ForeColor = Color.Red
        ElseIf Val(txbNGCnt.Text) < PreCnt_IQC.NGCnt Then
            txbNGCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.NGCnt = txbNGCnt.Text
        txbNGCnt.Refresh()
    End Sub
    Private Sub txbProCnt_TextChanged(sender As Object, e As EventArgs) Handles txbProCnt.TextChanged
        If Val(txbProCnt.Text) > PreCnt_IQC.ProCnt Then
            txbProCnt.ForeColor = Color.Red
        ElseIf Val(txbProCnt.Text) < PreCnt_IQC.ProCnt Then
            txbProCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.ProCnt = txbProCnt.Text
        txbProCnt.Refresh()
    End Sub

    Private Sub txbLuxCnt_TextChanged(sender As Object, e As EventArgs) Handles txbLuxCnt.TextChanged
        If Val(txbLuxCnt.Text) > PreCnt_IQC.LuxCnt Then
            txbLuxCnt.ForeColor = Color.Red
        ElseIf Val(txbLuxCnt.Text) < PreCnt_IQC.LuxCnt Then
            txbLuxCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.LuxCnt = txbLuxCnt.Text
        txbLuxCnt.Refresh()
    End Sub

    Private Sub txbEyeCnt_TextChanged(sender As Object, e As EventArgs) Handles txbEyeCnt.TextChanged
        If Val(txbEyeCnt.Text) > PreCnt_IQC.EyeCnt Then
            txbEyeCnt.ForeColor = Color.Red
        ElseIf Val(txbEyeCnt.Text) < PreCnt_IQC.EyeCnt Then
            txbEyeCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.EyeCnt = txbEyeCnt.Text
        txbEyeCnt.Refresh()
    End Sub

    Private Sub txbR1CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR1CntIQC.TextChanged
        If Val(txbR1CntIQC.Text) > PreCnt_IQC.R1Cnt Then
            txbR1CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR1CntIQC.Text) < PreCnt_IQC.R1Cnt Then
            txbR1CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R1Cnt = txbR1CntIQC.Text
        txbR1CntIQC.Refresh()
    End Sub

    Private Sub txbR2CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR2CntIQC.TextChanged
        If Val(txbR2CntIQC.Text) > PreCnt_IQC.R2Cnt Then
            txbR2CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR2CntIQC.Text) < PreCnt_IQC.R2Cnt Then
            txbR2CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R2Cnt = txbR2CntIQC.Text
        txbR2CntIQC.Refresh()
    End Sub

    Private Sub txbR3CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR3CntIQC.TextChanged
        If Val(txbR3CntIQC.Text) > PreCnt_IQC.R3Cnt Then
            txbR3CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR3CntIQC.Text) < PreCnt_IQC.R3Cnt Then
            txbR3CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R3Cnt = txbR3CntIQC.Text
        txbR3CntIQC.Refresh()
    End Sub

    Private Sub txbR4CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR4CntIQC.TextChanged
        If Val(txbR4CntIQC.Text) > PreCnt_IQC.R4Cnt Then
            txbR4CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR4CntIQC.Text) < PreCnt_IQC.R4Cnt Then
            txbR4CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R4Cnt = txbR4CntIQC.Text
        txbR4CntIQC.Refresh()
    End Sub

    Private Sub txbR5CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR5CntIQC.TextChanged
        If Val(txbR5CntIQC.Text) > PreCnt_IQC.R5Cnt Then
            txbR5CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR5CntIQC.Text) < PreCnt_IQC.R5Cnt Then
            txbR5CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R5Cnt = txbR5CntIQC.Text
        txbR5CntIQC.Refresh()
    End Sub

    Private Sub txbR6CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR6CntIQC.TextChanged
        If Val(txbR6CntIQC.Text) > PreCnt_IQC.R6Cnt Then
            txbR6CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR6CntIQC.Text) < PreCnt_IQC.R6Cnt Then
            txbR6CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R6Cnt = txbR6CntIQC.Text
        txbR6CntIQC.Refresh()
    End Sub

    Private Sub txbR7CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR7CntIQC.TextChanged
        If Val(txbR7CntIQC.Text) > PreCnt_IQC.R7Cnt Then
            txbR7CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR7CntIQC.Text) < PreCnt_IQC.R7Cnt Then
            txbR7CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R7Cnt = txbR7CntIQC.Text
        txbR7CntIQC.Refresh()
    End Sub

    Private Sub txbR8CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR8CntIQC.TextChanged
        If Val(txbR8CntIQC.Text) > PreCnt_IQC.R8Cnt Then
            txbR8CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR8CntIQC.Text) < PreCnt_IQC.R8Cnt Then
            txbR8CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R8Cnt = txbR8CntIQC.Text
        txbR8CntIQC.Refresh()
    End Sub

    Private Sub txbR9CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR9CntIQC.TextChanged
        If Val(txbR9CntIQC.Text) > PreCnt_IQC.R9Cnt Then
            txbR9CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR9CntIQC.Text) < PreCnt_IQC.R9Cnt Then
            txbR9CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R9Cnt = txbR9CntIQC.Text
        txbR9CntIQC.Refresh()
    End Sub

    Private Sub txbR10CntIQC_TextChanged(sender As Object, e As EventArgs) Handles txbR10CntIQC.TextChanged
        If Val(txbR10CntIQC.Text) > PreCnt_IQC.R10Cnt Then
            txbR10CntIQC.ForeColor = Color.Red
        ElseIf Val(txbR10CntIQC.Text) < PreCnt_IQC.R10Cnt Then
            txbR10CntIQC.ForeColor = Color.Blue
        End If
        PreCnt_IQC.R10Cnt = txbR10CntIQC.Text
        txbR10CntIQC.Refresh()
    End Sub

    Private Sub txbReTotalCnt_TextChanged(sender As Object, e As EventArgs) Handles txbReTotalCnt.TextChanged
        If Val(txbReTotalCnt.Text) > PreCnt_IQC.ReTotalCnt Then
            txbReTotalCnt.ForeColor = Color.Red
        ElseIf Val(txbReTotalCnt.Text) < PreCnt_IQC.ReTotalCnt Then
            txbReTotalCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.ReTotalCnt = txbReTotalCnt.Text
        txbReTotalCnt.Refresh()
    End Sub
    Private Sub txbReOkCnt_TextChanged(sender As Object, e As EventArgs) Handles txbReOkCnt.TextChanged
        If Val(txbReOkCnt.Text) > PreCnt_IQC.ReOkCnt Then
            txbReOkCnt.ForeColor = Color.Red
        ElseIf Val(txbReOkCnt.Text) < PreCnt_IQC.ReOkCnt Then
            txbReOkCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.ReOkCnt = txbReOkCnt.Text
        txbReOkCnt.Refresh()
    End Sub

    Private Sub txbReNgCnt_TextChanged(sender As Object, e As EventArgs) Handles txbReNgCnt.TextChanged
        If Val(txbReNgCnt.Text) > PreCnt_IQC.ReNgCnt Then
            txbReNgCnt.ForeColor = Color.Red
        ElseIf Val(txbReNgCnt.Text) < PreCnt_IQC.ReNgCnt Then
            txbReNgCnt.ForeColor = Color.Blue
        End If
        PreCnt_IQC.ReNgCnt = txbReNgCnt.Text
        txbReNgCnt.Refresh()
    End Sub

    Private Sub txbTotalCnt_GotFocus(sender As Object, e As EventArgs) Handles txbTotalCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbOKCnt_GotFocus(sender As Object, e As EventArgs) Handles txbOKCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbNGCnt_GotFocus(sender As Object, e As EventArgs) Handles txbNGCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbProCnt_GotFocus(sender As Object, e As EventArgs) Handles txbProCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbLuxCnt_GotFocus(sender As Object, e As EventArgs) Handles txbLuxCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbEyeCnt_GotFocus(sender As Object, e As EventArgs) Handles txbEyeCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbR1CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR1CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbR2CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR2CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbR3CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR3CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbR4CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR4CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbR5CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR5CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbR6CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR6CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbR7CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR7CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbR8CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR8CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbR9CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR9CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbR10CntIQC_GotFocus(sender As Object, e As EventArgs) Handles txbR10CntIQC.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbReTotalCnt_GotFocus(sender As Object, e As EventArgs) Handles txbReTotalCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub txbReOkCnt_GotFocus(sender As Object, e As EventArgs) Handles txbReOkCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub

    Private Sub txbReNgCnt_GotFocus(sender As Object, e As EventArgs) Handles txbReNgCnt.GotFocus
        pnlIQCDisp.Focus()
    End Sub
    Private Sub lblOntime_DoubleClick(sender As Object, e As EventArgs) Handles lblOntime.DoubleClick
        If ckbLogDetail.Visible = True Then
            ckbLogDetail.Visible = False
        Else
            ckbLogDetail.Visible = True
        End If
    End Sub

    Private Sub ckbLogDetail_CheckedChanged(sender As Object, e As EventArgs) Handles ckbLogDetail.CheckedChanged
        If ckbLogDetail.Checked Then
            ShowDetailLog = True
        Else
            ShowDetailLog = False
        End If
    End Sub

    Private Sub lblMesureDelaytime_DoubleClick(sender As Object, e As EventArgs) Handles lblMesureDelaytime.DoubleClick
        If ckbRunDemoMode.Visible = True Then
            ckbRunDemoMode.Visible = False
        Else
            ckbRunDemoMode.Visible = True
        End If
    End Sub
    Private Sub ckbRunDemoMode_CheckedChanged(sender As Object, e As EventArgs) Handles ckbRunDemoMode.CheckedChanged
        If ckbRunDemoMode.Checked Then
            RunDemoMode = True
        Else
            RunDemoMode = False
        End If
    End Sub

    Private Sub txbIQCOffsetValue_TextChanged(sender As Object, e As EventArgs) Handles txbValueError.TextChanged
        txbValueError.ForeColor = Color.Red
    End Sub

    Private Sub ckbUseInfoInput_CheckedChanged(sender As Object, e As EventArgs) Handles ckbUseInfoInput.CheckedChanged

        If ckbUseInfoInput.Checked Then
            lblGradePD1.Location = New Size(96, 29)
            txbGradePD1.Location = New Size(300, 14)

            lblCapacitorPD1.Location = New Size(96, 136)
            txbCapacitorPD1.Location = New Size(300, 121)

            lblCartridgePD1.Location = New Size(39, 243)
            txbCartridgePD1.Location = New Size(300, 228)

            gpbCheckGrade.Visible = True
        Else
            lblGradePD1.Location = New Size(202, 29)
            txbGradePD1.Location = New Size(406, 14)

            lblCapacitorPD1.Location = New Size(202, 136)
            txbCapacitorPD1.Location = New Size(406, 121)

            lblCartridgePD1.Location = New Size(145, 243)
            txbCartridgePD1.Location = New Size(406, 228)

            gpbCheckGrade.Visible = False
        End If

        lblGradePD1.Refresh()
        txbGradePD1.Refresh()
        lblCapacitorPD1.Refresh()
        txbCapacitorPD1.Refresh()
        lblCartridgePD1.Refresh()
        txbCartridgePD1.Refresh()
        gpbCheckGrade.Refresh()

    End Sub

    Private Sub cboGrade_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboGrade.SelectedIndexChanged
        txbPV_SN.Focus()
    End Sub

    Private Sub cboCapacitor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCapacitor.SelectedIndexChanged
        txbPV_SN.Focus()
    End Sub

    Private Sub gpbProduct1_Enter(sender As Object, e As EventArgs) Handles gpbProduct1.Enter
        If ckbUseInfoInput.Checked = False And chbAutoMeasure.Checked Then
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub txbTotalCntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbTotalCntPD1.TextChanged
        If txbTotalCntPD1.Text > PreCnt_PD1.TotalCnt Then
            txbTotalCntPD1.ForeColor = Color.Red
        ElseIf txbTotalCntPD1.Text < PreCnt_PD1.TotalCnt Then
            txbTotalCntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.TotalCnt = txbTotalCntPD1.Text
        txbTotalCntPD1.Refresh()
    End Sub

    Private Sub txbOkCntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbOkCntPD1.TextChanged
        If txbOkCntPD1.Text > PreCnt_PD1.OKCnt Then
            txbOkCntPD1.ForeColor = Color.Red
        ElseIf txbOkCntPD1.Text < PreCnt_PD1.OKCnt Then
            txbOkCntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.OKCnt = txbOkCntPD1.Text
        txbOkCntPD1.Refresh()
    End Sub

    Private Sub txbNgCntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbNgCntPD1.TextChanged
        If txbNgCntPD1.Text > PreCnt_PD1.NGCnt Then
            txbNgCntPD1.ForeColor = Color.Red
        ElseIf txbNgCntPD1.Text < PreCnt_PD1.NGCnt Then
            txbNgCntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.NGCnt = txbNgCntPD1.Text
        txbNgCntPD1.Refresh()
    End Sub

    Private Sub txbProCntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbProCntPD1.TextChanged
        If txbProCntPD1.Text > PreCnt_PD1.ProCnt Then
            txbProCntPD1.ForeColor = Color.Red
        ElseIf txbProCntPD1.Text < PreCnt_PD1.ProCnt Then
            txbProCntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.ProCnt = txbProCntPD1.Text
        txbProCntPD1.Refresh()
    End Sub

    Private Sub txbLuxCntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbLuxCntPD1.TextChanged
        If txbLuxCntPD1.Text > PreCnt_PD1.LuxCnt Then
            txbLuxCntPD1.ForeColor = Color.Red
        ElseIf txbLuxCntPD1.Text < PreCnt_PD1.LuxCnt Then
            txbLuxCntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.LuxCnt = txbLuxCntPD1.Text
        txbLuxCntPD1.Refresh()
    End Sub

    Private Sub txbEyeCntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbEyeCntPD1.TextChanged
        If txbEyeCntPD1.Text > PreCnt_PD1.EyeCnt Then
            txbEyeCntPD1.ForeColor = Color.Red
        ElseIf txbEyeCntPD1.Text < PreCnt_PD1.EyeCnt Then
            txbEyeCntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.EyeCnt = txbEyeCntPD1.Text
        txbEyeCntPD1.Refresh()
    End Sub

    Private Sub txbR1CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR1CntPD1.TextChanged
        If txbR1CntPD1.Text > PreCnt_PD1.R1Cnt Then
            txbR1CntPD1.ForeColor = Color.Red
        ElseIf txbR1CntPD1.Text < PreCnt_PD1.R1Cnt Then
            txbR1CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R1Cnt = txbR1CntPD1.Text
        txbR1CntPD1.Refresh()
    End Sub

    Private Sub txbR2CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR2CntPD1.TextChanged
        If txbR2CntPD1.Text > PreCnt_PD1.R2Cnt Then
            txbR2CntPD1.ForeColor = Color.Red
        ElseIf txbR2CntPD1.Text < PreCnt_PD1.R2Cnt Then
            txbR2CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R2Cnt = txbR2CntPD1.Text
        txbR2CntPD1.Refresh()
    End Sub

    Private Sub txbR3CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR3CntPD1.TextChanged
        If txbR3CntPD1.Text > PreCnt_PD1.R3Cnt Then
            txbR3CntPD1.ForeColor = Color.Red
        ElseIf txbR3CntPD1.Text < PreCnt_PD1.R3Cnt Then
            txbR3CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R3Cnt = txbR3CntPD1.Text
        txbR3CntPD1.Refresh()
    End Sub

    Private Sub txbR4CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR4CntPD1.TextChanged
        If txbR4CntPD1.Text > PreCnt_PD1.R4Cnt Then
            txbR4CntPD1.ForeColor = Color.Red
        ElseIf txbR4CntPD1.Text < PreCnt_PD1.R4Cnt Then
            txbR4CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R4Cnt = txbR4CntPD1.Text
        txbR4CntPD1.Refresh()
    End Sub

    Private Sub txbR5CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR5CntPD1.TextChanged
        If txbR5CntPD1.Text > PreCnt_PD1.R5Cnt Then
            txbR5CntPD1.ForeColor = Color.Red
        ElseIf txbR5CntPD1.Text < PreCnt_PD1.R5Cnt Then
            txbR5CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R5Cnt = txbR5CntPD1.Text
        txbR5CntPD1.Refresh()
    End Sub
    Private Sub txbR6CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR6CntPD1.TextChanged
        If txbR6CntPD1.Text > PreCnt_PD1.R6Cnt Then
            txbR6CntPD1.ForeColor = Color.Red
        ElseIf txbR6CntPD1.Text < PreCnt_PD1.R6Cnt Then
            txbR6CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R6Cnt = txbR6CntPD1.Text
        txbR6CntPD1.Refresh()
    End Sub
    Private Sub txbR7CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR7CntPD1.TextChanged
        If txbR7CntPD1.Text > PreCnt_PD1.R7Cnt Then
            txbR7CntPD1.ForeColor = Color.Red
        ElseIf txbR7CntPD1.Text < PreCnt_PD1.R7Cnt Then
            txbR7CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R7Cnt = txbR7CntPD1.Text
        txbR7CntPD1.Refresh()
    End Sub
    Private Sub txbR8CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR8CntPD1.TextChanged
        If txbR8CntPD1.Text > PreCnt_PD1.R8Cnt Then
            txbR8CntPD1.ForeColor = Color.Red
        ElseIf txbR8CntPD1.Text < PreCnt_PD1.R8Cnt Then
            txbR8CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R8Cnt = txbR8CntPD1.Text
        txbR8CntPD1.Refresh()
    End Sub
    Private Sub txbR9CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR9CntPD1.TextChanged
        If txbR9CntPD1.Text > PreCnt_PD1.R9Cnt Then
            txbR9CntPD1.ForeColor = Color.Red
        ElseIf txbR9CntPD1.Text < PreCnt_PD1.R9Cnt Then
            txbR9CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R9Cnt = txbR9CntPD1.Text
        txbR9CntPD1.Refresh()
    End Sub
    Private Sub txbR10CntPD1_TextChanged(sender As Object, e As EventArgs) Handles txbR10CntPD1.TextChanged
        If txbR10CntPD1.Text > PreCnt_PD1.R10Cnt Then
            txbR10CntPD1.ForeColor = Color.Red
        ElseIf txbR10CntPD1.Text < PreCnt_PD1.R10Cnt Then
            txbR10CntPD1.ForeColor = Color.Blue
        End If
        PreCnt_PD1.R10Cnt = txbR10CntPD1.Text
        txbR10CntPD1.Refresh()
    End Sub

    Public Sub UpdateCartType()
        CartType = CART_TYPE.CART_NONE

        If ckbUseGradeChange.Checked Then
            If rdbModelPro.Checked And rdbTypeFace.Checked Then
                CartType = CART_TYPE.PRO_FACE
            ElseIf rdbModelPro.Checked And rdbTypeEye.Checked Then
                CartType = CART_TYPE.PRO_EYE
            ElseIf rdbModelLux.Checked And rdbTypeFace.Checked Then
                CartType = CART_TYPE.LUX_FACE
            ElseIf rdbModelLux.Checked And rdbTypeEye.Checked Then
                CartType = CART_TYPE.LUX_EYE
            End If
        Else
            If rdbModelPro.Checked Then
                CartType = CART_TYPE.PRO_EYE
            ElseIf rdbModelLux.Checked Then
                CartType = CART_TYPE.LUX_EYE
            End If
        End If

        txbModelSelect.Text = CartTypeName(CartType)
        txbModelSelect.Refresh()

    End Sub
    Public Sub UpdateCartTypeWindow()

        CartType = CART_TYPE.CART_NONE

        If ckbUseGradeChange.Checked Then
            If frmModelSelect.rdbModelProWindow.Checked And frmModelSelect.rdbTypeFaceWindow.Checked Then
                CartType = CART_TYPE.PRO_FACE
            ElseIf frmModelSelect.rdbModelProWindow.Checked And frmModelSelect.rdbTypeEyeWindow.Checked Then
                CartType = CART_TYPE.PRO_EYE
            ElseIf frmModelSelect.rdbModelLuxWindow.Checked And frmModelSelect.rdbTypeFaceWindow.Checked Then
                CartType = CART_TYPE.LUX_FACE
            ElseIf frmModelSelect.rdbModelLuxWindow.Checked And frmModelSelect.rdbTypeEyeWindow.Checked Then
                CartType = CART_TYPE.LUX_EYE
            End If
        Else
            If frmModelSelect.rdbModelProWindow.Checked Then
                CartType = CART_TYPE.PRO_EYE
            ElseIf frmModelSelect.rdbModelLuxWindow.Checked Then
                CartType = CART_TYPE.LUX_EYE
            End If
        End If

        frmModelSelect.txbModelSelectWindow.Text = CartTypeName(CartType)
        frmModelSelect.txbModelSelectWindow.Refresh()

        If ckbUseGradeChange.Checked Then
            If frmModelSelect.rdbModelProWindow.Checked Or frmModelSelect.rdbModelLuxWindow.Checked Then
                If frmModelSelect.rdbTypeFaceWindow.Checked = False And frmModelSelect.rdbTypeEyeWindow.Checked = False Then
                    frmModelSelect.lblModelSelectTitle.Text = "카트리지 타입을 선택해주세요!"
                End If
            ElseIf frmModelSelect.rdbTypeFaceWindow.Checked Or frmModelSelect.rdbTypeEyeWindow.Checked Then
                If frmModelSelect.rdbModelProWindow.Checked = False And frmModelSelect.rdbTypeFaceWindow.Checked = False Then
                    frmModelSelect.lblModelSelectTitle.Text = "카트리지 모델을 선택해주세요!"
                End If
            End If
        End If

        If CartType Then
            frmModelSelect.lblModelSelectTitle.Text = "  사양 선택이 완료되었습니다!"
            frmModelSelect.Refresh()
            'DelayMs(5000)
            'frmModelSelect.Close()
        End If

    End Sub

    Private Sub ckbUseGradeChange_CheckedChanged(sender As Object, e As EventArgs) Handles ckbUseGradeChange.CheckedChanged
        If ckbUseGradeChange.Checked Then
            pnlModelSelect.Location = New Size(6, 148)
            pnlTypeSelect.Location = New Size(6, 180)
            pnlTypeSelect.Visible = True
            frmModelSelect.pnlModelSelectWindow.Location = New Size(1, 312)
            frmModelSelect.pnlTypeSelectWindow.Location = New Size(1, 412)
            frmModelSelect.pnlTypeSelectWindow.Visible = True
            gpbCartType.Text = "카트리지 모델 선택 설정"
        Else
            pnlModelSelect.Location = New Size(6, 164)
            pnlTypeSelect.Visible = False
            frmModelSelect.pnlModelSelectWindow.Location = New Size(1, 362)
            frmModelSelect.pnlTypeSelectWindow.Visible = False
            gpbCartType.Text = "EYE 카트리지 모델 선택 설정"
        End If

        gpbCartType.Refresh()
        pnlModelSelect.Refresh()
        pnlTypeSelect.Refresh()
        frmModelSelect.pnlModelSelectWindow.Refresh()
        frmModelSelect.pnlTypeSelectWindow.Refresh()
        frmModelSelect.pnlTypeSelectWindow.Refresh()
    End Sub

    Private Sub rdbModelPro_CheckedChanged(sender As Object, e As EventArgs) Handles rdbModelPro.CheckedChanged
        If rdbModelPro.Checked Then
            rdbModelPro.ForeColor = Color.Red
        Else
            rdbModelPro.ForeColor = Color.Black
        End If
        rdbModelPro.Refresh()
        UpdateCartType()
    End Sub

    Private Sub rdbModelLux_CheckedChanged(sender As Object, e As EventArgs) Handles rdbModelLux.CheckedChanged
        If rdbModelLux.Checked Then
            rdbModelLux.ForeColor = Color.Red
        Else
            rdbModelLux.ForeColor = Color.Black
        End If
        rdbModelLux.Refresh()
        UpdateCartType()
    End Sub

    Private Sub rdbTypeFace_CheckedChanged(sender As Object, e As EventArgs) Handles rdbTypeFace.CheckedChanged
        If rdbTypeFace.Checked Then
            rdbTypeFace.ForeColor = Color.Red
        Else
            rdbTypeFace.ForeColor = Color.Black
        End If
        rdbTypeFace.Refresh()
        UpdateCartType()
    End Sub

    Private Sub rdbTypeEye_CheckedChanged(sender As Object, e As EventArgs) Handles rdbTypeEye.CheckedChanged
        If rdbTypeEye.Checked Then
            rdbTypeEye.ForeColor = Color.Red
        Else
            rdbTypeEye.ForeColor = Color.Black
        End If
        rdbTypeEye.Refresh()
        UpdateCartType()
    End Sub

    Private Sub ckbUseModelSelect_CheckedChanged(sender As Object, e As EventArgs) Handles ckbUseModelSelect.CheckedChanged

        If ckbUseModelSelect.Checked Then
            lblGradePD1.Location = New Size(202, 29)
            txbGradePD1.Location = New Size(406, 14)

            lblCapacitorPD1.Location = New Size(202, 136)
            txbCapacitorPD1.Location = New Size(406, 121)

            lblCartridgePD1.Location = New Size(145, 243)
            txbCartridgePD1.Location = New Size(406, 228)

            gpbCartType.Visible = False
        Else
            lblGradePD1.Location = New Size(96, 29)
            txbGradePD1.Location = New Size(300, 14)

            lblCapacitorPD1.Location = New Size(96, 136)
            txbCapacitorPD1.Location = New Size(300, 121)

            lblCartridgePD1.Location = New Size(39, 243)
            txbCartridgePD1.Location = New Size(300, 228)

            gpbCartType.Visible = True
        End If

        lblGradePD1.Refresh()
        txbGradePD1.Refresh()
        lblCapacitorPD1.Refresh()
        txbCapacitorPD1.Refresh()
        lblCartridgePD1.Refresh()
        txbCartridgePD1.Refresh()
        gpbCheckGrade.Refresh()
    End Sub

    Private Sub AutoAgingTimer_Tick(sender As Object, e As EventArgs) Handles AutoAgingTimer.Tick

        If AutoAgingCnt > 0 Then
            AutoAgingCnt -= 1
            txbAgingWait.Text = AutoAgingCnt
            txbAgingWait.Refresh()
        Else
            AutoAgingCnt = Val(txbAutoInterval.Text)
            txbAgingWait.Text = AutoAgingCnt
            txbAgingWait.Refresh()
            AutoAgingTimer.Enabled = False
            RunProcess()
            txbShotCnt.Text += 1
            AutoAgingTimer.Enabled = True
        End If

    End Sub

    Private Sub btnAutoAging_Click(sender As Object, e As EventArgs) Handles btnAutoAging.Click
        If btnAutoAging.Text = "자동시작" Then
            AutoAgingTimer.Enabled = True
            btnAutoAging.Text = "자동멈춤"
        Else
            AutoAgingTimer.Enabled = False
            btnAutoAging.Text = "자동시작"
            AutoAgingCnt = 0
        End If
        btnAutoAging.Refresh()
    End Sub

    Private Sub btnRunPD3_Click(sender As Object, e As EventArgs) Handles btnRunPD3.Click
        RunProcess()
    End Sub

    Private Sub rdbCapacitor1_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor1.CheckedChanged
        If (rdbCapacitor1.Checked) Then txbCapacitorPD3.Text = "0"
    End Sub

    Private Sub rdbCapacitor2_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor2.CheckedChanged
        If (rdbCapacitor2.Checked) Then txbCapacitorPD3.Text = "22"
    End Sub

    Private Sub rdbCapacitor3_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor3.CheckedChanged
        If (rdbCapacitor3.Checked) Then txbCapacitorPD3.Text = "56"
    End Sub

    Private Sub rdbCapacitor4_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor4.CheckedChanged
        If (rdbCapacitor4.Checked) Then txbCapacitorPD3.Text = "82"
    End Sub

    Private Sub rdbCapacitor5_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor5.CheckedChanged
        If (rdbCapacitor5.Checked) Then txbCapacitorPD3.Text = "100"
    End Sub

    Private Sub rdbCapacitor6_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor6.CheckedChanged
        If (rdbCapacitor6.Checked) Then txbCapacitorPD3.Text = "120"
    End Sub

    Private Sub rdbCapacitor7_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor7.CheckedChanged
        If (rdbCapacitor7.Checked) Then txbCapacitorPD3.Text = "150"
    End Sub

    Private Sub rdbCapacitor8_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor8.CheckedChanged
        If (rdbCapacitor8.Checked) Then txbCapacitorPD3.Text = "180"
    End Sub
    Private Sub rdbCapacitor9_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor9.CheckedChanged
        If (rdbCapacitor9.Checked) Then txbCapacitorPD3.Text = "220"
    End Sub
    Private Sub rdbCapacitor10_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCapacitor10.CheckedChanged
        If (rdbCapacitor10.Checked) Then txbCapacitorPD3.Text = "270"
    End Sub

    Private Sub txbPV_Freq_TextChanged(sender As Object, e As EventArgs) Handles txbPV_Freq.TextChanged
        If chbAutoMeasure.Checked = False And DevNum = 4 Then
            txbGradePD3.Text = txbPV_Freq.Text
        End If
    End Sub

    Private Sub txbGradePD3_GotFocus(sender As Object, e As EventArgs) Handles txbGradePD3.GotFocus
        gpbProduct3.Focus()
    End Sub

    Private Sub txbCapacitorPD3_GotFocus(sender As Object, e As EventArgs) Handles txbCapacitorPD3.GotFocus
        gpbProduct3.Focus()
    End Sub

    Private Sub rdbFace_CheckedChanged(sender As Object, e As EventArgs) Handles rdbFace.CheckedChanged
        lblMode.Text = "모드"
        rdbMode1.Checked = True
        rdbMode1.Text = "3.0"
        rdbMode1.Refresh()
        rdbMode2.Checked = False
        rdbMode2.Text = "4.5"
        rdbMode2.Refresh()
        rdbMode3.Checked = False
        rdbMode3.Text = "Dual"
        rdbMode3.Refresh()

        'EYE_CART : 1 , FACE_CART : 2
        If CkbNoRun = False Then
            If SetCartType(2) Then
                MsgBox("카트리지 정보 설정 실패.")
            End If
        End If
        CkbNoRun = False

        If chbUseBarcode.Checked Then
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub rdbEye_CheckedChanged(sender As Object, e As EventArgs) Handles rdbEye.CheckedChanged
        lblMode.Text = "속도"
        rdbMode1.Checked = False
        rdbMode1.Text = "1Hz"
        rdbMode1.Refresh()
        rdbMode2.Checked = False
        rdbMode2.Text = "2Hz"
        rdbMode2.Refresh()
        rdbMode3.Checked = True
        rdbMode3.Text = "3Hz"
        rdbMode3.Refresh()

        'EYE_CART : 1 , FACE_CART : 2
        If CkbNoRun = False Then
            If SetCartType(1) Then
                MsgBox("카트리지 정보 설정 실패.")
            End If
        End If
        CkbNoRun = False

        If chbUseBarcode.Checked Then
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub gpbProduct3_GotFocus(sender As Object, e As EventArgs) Handles gpbProduct3.GotFocus
        If chbAutoMeasure.Checked Then
            txbPV_SN.Focus()
        End If
    End Sub

    Private Sub rdbMode1_CheckedChanged(sender As Object, e As EventArgs) Handles rdbMode1.CheckedChanged
        If CkbNoRun = False Then
            If rdbFace.Checked Then
                'DUAL : 1 , 3.0 : 2 , 4.5 : 3
                If SetDepthType(2) Then
                    MsgBox("카트리지 정보 설정 실패.")
                End If
            ElseIf rdbEye.Checked Then
                '1Hz : 1 , 2Hz : 2 , 3Hz : 3
                If SetHertzType(1) Then
                    MsgBox("카트리지 정보 설정 실패.")
                End If
            End If
        End If
        CkbNoRun = False

        txbPV_SN.Focus()
    End Sub

    Private Sub rdbMode2_CheckedChanged(sender As Object, e As EventArgs) Handles rdbMode2.CheckedChanged
        If CkbNoRun = False Then
            If rdbFace.Checked Then
                'DUAL : 1 , 3.0 : 2 , 4.5 : 3
                If SetDepthType(3) Then
                    MsgBox("카트리지 정보 설정 실패.")
                End If
            ElseIf rdbEye.Checked Then
                '1Hz : 1 , 2Hz : 2 , 3Hz : 3
                If SetHertzType(2) Then
                    MsgBox("카트리지 정보 설정 실패.")
                End If
            End If
        End If
        CkbNoRun = False

        txbPV_SN.Focus()
    End Sub

    Private Sub rdbMode3_CheckedChanged(sender As Object, e As EventArgs) Handles rdbMode3.CheckedChanged
        If CkbNoRun = False Then
            If rdbFace.Checked Then
                'DUAL : 1 , 3.0 : 2 , 4.5 : 3
                If SetDepthType(1) Then
                    MsgBox("카트리지 정보 설정 실패.")
                End If
            ElseIf rdbEye.Checked Then
                '1Hz : 1 , 2Hz : 2 , 3Hz : 3
                If SetHertzType(3) Then
                    MsgBox("카트리지 정보 설정 실패.")
                End If
            End If
        End If
        CkbNoRun = False

        txbPV_SN.Focus()
    End Sub

    Private Sub txbMotorOnTime_TextChanged(sender As Object, e As EventArgs) Handles txbMotorOnTime.TextChanged
        txbMotorOnTime.ForeColor = Color.Red
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ReDim RegMeasure.MeasureValue(11)

        For i = 0 To 10

        Next

        RegMeasure.MeasureValue(0) = 0
        RegMeasure.MeasureValue(1) = TextBox2.Text
        RegMeasure.MeasureValue(2) = TextBox27.Text
        RegMeasure.MeasureValue(3) = TextBox26.Text
        RegMeasure.MeasureValue(4) = TextBox25.Text
        RegMeasure.MeasureValue(5) = TextBox24.Text
        RegMeasure.MeasureValue(6) = TextBox23.Text
        RegMeasure.MeasureValue(7) = TextBox22.Text
        RegMeasure.MeasureValue(8) = TextBox21.Text
        RegMeasure.MeasureValue(9) = TextBox20.Text
        RegMeasure.MeasureValue(10) = TextBox19.Text

        'For Cnt = 1 To 9
        '    If RegMeasure.MeasureValue(Cnt) <= ProMAX And RegMeasure.MeasureValue(0) = 0 Then RegMeasure.MeasureValue(0) = Cnt

        '    If RegMeasure.MeasureValue(0) Then
        '        If RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) < RegMeasure.MeasureValue(Cnt + 1) Then
        '            If RegMeasure.MeasureValue(Cnt + 1) <= ProMAX Then
        '                RegMeasure.MeasureValue(0) = Cnt + 1
        '            Else
        '                If MySqlModule.RegMeasure.MeasureValue(Cnt) <= ProMAX Then RegMeasure.MeasureValue(0) = Cnt
        '            End If
        '        End If
        '    End If
        'Next


        'For Cnt = 1 To 10
        '    If RegMeasure.MeasureValue(Cnt) <= ProMAX And RegMeasure.MeasureValue(0) = 0 Then RegMeasure.MeasureValue(0) = Cnt

        '    If RegMeasure.MeasureValue(0) Then
        '        If RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) < RegMeasure.MeasureValue(Cnt) Then
        '            If RegMeasure.MeasureValue(Cnt) <= ProMAX Then
        '                RegMeasure.MeasureValue(0) = Cnt
        '            End If
        '        End If
        '    End If

        'Next


        If RegMeasure.MeasureValue(0) = 0 Then
            RegMeasure.MeasureOkNg = "NG"
            RegMeasure.MeasureGrade = "OVER"
            txbGrade.Text = RegMeasure.MeasureGrade
            RegMeasure.MeasureValue(0) = 0
            txbGrade.BackColor = Color.Red
        ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= ProMIN Then
            RegMeasure.MeasureOkNg = "OK"
            RegMeasure.MeasureGrade = "PRO"
            txbGrade.Text = RegMeasure.MeasureGrade
            txbGrade.BackColor = Color.Green
        ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= LuxMIN Then
            RegMeasure.MeasureOkNg = "OK"
            RegMeasure.MeasureGrade = "LUX"
            txbGrade.Text = RegMeasure.MeasureGrade
            txbGrade.BackColor = Color.Green
        ElseIf RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) >= EyeMIN Then
            RegMeasure.MeasureOkNg = "OK"
            RegMeasure.MeasureGrade = "EYE"
            txbGrade.Text = RegMeasure.MeasureGrade
            txbGrade.BackColor = Color.Green
        Else
            RegMeasure.MeasureOkNg = "NG"
            RegMeasure.MeasureGrade = "UNDER"
            txbGrade.Text = RegMeasure.MeasureGrade
            RegMeasure.MeasureValue(0) = 0
            txbGrade.BackColor = Color.Red
        End If
        txbGrade.Refresh()

        RegMeasure.MeasureCapacitor = RELAY_C(RegMeasure.MeasureValue(0))
        txbCapacitor.Text = RegMeasure.MeasureCapacitor
        If RegMeasure.MeasureValue(0) Then
            txbCapacitor.BackColor = Color.Green
        Else
            txbCapacitor.BackColor = Color.Red
        End If
        txbCapacitor.Refresh()
    End Sub

    Private Sub btnCal_Click(sender As Object, e As EventArgs) Handles btnCal.Click
        If CalModeOn = False Then
            CalModeOn = True
            'txbCmdWindow.Clear() 'Commenting out to check logs
            txbCmdWindow.Visible = True
            gbTestWindow.Visible = False
            btnRunIQC.Enabled = True
            btnRunIQC.Text = "완료"
        End If

        txbPV_Freq.Text = "7000"
        RegMeasure.MeasurePV_SN = "DITTEST"

        RunProcess()

        btnRunIQC.Enabled = True
        txbPV_Freq.Text = "N/A"

        resizeCmdWindow(505, 380, 513, 21)
    End Sub

    Private Sub btPrintref_Click(sender As Object, e As EventArgs) Handles btPrintref.Click
        Dim lvi(1) As String
        Dim alList As System.Collections.ArrayList = New System.Collections.ArrayList(PrinterSettings.InstalledPrinters)

        alList.Sort()
        dgvPrinter.Rows.Clear()

        For i = 0 To alList.Count - 1
            lvi(0) = i + 1
            lvi(1) = alList(i).ToString

            dgvPrinter.Rows.Add(lvi)
        Next
    End Sub

    Private Sub btnName1_Click(sender As Object, e As EventArgs) Handles btnName1.Click
        Dim row As DataGridViewRow = dgvPrinter.SelectedRows(0)
        Dim printname As String = row.Cells(1).Value.ToString
        txbPrintApply.Text = printname
    End Sub

    Private Sub btnLabel1_Click(sender As Object, e As EventArgs) Handles btnLabel1.Click
        Dim fileAddress As String = openfile()

        If fileAddress <> "fail" Then
            txbLabel1.Text = fileAddress
        End If
    End Sub

    Private Function openfile() As String
        Dim ofd As OpenFileDialog = New OpenFileDialog
        ofd.Title = "라벨 파일 주소 불러오기"
        ofd.FileName = ""
        ofd.Filter = "라벨 파일(*.lbx) | *.lbx"

        Dim dr As DialogResult = ofd.ShowDialog

        If dr = DialogResult.OK Then
            Return ofd.FileName
        End If

        Return "fail"

    End Function

    Private Sub Mqtt_DataSend(loadText As String)
        Try

            If loadText.Replace(" ", "") = "" Then
                MsgBox("저장할 데이터가 존재하지 않습니다. 데이터를 확인하여 주십시오.", vbCritical)
                Exit Sub
            End If
            If MQTClient IsNot Nothing AndAlso MQTClient.IsConnected Then
                If mqttTopic.Length <> 0 Then
                    Try
                        Dim qos As Byte = 0
                        MQTClient.Publish(mqttTopic, System.Text.Encoding.Default.GetBytes(loadText), qos, False)
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical)
                    End Try
                Else
                    MsgBox("토픽을 지정하지 않았습니다. 토픽을 지정하여 주십시오.", vbExclamation)
                End If
            Else
                MsgBox("Broker 호스트와 연결되지 않았습니다. 호스트와 연결하여 주십시오.", vbExclamation)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frmTD_JIG_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        End
    End Sub

    Private Sub lblTime_DoubleClick(sender As Object, e As EventArgs) Handles lblTime.DoubleClick
        MsgBox("MQTT 정보" + vbCrLf + "MQTT Broker : " + mqttBroker + vbCrLf + "MQTT Topic : " + mqttTopic + vbCrLf + "DUALSONIC VER 3.2.2", vbInformation)
    End Sub

    Private Sub rdbCmat_5_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCmat_5.CheckedChanged
        If rdbCmat_5.Checked = True Then
            nudmat_1.Enabled = True
            nudmat_2.Enabled = True
            nudmat_3.Enabled = True
            nudmat_4.Enabled = True
            nudmat_5.Enabled = True

            ckbSerialUse.Checked = False
            ckbSerialUse.Enabled = False
            btnGradeStop.Enabled = False
            gbDSMode.Enabled = False
            GradeStop = 3
            SettingMeasure = String.Empty
        End If
    End Sub

    Private Sub rdbCmat_10_CheckedChanged(sender As Object, e As EventArgs) Handles rdbCmat_10.CheckedChanged
        If rdbCmat_10.Checked = True Then
            nudmat_1.Enabled = False
            nudmat_2.Enabled = False
            nudmat_3.Enabled = False
            nudmat_4.Enabled = False
            nudmat_5.Enabled = False

            ckbSerialUse.Checked = True
            ckbSerialUse.Enabled = True

            If ckbSerialUse.Checked = True Then
                btnGradeStop.Enabled = True
                gbDSMode.Enabled = True
            End If

        End If
    End Sub

    Private Sub btnGradeStop_Click(sender As Object, e As EventArgs) Handles btnGradeStop.Click
        GradeStop += 1

        If GradeStop Mod 3 = 2 Then
            gradeChange("EYE")
        ElseIf GradeStop Mod 3 = 1 Then
            gradeChange("PRO")
        Else
            gradeReset()
        End If
    End Sub

    Private Sub gradeChange(typeName As String)
        SettingMeasure = typeName
        txbGradePD1.BackColor = Color.Green
        txbGradePD1.Text = SettingMeasure
        txbGradePD1.Refresh()
    End Sub

    Private Sub gradeReset()
        SettingMeasure = String.Empty
        txbGradePD1.BackColor = DefaultBackColor
        txbGradePD1.Text = "대기"
        txbGradePD1.Refresh()
    End Sub

    Private Sub ckbSerialUse_CheckedChanged(sender As Object, e As EventArgs) Handles ckbSerialUse.CheckedChanged
        If ckbSerialUse.Checked = True Then
            If cmdSerialPort.IsOpen = False Then
                OpenSerial()
            End If
        End If

        OkNgTestStepSet()
    End Sub

    Private Sub OkNgTestStepSet()
        If ckbSerialUse.Checked = False Then
            TeststepSet.First_Ok = TestStepList.MAX_First_OK
            TeststepSet.First_Ng = TestStepList.MAX_First_NG
            TeststepSet.Re_OK = TestStepList.MAX_RE_OK
            TeststepSet.RE_Ng = TestStepList.MAX_RE_NG

            gbDSMode.Enabled = False
            btnGradeStop.Enabled = False
        Else
            TeststepSet.First_Ok = TestStepList.DUAL_First_OK
            TeststepSet.First_Ng = TestStepList.DUAL_First_NG
            TeststepSet.Re_OK = TestStepList.DUAL_RE_OK
            TeststepSet.RE_Ng = TestStepList.DUAL_RE_NG

            gbDSMode.Enabled = True
            btnGradeStop.Enabled = True
        End If
    End Sub

    Private Sub resizeCmdWindow(width As Int32, height As Int32, left As Int32, top As Int32)
        txbCmdWindow.Width = width
        txbCmdWindow.Height = height
        txbCmdWindow.Left = left
        txbCmdWindow.Top = top
    End Sub

    Private Sub showCmdWindow()
        resizeCmdWindow(505, 200, 513, 200)
        txbCmdWindow.BringToFront()
        txbCmdWindow.Visible = True
    End Sub

End Class