VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "PC/SC Demo"
   ClientHeight    =   5610
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   8295
   LinkTopic       =   "Form1"
   ScaleHeight     =   5610
   ScaleWidth      =   8295
   StartUpPosition =   3  '´°¿ÚÈ±Ê¡
   Begin VB.Frame Frame2 
      Caption         =   " Mifareone "
      Height          =   3375
      Left            =   120
      TabIndex        =   1
      Top             =   1800
      Width           =   8055
      Begin VB.OptionButton Radio_KeyB 
         Caption         =   "B Key"
         Height          =   375
         Left            =   6120
         TabIndex        =   14
         Top             =   960
         Width           =   855
      End
      Begin VB.OptionButton Radio_KeyA 
         Caption         =   "A Key"
         Height          =   375
         Left            =   5160
         TabIndex        =   13
         Top             =   960
         Width           =   855
      End
      Begin VB.CommandButton BT_M1Halt 
         Caption         =   "HaltCard"
         Height          =   495
         Left            =   6000
         TabIndex        =   12
         Top             =   2640
         Width           =   1455
      End
      Begin VB.CommandButton BT_M1Write 
         Caption         =   "WriteCard"
         Height          =   495
         Left            =   4320
         TabIndex        =   11
         Top             =   2640
         Width           =   1455
      End
      Begin VB.CommandButton BT_M1Read 
         Caption         =   "ReadCard"
         Height          =   495
         Left            =   2640
         TabIndex        =   10
         Top             =   2640
         Width           =   1455
      End
      Begin VB.CommandButton BT_M1Search 
         Caption         =   "SearchCard"
         Height          =   495
         Left            =   960
         TabIndex        =   9
         Top             =   2640
         Width           =   1455
      End
      Begin VB.TextBox M1_Data 
         Height          =   375
         Left            =   960
         TabIndex        =   8
         Top             =   2040
         Width           =   6855
      End
      Begin VB.ComboBox M1_Block 
         Height          =   300
         Left            =   960
         TabIndex        =   7
         Top             =   1560
         Width           =   2295
      End
      Begin VB.TextBox M1_Key 
         Height          =   375
         Left            =   960
         TabIndex        =   6
         Text            =   "FFFFFFFFFFFF"
         Top             =   960
         Width           =   4095
      End
      Begin VB.TextBox M1_UID 
         Height          =   375
         Left            =   960
         TabIndex        =   5
         Top             =   360
         Width           =   4095
      End
      Begin VB.Label Label6 
         Alignment       =   1  'Right Justify
         Caption         =   "Data"
         Height          =   255
         Left            =   480
         TabIndex        =   20
         Top             =   2160
         Width           =   375
      End
      Begin VB.Label Label5 
         Alignment       =   1  'Right Justify
         Caption         =   "BlockNO"
         Height          =   255
         Left            =   240
         TabIndex        =   19
         Top             =   1560
         Width           =   615
      End
      Begin VB.Label Label4 
         Alignment       =   1  'Right Justify
         Caption         =   "Key"
         Height          =   255
         Left            =   480
         TabIndex        =   18
         Top             =   1080
         Width           =   375
      End
      Begin VB.Label Label3 
         Alignment       =   1  'Right Justify
         Caption         =   "UID"
         Height          =   255
         Left            =   480
         TabIndex        =   17
         Top             =   480
         Width           =   375
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   " Reader "
      Height          =   1575
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   8055
      Begin VB.CommandButton BT_Updata 
         Caption         =   "UpdataReader"
         Height          =   495
         Left            =   6240
         TabIndex        =   4
         Top             =   840
         Width           =   1575
      End
      Begin VB.CommandButton BT_Connect 
         Caption         =   "ConnectReader"
         Height          =   495
         Left            =   6240
         TabIndex        =   3
         Top             =   240
         Width           =   1575
      End
      Begin VB.ComboBox Reader 
         Height          =   300
         Left            =   240
         TabIndex        =   2
         Top             =   720
         Width           =   5775
      End
      Begin VB.Label Label2 
         Caption         =   "Please select PC/SC reader!"
         Height          =   255
         Left            =   240
         TabIndex        =   16
         Top             =   360
         Width           =   5295
      End
   End
   Begin VB.Label Status 
      Caption         =   "Please select PC/SC reader!"
      Height          =   255
      Left            =   240
      TabIndex        =   15
      Top             =   5280
      Width           =   7935
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'PC/SC MR-800 Reader demo
'Jinmuyu Electronic company
Public m_bM1CardActiveFlag As Boolean


'Connect reader
Private Sub BT_Connect_Click()

Dim bConnectFlag As Boolean
  
If (m_bActiveFlag) Then
    'Disconnect
    bConnectFlag = DisconnectReader()
    If (bConnectFlag) Then
        BT_Connect.Caption = "ConnectReader"
        Reader.Enabled = True
        Status.Caption = "Please select PC/SC reader!"
    End If
Else
    If (Reader.ListIndex > -1) Then
        'Connect
        bConnectFlag = ConnectReader(Reader.ListIndex)
        If (bConnectFlag) Then
            BT_Connect.Caption = "DisConnect"
            Reader.Enabled = False
            Status.Caption = "PC/SC reader connected!"
        End If
    End If
End If

End Sub

'Halt Mifareone card
Private Sub BT_M1Halt_Click()

