VERSION 5.00
Begin VB.Form Form2 
   BackColor       =   &H80000004&
   Caption         =   "EM Format Writer"
   ClientHeight    =   1545
   ClientLeft      =   2925
   ClientTop       =   6180
   ClientWidth     =   6015
   Icon            =   "Form2.frx":0000
   LinkTopic       =   "Form2"
   ScaleHeight     =   1545
   ScaleWidth      =   6015
   Begin VB.Timer Timer1 
      Interval        =   2000
      Left            =   4560
      Tag             =   "5"
      Top             =   240
   End
   Begin VB.CommandButton Command3 
      Caption         =   "Read"
      Height          =   375
      Left            =   1920
      TabIndex        =   4
      Top             =   960
      Width           =   1215
   End
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   2160
      TabIndex        =   3
      Text            =   "Text1"
      Top             =   360
      Width           =   1935
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Cancel"
      Height          =   375
      Left            =   4440
      TabIndex        =   2
      Top             =   960
      Width           =   1215
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Write"
      Height          =   375
      Left            =   480
      TabIndex        =   1
      Top             =   960
      Width           =   1215
   End
   Begin VB.Label Label2 
      BackColor       =   &H80000004&
      Caption         =   "Data ："
      Height          =   375
      Left            =   960
      TabIndex        =   0
      Top             =   360
      Width           =   975
   End
End
Attribute VB_Name = "Form2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub Command1_Click()
Command1.Enabled = False
'If linkcomok < 0 Then
'   aa = MsgBox("串口连接不正确，请核对！", vbOKOnly, "警告！")
'   Command1.Enabled = True
'   Exit Sub
'End If
Dim emstring1 As String
Dim emstring2 As String
Dim i, j, k, s1$, writedata(10) As Byte
Dim emstring(63) As String
Dim emstring11(15) As String

' DelayMs (10)
'Timer1.Tag = 5
If Len(Text1.Text) <> 10 Then
  aa = MsgBox("Error Data Length！", vbOKOnly, "Warning ！")
  Command1.Enabled = True
  Exit Sub
End If
emstring2 = ""
k = 0
For i = 0 To 9
  emstring1 = getbin(Mid(Text1.Text, i + 1, 1))
     For j = 1 To 4
         emstring(8 + k + j) = Mid(emstring1, j, 1)
     Next
     k = k + 5
Next
emstring(13) = emstring(9) Xor emstring(10) Xor emstring(11) Xor emstring(12)
emstring(18) = emstring(9 + 5) Xor emstring(10 + 5) Xor emstring(11 + 5) Xor emstring(12 + 5)
emstring(23) = emstring(9 + 10) Xor emstring(10 + 10) Xor emstring(11 + 10) Xor emstring(12 + 10)
emstring(28) = emstring(9 + 15) Xor emstring(10 + 15) Xor emstring(11 + 15) Xor emstring(12 + 15)
emstring(33) = emstring(9 + 20) Xor emstring(10 + 20) Xor emstring(11 + 20) Xor emstring(12 + 20)
emstring(38) = emstring(9 + 25) Xor emstring(10 + 25) Xor emstring(11 + 25) Xor emstring(12 + 25)
emstring(43) = emstring(9 + 30) Xor emstring(10 + 30) Xor emstring(11 + 30) Xor emstring(12 + 30)
emstring(48) = emstring(9 + 35) Xor emstring(10 + 35) Xor emstring(11 + 35) Xor emstring(12 + 35)
emstring(53) = emstring(9 + 40) Xor emstring(10 + 40) Xor emstring(11 + 40) Xor emstring(12 + 40)
emstring(58) = emstring(9 + 45) Xor emstring(10 + 45) Xor emstring(11 + 45) Xor emstring(12 + 45)
emstring(59) = emstring(9) Xor emstring(14) Xor emstring(19) Xor emstring(24) Xor emstring(29) Xor emstring(34) Xor emstring(39) Xor emstring(44) Xor emstring(49) Xor emstring(54)
emstring(60) = emstring(9 + 1) Xor emstring(14 + 1) Xor emstring(19 + 1) Xor emstring(24 + 1) Xor emstring(29 + 1) Xor emstring(34 + 1) Xor emstring(39 + 1) Xor emstring(44 + 1) Xor emstring(49 + 1) Xor emstring(54 + 1)
emstring(61) = emstring(9 + 2) Xor emstring(14 + 2) Xor emstring(19 + 2) Xor emstring(24 + 2) Xor emstring(29 + 2) Xor emstring(34 + 2) Xor emstring(39 + 2) Xor emstring(44 + 2) Xor emstring(49 + 2) Xor emstring(54 + 2)
emstring(62) = emstring(9 + 3) Xor emstring(14 + 3) Xor emstring(19 + 3) Xor emstring(24 + 3) Xor emstring(29 + 3) Xor emstring(34 + 3) Xor emstring(39 + 3) Xor emstring(44 + 3) Xor emstring(49 + 3) Xor emstring(54 + 3)
emstring(63) = "0"

For i = 0 To 8
  emstring(i) = "1"
Next
For i = 0 To 63
   emstring2 = emstring2 + emstring(i)
