Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents btnOpen As System.Windows.Forms.Button
    Friend WithEvents btnClose As System.Windows.Forms.Button
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents btnExit As System.Windows.Forms.Button
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

#Region " Windows Form 디자이너에서 생성한 코드 "
    Private m_bMode As Boolean

    Public Sub New()
        MyBase.New()

        '이 호출은 Windows Form 디자이너에 필요합니다.
        InitializeComponent()

        'InitializeComponent()를 호출한 다음에 초기화 작업을 추가하십시오.
    End Sub

    'Form은 Dispose를 재정의하여 구성 요소 목록을 정리합니다.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    '참고: 다음 프로시저는 Windows Form 디자이너에 필요합니다.
    'Windows Form 디자이너를 사용하여 수정할 수 있습니다.  
    '코드 편집기를 사용하여 수정하지 마십시오.
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents btnMSRRead As System.Windows.Forms.Button
    Friend WithEvents btnMSRCancel As System.Windows.Forms.Button
    Friend WithEvents cboPort As System.Windows.Forms.ComboBox
    Friend WithEvents txtMSRDATA1 As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents txtMSRDATA2 As System.Windows.Forms.TextBox
    Friend WithEvents txtMSRDATA3 As System.Windows.Forms.TextBox
    Friend WithEvents m_bTrackCheck1 As System.Windows.Forms.CheckBox
    Friend WithEvents m_bTrackCheck3 As System.Windows.Forms.CheckBox
    Friend WithEvents m_bTrackCheck2 As System.Windows.Forms.CheckBox
    Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.Label1 = New System.Windows.Forms.Label
        Me.btnOpen = New System.Windows.Forms.Button
        Me.btnClose = New System.Windows.Forms.Button
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.CheckBox2 = New System.Windows.Forms.CheckBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.txtMSRDATA1 = New System.Windows.Forms.TextBox
        Me.btnMSRRead = New System.Windows.Forms.Button
        Me.btnMSRCancel = New System.Windows.Forms.Button
        Me.btnExit = New System.Windows.Forms.Button
        Me.Timer1 = New System.Windows.Forms.Timer
        Me.cboPort = New System.Windows.Forms.ComboBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.txtMSRDATA2 = New System.Windows.Forms.TextBox
        Me.txtMSRDATA3 = New System.Windows.Forms.TextBox
        Me.m_bTrackCheck1 = New System.Windows.Forms.CheckBox
        Me.m_bTrackCheck3 = New System.Windows.Forms.CheckBox
        Me.m_bTrackCheck2 = New System.Windows.Forms.CheckBox
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 12)
        Me.Label1.Size = New System.Drawing.Size(40, 16)
        Me.Label1.Text = "Port : "
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'btnOpen
        '
        Me.btnOpen.Location = New System.Drawing.Point(144, 8)
        Me.btnOpen.Size = New System.Drawing.Size(88, 24)
        Me.btnOpen.Text = "Open"
        '
        'btnClose
        '
        Me.btnClose.Location = New System.Drawing.Point(144, 36)
        Me.btnClose.Size = New System.Drawing.Size(88, 24)
        Me.btnClose.Text = "Close"
        '
        'CheckBox1
        '
        Me.CheckBox1.Location = New System.Drawing.Point(16, 32)
        Me.CheckBox1.Size = New System.Drawing.Size(104, 18)
        Me.CheckBox1.Text = "Printer OFF"
        '
        'CheckBox2
        '
        Me.CheckBox2.Location = New System.Drawing.Point(16, 48)
        Me.CheckBox2.Size = New System.Drawing.Size(104, 18)
        Me.CheckBox2.Text = "MSR Read"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(24, 136)
        Me.Label2.Size = New System.Drawing.Size(80, 16)
        Me.Label2.Text = "MSR Data1"
        '
        'txtMSRDATA1
        '
        Me.txtMSRDATA1.Location = New System.Drawing.Point(24, 152)
        Me.txtMSRDATA1.Size = New System.Drawing.Size(208, 21)
        Me.txtMSRDATA1.Text = ""
        '
        'btnMSRRead
        '
        Me.btnMSRRead.Location = New System.Drawing.Point(144, 80)
        Me.btnMSRRead.Size = New System.Drawing.Size(88, 24)
        Me.btnMSRRead.Text = "MSR Read"
        '
        'btnMSRCancel
        '
        Me.btnMSRCancel.Location = New System.Drawing.Point(144, 112)
        Me.btnMSRCancel.Size = New System.Drawing.Size(88, 24)
        Me.btnMSRCancel.Text = "MSR Cancel"
        '
        'btnExit
        '
        Me.btnExit.Location = New System.Drawing.Point(24, 256)
        Me.btnExit.Size = New System.Drawing.Size(208, 24)
        Me.btnExit.Text = "Exit"
        '
        'Timer1
        '
        '
        'cboPort
        '
        Me.cboPort.Location = New System.Drawing.Point(64, 8)
        Me.cboPort.Size = New System.Drawing.Size(64, 20)
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(24, 176)
        Me.Label3.Size = New System.Drawing.Size(80, 16)
        Me.Label3.Text = "MSR Data2"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(24, 216)
        Me.Label4.Size = New System.Drawing.Size(80, 16)
        Me.Label4.Text = "MSR Data3"
        '
        'txtMSRDATA2
        '
        Me.txtMSRDATA2.Location = New System.Drawing.Point(24, 190)
        Me.txtMSRDATA2.Size = New System.Drawing.Size(208, 21)
        Me.txtMSRDATA2.Text = ""
        '
        'txtMSRDATA3
        '
        Me.txtMSRDATA3.Location = New System.Drawing.Point(24, 230)
        Me.txtMSRDATA3.Size = New System.Drawing.Size(208, 21)
        Me.txtMSRDATA3.Text = ""
        '
        'm_bTrackCheck1
        '
        Me.m_bTrackCheck1.Location = New System.Drawing.Point(16, 72)
        Me.m_bTrackCheck1.Size = New System.Drawing.Size(60, 18)
        Me.m_bTrackCheck1.Text = "Track1"
        '
        'm_bTrackCheck3
        '
        Me.m_bTrackCheck3.Location = New System.Drawing.Point(16, 112)
        Me.m_bTrackCheck3.Size = New System.Drawing.Size(60, 18)
        Me.m_bTrackCheck3.Text = "Track3"
        '
        'm_bTrackCheck2
        '
        Me.m_bTrackCheck2.Location = New System.Drawing.Point(16, 92)
        Me.m_bTrackCheck2.Size = New System.Drawing.Size(60, 18)
        Me.m_bTrackCheck2.Text = "Track2"
        '
        'Form1
        '
        Me.ClientSize = New System.Drawing.Size(240, 296)
        Me.Controls.Add(Me.m_bTrackCheck2)
        Me.Controls.Add(Me.m_bTrackCheck3)
        Me.Controls.Add(Me.m_bTrackCheck1)
        Me.Controls.Add(Me.txtMSRDATA3)
        Me.Controls.Add(Me.txtMSRDATA2)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.cboPort)
        Me.Controls.Add(Me.btnExit)
        Me.Controls.Add(Me.btnMSRCancel)
        Me.Controls.Add(Me.btnMSRRead)
        Me.Controls.Add(Me.txtMSRDATA1)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.CheckBox2)
        Me.Controls.Add(Me.CheckBox1)
        Me.Controls.Add(Me.btnClose)
        Me.Controls.Add(Me.btnOpen)
        Me.Controls.Add(Me.Label1)
        Me.Menu = Me.MainMenu1
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim lResult As Long
        lResult = OpenPort(cboPort.Text, 38400)
        If (lResult <> 0) Then
            MessageBox.Show("Open Port Failed")
            Return
        End If

        btnOpen.Enabled = False
        btnClose.Enabled = True
        btnMSRRead.Enabled = True
        btnMSRCancel.Enabled = True
        btnExit.Enabled = False
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim lResult As Long
        lResult = ClosePort()
        If (lResult <> 0) Then
            MessageBox.Show("Close Port Failed!!!")
            Return
        End If

        btnOpen.Enabled = True
        btnClose.Enabled = False
        btnMSRRead.Enabled = False
        btnMSRCancel.Enabled = False
        btnExit.Enabled = True
    End Sub

    Private Sub btnMSRRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSRRead.Click

        Dim lResult As Long
        Dim lState As Int32
        Dim lTrack As Int32

        lTrack = 0

        '   MSRSelectTrack(2) ' Only Track2.
        '	MSRSelectTrack(3) ' Track1/2.

        If (m_bTrackCheck1.Checked) Then
            lTrack = lTrack Or 1
        End If
        If (m_bTrackCheck2.Checked) Then
            lTrack = lTrack Or 2
        End If
        If (m_bTrackCheck3.Checked) Then
            lTrack = lTrack Or 4
        End If

        MSRSelectTrack(lTrack)

        lResult = PrinterCheck()

        Select Case lResult
            Case -1 ' Bluetooth Write Fail. Please Check Connection.
                MessageBox.Show("Write Error")
            Case -2
                MessageBox.Show("Fail to Read Printer Status")
        End Select

        lState = status()


        If ((lState And LK_STS_PRINTEROFF) = LK_STS_PRINTEROFF) Then
            CheckBox1.Checked = True
        ElseIf ((lState And LK_STS_MSR_READ) = LK_STS_MSR_READ) Then
            CheckBox2.Checked = True
        ElseIf ((lState And LK_STS_PAPER_EMPTY) = LK_STS_PAPER_EMPTY) Then
            CheckBox1.Checked = True
        ElseIf ((lState And LK_STS_COVER_OPEN) = LK_STS_COVER_OPEN) Then
            CheckBox1.Checked = True
        ElseIf ((lState And LK_STS_BATTERY_LOW) = LK_STS_BATTERY_LOW) Then
            CheckBox1.Checked = True
        End If


        If ((lState And LK_STS_MSR_READ) <> LK_STS_MSR_READ) Then
            txtMSRDATA1.Text = "MSR Not Read Mode"
            Return
        End If



        ' Stop CallBack
        Timer1.Enabled = False
        m_bMode = False
        btnMSRRead.Enabled = False
        btnMSRCancel.Enabled = True

        Timer1.Interval = 1000
        Timer1.Enabled = True


    End Sub

    Private Sub btnMSRCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSRCancel.Click
        m_bMode = True
        CheckBox2.Checked = False

        ' Stop CallBack
        Timer1.Enabled = False

        txtMSRDATA1.Text = "- Cancel to MSR Read"
        btnMSRRead.Enabled = True
        MSRReadCancel()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Dispose(True)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim lResult As Long
        Dim lState As Int32
        Dim status As Long

        Timer1.Enabled = False
        '        Dim MSRData(256) As Byte
        Dim Data As String
        Dim DataTrack1(256) As Byte
        Dim DataTrack2(256) As Byte
        Dim DataTrack3(256) As Byte

        '        status = MSRReadData(MSRData)
        status = MSRReadTrack(DataTrack1, DataTrack2, DataTrack3)

        Select Case status
            Case 0
                If (m_bMode = False) Then
                    Dim enc As New System.Text.ASCIIEncoding
                    Data = enc.GetString(DataTrack1, 0, 256)
                    txtMSRDATA1.Text = Data
                    Data = enc.GetString(DataTrack2, 0, 256)
                    txtMSRDATA2.Text = Data
                    Data = enc.GetString(DataTrack3, 0, 256)
                    txtMSRDATA3.Text = Data
                    m_bMode = True
                    CheckBox2.Checked = False
                    Timer1.Enabled = False
                    btnMSRRead.Enabled = True
                    btnMSRCancel.Enabled = False
                    MSRReadCancel()
                Else
                    txtMSRDATA1.Text = "Cancel to MSR Read"
                    Return
                End If

            Case 1
                If (m_bMode = False) Then
                    txtMSRDATA1.Text = "Check-Sum Error..."
                    Timer1.Interval = 1000
                    Timer1.Enabled = True
                Else
                    txtMSRDATA1.Text = "Cancel to MSR Read"
                    Return
                End If

            Case 2
                If (m_bMode = False) Then
                    txtMSRDATA1.Text = "Timeout Error..."
                    txtMSRDATA2.Text = "Timeout Error..."
                    txtMSRDATA3.Text = "Timeout Error..."
                    Timer1.Interval = 1000
                    Timer1.Enabled = True
                Else
                    txtMSRDATA1.Text = "Cancel to MSR Read"
                    Return
                End If
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboPort.Items.Add("COM1:")
        cboPort.Items.Add("COM2:")
        cboPort.Items.Add("COM3:")
        cboPort.Items.Add("COM4:")
        cboPort.Items.Add("COM5:")
        cboPort.Items.Add("COM6:")
        cboPort.Items.Add("COM7:")
        cboPort.Items.Add("COM8:")
        cboPort.Items.Add("COM9:")
        cboPort.SelectedIndex = 7

        btnClose.Enabled = False
        btnMSRRead.Enabled = False
        btnMSRCancel.Enabled = False

        m_bTrackCheck1.Checked = True
        m_bTrackCheck2.Checked = True
        m_bTrackCheck3.Checked = False
    End Sub

    Private Sub m_bTrackCheck1_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_bTrackCheck1.CheckStateChanged
        Dim bChecked As Boolean

        bChecked = m_bTrackCheck1.Checked
        If (bChecked) Then
            If (m_bTrackCheck3.Checked) Then
                MessageBox.Show("Track1 and Track3 Setting's Error", "Error")
                m_bTrackCheck1.Checked = False
            Else
                m_bTrackCheck1.Checked = True
            End If
        Else
            m_bTrackCheck1.Checked = False
        End If
    End Sub

    Private Sub m_bTrackCheck2_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_bTrackCheck2.CheckStateChanged
        Dim bChecked As Boolean

        bChecked = m_bTrackCheck2.Checked
        If (bChecked) Then
            m_bTrackCheck2.Checked = True
        Else
            m_bTrackCheck2.Checked = False
        End If
    End Sub

    Private Sub m_bTrackCheck3_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_bTrackCheck3.CheckStateChanged
        Dim bChecked As Boolean

        bChecked = m_bTrackCheck3.Checked
        If (bChecked) Then
            If (m_bTrackCheck1.Checked) Then
                MessageBox.Show("Track1 and Track3 Setting's Error", "Error")
                m_bTrackCheck3.Checked = False
            Else
                m_bTrackCheck3.Checked = True
            End If
        Else
            m_bTrackCheck3.Checked = False
        End If

    End Sub

End Class