Dim iSW As Long

    iSW = TypeA_Halt()
    If (SUCCESS = iSW) Then
        Status.Caption = "Halt card successful!"
    Else
        Status.Caption = "Halt card fail!"
    End If

End Sub

'Read mifareone card
Private Sub BT_M1Read_Click()

Dim cKeyType, cKeyConfig As Byte
Dim block, iReceiveLen As Integer
Dim cReceiveData(0 To 15) As Byte
Dim key, Data As String
Dim KeyData(0 To 10) As Byte
Dim i, j As Integer
Dim iSW As Long

If (Not m_bM1CardActiveFlag) Then
    Status.Caption = "Please clicked SearchCard button!"
    Exit Sub
End If
    
If Radio_KeyA.value = True Then
    cKeyType = &H60
Else
    cKeyType = &H61
End If

cKeyConfig = &H6
block = M1_Block.ListIndex

'Get Key
key = Trim(M1_Key.Text)
If Len(key) <> 12 Then
'    MessageBox(0,'Key length must be 6 byte','Error Key',MB_OK);
    Status.Caption = "Key length must be 6 byte!"
    M1_Key.SetFocus
    Exit Sub
End If

For i = 0 To 5
    KeyData(i) = Val("&H" & Mid(key, i * 2 + 1, 2))
Next i
M1_Data.Text = ""

'Authentication
iSW = Authentication(block, cKeyType, cKeyConfig, KeyData)
If (SUCCESS <> iSW) Then
    Status.Caption = "Authentication fail!"
    m_bM1CardActiveFlag = False
    Exit Sub
End If

'ReadCard
iReceiveLen = 16
iSW = ReadBinaryBlock(block, cReceiveData, iReceiveLen)
If (SUCCESS = iSW) Then
    Data = ""
    For i = 0 To iReceiveLen - 1
        Data = Data & Right("00" & Hex(cReceiveData(i)), 2)
    Next i
    M1_Data.Text = Data
    Status.Caption = "Read card successful!"
Else
    Status.Caption = "Read card fail!"
End If

End Sub

'Search mifareone card
Private Sub BT_M1Search_Click()

Dim iSW, i As Long
Dim CardInfo(0 To 20) As Byte
Dim ReceiveLen As Integer
Dim UID As String
    
M1_UID.Text = ""
M1_Data.Text = ""
UID = ""
    
iSW = GetData(GET_MIFAREONE, CardInfo, ReceiveLen)
If (SUCCESS = iSW) Then
    'Request card successful
    For i = 0 To CardInfo(0) - 1
        UID = UID & Right("00" & Hex(CardInfo(i + 1)), 2)
    Next i
    M1_UID.Text = UID
    Status.Caption = "Search card successful!"
    m_bM1CardActiveFlag = True
Else
    Status.Caption = "Search card fail!"
    m_bM1CardActiveFlag = False
End If

End Sub

'Write mifareone card
Private Sub BT_M1Write_Click()
Dim cKeyType, cKeyConfig As Byte
Dim block, iReceiveLen As Integer
Dim cWriteData(0 To 15) As Byte
Dim key, Data As String
Dim KeyData(0 To 10) As Byte
Dim i, j As Integer
Dim iSW As Long

If (Not m_bM1CardActiveFlag) Then
    Status.Caption = "Please clicked SearchCard button!"
    Exit Sub
End If
    
If Radio_KeyA.value = True Then
    cKeyType = &H60
Else
    cKeyType = &H61
End If

cKeyConfig = &H6
block = M1_Block.ListIndex

'Get Key
key = Trim(M1_Key.Text)
If Len(key) <> 12 Then
    Status.Caption = "Key length must be 6 byte!"
    M1_Key.SetFocus
    Exit Sub
End If

For i = 0 To 5
    KeyData(i) = Val("&H" & Mid(key, i * 2 + 1, 2))
Next i

'Authentication
iSW = Authentication(block, cKeyType, cKeyConfig, KeyData)
If (SUCCESS <> iSW) Then
    Status.Caption = "Authentication fail!"
    m_bM1CardActiveFlag = False
    Exit Sub
End If

'Get writed data
Data = Trim(M1_Data.Text)
If Len(Data) <> 32 Then
    Status.Caption = "Data length must be 16 byte!"
    M1_Data.SetFocus
    Exit Sub
End If

For i = 0 To 15
    cWriteData(i) = Val("&H" & Mid(Data, i * 2 + 1, 2))
Next i

'WriteCard
iSW = UpdataBinaryBlock(block, cWriteData, 16)
If (SUCCESS = iSW) Then
    Status.Caption = "Write card successful!"
Else
    Status.Caption = "Write card fail!"
End If

End Sub

'Updata reader counter
Private Sub BT_Updata_Click()
    
Dim iSW, i As Integer
Reader.Clear
Reader.Enabled = True
    
iSW = UpdataReader()
If (iSW <> 0) Then
    For i = 0 To m_bReaderCounter - 1
        Reader.AddItem (ReaderName(i))
    Next i
    Reader.ListIndex = 0
    BT_Connect.Caption = "ConnectReader"
    Status.Caption = "Please select PC/SC reader!"
End If

End Sub

'Initialize
Private Sub Form_Load()
Dim i&
Dim iSW As Long
    For i = 0 To 63
        M1_Block.AddItem CStr(i), i
    Next i
    M1_Block.ListIndex = 1:
    Radio_KeyA.value = True
    m_bM1CardActiveFlag = False
    Call BT_Updata_Click

End Sub