Next
For i = 0 To 15
  emstring11(i) = ""
Next
k = 0
For i = 0 To 15
   For j = 0 To 3
      emstring11(i) = emstring11(i) + Mid(emstring2, j + k + 1, 1)
      Next
      k = k + 4
Next


emstring2 = ""
For i = 0 To 15
  emstring11(i) = gethex(emstring11(i))
emstring2 = emstring2 + emstring11(i)
Next

'--
s1 = Mid(emstring2, 1, 8)

For i = 0 To 3
    writedata(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
Next i

  aa = Standard_Write(2, 0, writedata(0), 1)
 ' DelayMs (1000)
s1 = Mid(emstring2, 9, 8)

For i = 0 To 3
    writedata(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
Next i
  aa = Standard_Write(2, 0, writedata(0), 2)
 ' DelayMs (3000)

's1 = "00148040"
'For i = 0 To 3
'    writedata(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
'Next i
'  aa = Standard_Write(2, 0, writedata(0), 0)
  'DelayMs (1000)
 
 'aa = wr_card(18, 0, 1, 4, Mid(emstring2, 1, 8))
 'aa = wr_card(18, 0, 2, 4, Mid(emstring2, 9, 8))

'aa = wr_card(18, 0, 0, 4, "00148040")
i = Reset_Command()
Command1.Enabled = True
 status = rf_beep(0, 6)
End Sub


Function getbin(hexstring As String) As String
Select Case hexstring
 Case "F"
 getbin = "1111"
 Case "E"
 getbin = "1110"
 Case "D"
 getbin = "1101"
 Case "C"
 getbin = "1100"
 Case "B"
 getbin = "1011"
 Case "A"
 getbin = "1010"
 Case "f"
 getbin = "1111"
 Case "e"
 getbin = "1110"
 Case "d"
 getbin = "1101"
 Case "c"
 getbin = "1100"
 Case "b"
 getbin = "1011"
 Case "a"
 getbin = "1010"
 Case "9"
 getbin = "1001"
 Case "8"
 getbin = "1000"
 Case "7"
 getbin = "0111"
 Case "6"
 getbin = "0110"
 Case "5"
 getbin = "0101"
 Case "4"
 getbin = "0100"
 Case "3"
 getbin = "0011"
 Case "2"
 getbin = "0010"
 Case "1"
 getbin = "0001"
 Case "0"
 getbin = "0000"
 End Select
 End Function
Function gethex(binstring As String) As String
Select Case binstring
 Case "1111"
 gethex = "F"
 Case "1110"
 gethex = "E"
 Case "1101"
 gethex = "D"
 Case "1100"
 gethex = "C"
 Case "1011"
 gethex = "B"
 Case "1010"
 gethex = "A"
 Case "1001"
 gethex = "9"
 Case "1000"
 gethex = "8"
 Case "0111"
 gethex = "7"
 Case "0110"
 gethex = "6"
 Case "0101"
 gethex = "5"
 Case "0100"
 gethex = "4"
 Case "0011"
 gethex = "3"
 Case "0010"
 gethex = "2"
 Case "0001"
 gethex = "1"
 Case "0000"
 gethex = "0"
 End Select
 
 End Function
 Function DelayMs(delay As Integer)
'    Form2.Timer1.Enabled = True
    DelayTime = 0
    Timer1.Enabled = True
    'DelayFlag = True
    While (DelayTime = 0)
       'DelayTime = DelayTime + 1
    Wend
    Timer1.Enabled = False
    'DelayFlag = False
    
    DelayTime = 0
End Function



Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Command3_Click()
 Dim i As Long, buff(50) As Byte, s1$, DataLength As Byte, j&, port&, baud&
'i = Reset_Command()
 Text1.Text = ""
 i = Read_Em4001(buff(0))
 
If (i <> 0) Then
     aa = MsgBox("Read Card Fail ！", vbOKOnly, "Alert！")
     Form1.StatusBar1.Panels(2).Text = "Read Card Fail"
     Exit Sub
End If
s1 = ""
   For i = 0 To 4
   s1 = s1 & Right("00" & Hex(buff(i)), 2)
   Next i

Text1.Text = s1
 status = rf_beep(0, 6)
End Sub

Private Sub Form_Load()
   Form2.Left = (Screen.Width - Width) / 2    ' 在水平方向上居中显示。
   Form2.Top = (Screen.Height - Height) / 2
Timer1.Enabled = False
Select Case cardtype

Case "E5550"

Case "RF256-12"
    Form2.Caption = "更改密碼："
    Combo1.Visible = False
    Label1.Visible = False
    Label2.Caption = "密   碼："
Case "mafare S50"

  
End Select
  Form2.Text1.Text = ""
  Form2.Text1.MaxLength = 10
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim i As Integer
  Select Case KeyAscii
    Case 48 To 57
    Case 8
    Case 13
    Case 105
    Case 97 To 102
    Case 65 To 70
    Case Else:
    aa = MsgBox("输入值应为“0-F”", vbOKOnly, "警告！")
    KeyAscii = 0
  End Select
End Sub

Private Sub Timer1_Timer()
  'If DelayFlag = True Then
    DelayTime = DelayTime + 1
' End If
End Sub
