mifare_demo with pcr310, gigatms, ltd. 進國科技大學資管系 饒瑞佶 2010/5

41
MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進進進進進進進進進 進進進 2010/5

Upload: caren-poole

Post on 03-Jan-2016

224 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

MIFARE_DEMOWITH

PCR310, GIGATMS, Ltd.

進國科技大學資管系饒瑞佶2010/5

Page 2: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 3: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 4: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbImports VB = Microsoft.VisualBasic

Structure MFRecord Dim Id As Integer Dim dt As Date Dim Action As Short Dim Reserve As Integer Dim Value1 As Integer Dim Value2 As IntegerEnd Structure

Page 5: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vb Public Function LenX(ByRef szData As String) As Integer Dim I, lLen As Integer lLen = 0 For I = 1 To Len(szData) Select Case Asc(Mid(szData, I, 1)) Case 0 To 255 'ASCII lLen = lLen + 1 Case Else 'DBCS lLen = lLen + 2 End Select Next I LenX = lLenEnd Function

Page 6: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbPublic Function HexToBytes(ByVal szHex As String) As Byte() Dim bData() As Byte Dim L As Integer Dim I As Integer L = Len(szHex) ReDim bData((Int(L / 2 + 0.5) - 1)) For I = 1 To L Step 2 bData((I - 1) / 2) = Val("&H" & Mid(szHex, I, 2)) Next I HexToBytes =

Microsoft.VisualBasic.Compatibility.VB6.CopyArray(bData)End Function

Page 7: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbPublic Function HexToString(ByVal szHex As String) As String Dim bData() As Byte Dim L As Integer Dim I As Integer Dim oBuffer As New System.Text.StringBuilder L = Len(szHex) ReDim bData((Int(L / 2 + 0.5) - 1)) For I = 1 To L Step 2 Try oBuffer.Append(Chr(CInt("&H" & Mid(szHex, I, 2)))) Catch ex As Exception

End Try Next I HexToString = oBuffer.ToString() End Function

Page 8: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbPublic Function BytesToHex(ByRef bData() As Byte) As String Dim I As Integer Dim oBuffer As New System.Text.StringBuilder For I = LBound(bData) To UBound(bData) Try oBuffer.Append(Right(Hex(&H100S + bData(I)), 2)) Catch ex As Exception

End Try Next I BytesToHex = oBuffer.ToString() End Function

Page 9: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbPublic Function StringToHex(ByVal szData As String) As String Dim I As Integer, iLen As Integer Dim oBuffer As New System.Text.StringBuilder iLen = Len(szData) For I = 1 To iLen Try oBuffer.Append(VB.Right(Hex$(Asc(Mid(szData, I, 1))), 4)) Catch ex As Exception

End Try Next I Return oBuffer.ToString() End Function

Page 10: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbPublic Sub PushToByteArray(ByVal iValue As Integer, ByRef bBuffer() As Byte, ByRef iStart As Integer, Optional ByVal

bIsMSBFirst As Boolean = False) Dim I As Integer Dim iBegin As Integer, iEnd As Integer, iStep As Integer If bIsMSBFirst Then iBegin = iStart + 3 iEnd = iStart iStep = -1 Else iBegin = iStart iEnd = iStart + 3 iStep = 1 End If Try For I = iBegin To iEnd Step iStep bBuffer(I) = CByte(iValue And &HFF%) iValue = (iValue >> 8) Next I iStart += 4 Catch ex As Exception

End Try End Sub

Page 11: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbPublic Sub PushToByteArray(ByVal nValue As Short, ByRef bBuffer() As Byte, ByRef iStart As Integer, Optional ByVal

bIsMSBFirst As Boolean = False) Dim I As Integer Dim iBegin As Integer, iEnd As Integer, iStep As Integer If bIsMSBFirst Then iBegin = iStart + 1 iEnd = iStart iStep = -1 Else iBegin = iStart iEnd = iStart + 1 iStep = 1 End If Try For I = iBegin To iEnd Step iStep bBuffer(I) = CByte(nValue And CShort(&HFF)) nValue = (nValue >> 8) Next I iStart += 2 Catch ex As Exception

End Try End Sub

Page 12: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vbPublic Sub LoadFormPosision(ByRef oForm As System.Windows.Forms.Form) Dim iTop As Integer Dim iLeft As Integer Dim szName As String szName = oForm.Name With My.Application.Info iTop = CInt(GetSetting(.Title, szName, "Top", CStr(oForm.Top))) iLeft = CInt(GetSetting(.Title, szName, "Left", CStr(oForm.Left))) End With Select Case iTop Case 0 To System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height Case Else iTop = oForm.Top End Select Select Case iLeft Case 0 To System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height Case Else iLeft = oForm.Left End Select oForm.Top = iTop oForm.Left = iLeft End Sub

Page 13: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Pcr310_fun.vb

Public Sub SaveFormPosision(ByRef oForm As System.Windows.Forms.Form)

Dim szName As String szName = oForm.Name With My.Application.Info SaveSetting(.Title, szName, "Top", CStr(oForm.Top)) SaveSetting(.Title, szName, "Left", CStr(oForm.Left)) End With End Sub

Page 14: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

參考 MS VB.COMPATIBILITY

Page 15: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 16: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 17: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

需要三個 dll 檔案

Page 18: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

透過參考加入 dll

Page 19: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 20: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

讀卡號

Page 21: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Imports VB = Microsoft.VisualBasicImports GIGATMS.NF.MifareReaderImports GIGATMS.NF.GNetPlus‘ 宣告全域變數‘建立物件 mf5x1

Public WithEvents MF5x1 As New GIGATMS.NF.MifareReader

‘卡號Public Id As Integer

Page 22: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

透過 mf5x1 物件讀取卡號Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

Button1.Click MF5x1.mfRequest() ' 對機器發出命令要求讀取

' 讀取卡號 Id = MF5x1.mfAnticollision If Id <> 0 Then Select Case MF5x1.mfCurrentClass Case iCardTypeConstants.MIFARE_UltraLight, iCardTypeConstants.MIFARE_DESFire TextBox1.Text = VB.Left(Id.ToString("X").PadLeft(8, "0"), 6) Case Else TextBox1.Text = Id.ToString("X").PadLeft(8, "0") End Select Else TextBox1.Text = MF5x1.GNetErrorCodeStr End IfEnd Sub

Page 23: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

執行結果

出了甚麼問題?

Page 24: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Port 需要啟動Private Sub Form1_Load(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

MF5x1.CommPort = 12 MF5x1.Settings = "19200,n,8,1" MF5x1.PortOpen = TrueEnd Sub

Page 25: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

判斷卡片種類Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim nResult As Short

MF5x1.mfRequest() ' 對機器發出命令要求讀取

' 讀取卡號 Id = MF5x1.mfAnticollision If Id <> 0 Then Select Case MF5x1.mfCurrentClass Case iCardTypeConstants.MIFARE_UltraLight, iCardTypeConstants.MIFARE_DESFire TextBox1.Text = VB.Left(Id.ToString("X").PadLeft(8, "0"), 6) Case Else TextBox1.Text = Id.ToString("X").PadLeft(8, "0") End Select Else TextBox1.Text = MF5x1.GNetErrorCodeStr End If ' 判斷卡片的記憶體大小 nResult = MF5x1.mfSelectCard(Id) If nResult > 0 Then Select Case MF5x1.mfCurrentClass Case iCardTypeConstants.MIFARE_UltraLight, iCardTypeConstants.MIFARE_DESFire Label2.Text = "MIFARE_UltraLight" Case iCardTypeConstants.MIFARE_1K Label2.Text = "MIFARE_1K" Case iCardTypeConstants.MIFARE_4K Label2.Text = "MIFARE_4K" End Select End IfEnd Sub

Page 26: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

讀取資料• BUTTON2.ENABLED=0• 等 BUTTON1 確定讀到卡號才啟動BUTTON2• SECTOR:0-15• BLOCK:0-3

Page 27: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

讀取資料Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

Button2.Click Dim szKey As String Dim bResult As Boolean ' 認證結果 Dim bBuffer() As Byte Dim sResult As String szKey = vbNullString ' 表示使用機器上與卡片上的 KEY 進行認證

If RadioButton1.Checked Then bResult = MF5x1.mfAuthenticate(ComboBox1.SelectedIndex, bKeyTypeConstants.KEY_A, vbNullString) Else bResult = MF5x1.mfAuthenticate(ComboBox1.SelectedIndex, bKeyTypeConstants.KEY_B, vbNullString) End If

Page 28: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

讀取資料 If bResult Then Label9.Text = "Pass" Button3.Enabled = True ' 讀取資料 ReDim bBuffer(0 To 15) If MF5x1.mfRead(ComboBox2.Text, bBuffer) Then sResult = pcr310_fun.BytesToHex(bBuffer) If MF5x1.mfCurrentClass = iCardTypeConstants.MIFARE_UltraLight Then Label8.Text = Mid(sResult, 0, 16) Else Label8.Text = sResult End If End If TextBox3.Text = tostring1(Label8.Text) Else Label9.Text = MF5x1.GNetErrorCodeStr TextBox3.Text=“” Button3.Enabled = False Exit Sub End IfEnd Sub

Page 29: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Private m_bNoChangeEvent As BooleanFunction tostring1(ByVal a As String) If m_bNoChangeEvent = False Then m_bNoChangeEvent = True Dim arr(a.Length) As Byte arr = pcr310_fun.HexToBytes(a) Dim i As Integer = 0 Dim temp As String = "" Dim x As Byte x = 161 ' 判斷是不是繁體字 0xA1 For i = 0 To arr.Length - 1 Step i + 1 If arr(i) > x Then temp = temp + System.Text.Encoding.GetEncoding("Big5").GetString(arr, i, 2) i = i + 1 Else i = arr.Length ' 直接結束 End If Next m_bNoChangeEvent = False If arr(0) > x And arr.Length <> 0 Then Return (temp) Else Return (pcr310_fun.HexToString(a)) End If End IfEnd Function

Page 30: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 31: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

寫入資料• BUTTON3

Page 32: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 33: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim BlkNum As Short Dim bResult As Boolean ' 認證結果 Dim st1 As Integer Dim bBuffer() As Byte Dim key As String Dim a1 As String

If RadioButton1.Checked = True Then key = bKeyTypeConstants.KEY_A ElseIf RadioButton2.Checked = True Then key = bKeyTypeConstants.KEY_B End If

' 讀取使用者選擇的 SECTOR st1 = ComboBox1.Text

Page 34: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

'ABkey 認證 bResult = MF5x1.mfAuthenticate(st1, key, vbNullString) If bResult Then Else MessageBox.Show(MF5x1.GNetErrorCodeStr) Exit Sub End If ' 讀取使用者選擇的 BLOCK BlkNum = ComboBox2.Text ' 將資料轉 16 進制 a1 = VB.Left(pcr310_fun.StringToHex(TextBox3.Text) & New String("0", 32),

32) bBuffer = pcr310_fun.HexToBytes(a1) ' 寫入資料 If MF5x1.mfWrite(BlkNum, bBuffer) Then MessageBox.Show(" 寫入成功 ") Else MessageBox.Show(" 寫入失敗 ") End If End Sub

Page 35: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5
Page 36: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

存入選擇的 Sector( 機器上 )Imports GIGATMS.NFPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim res As Boolean Dim nSector As Short

' fill to 12 char (6 HEXs) TextBox1.Text = TextBox1.Text & New String("0", 12 - Len(TextBox1.Text))

nSector = ComboBox1.SelectedIndex

'Save Key To EEPROM for one sector With Form1 If RadioButton1.Checked Then ' Key Type A res = .MF5x1.mfSaveKey(nSector, MifareReader.bKeyTypeConstants.KEY_A, TextBox1.Text) Else ' Key Type B res = .MF5x1.mfSaveKey(nSector, MifareReader.bKeyTypeConstants.KEY_B, TextBox1.Text) End If

If res Then ' show status Label1.Text = "Save Key To EEPROM:OK(" & CStr(nSector) & ")" Else Label1.Text = "Save Key To EEPROM:NG(" & CStr(nSector) & ")" End If End With End Sub

Page 37: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

存入所有 Sector( 機器上 )Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim res As Boolean Dim nSector As Short

' Save Key To EEPROM for all sector ' szKey = TextBox1.Text With Form1 For nSector = 0 To ComboBox1.Items.Count - 1 ' for MIFARE 1K (total 16 sectors) For retry = 1 To 3 If RadioButton1.Checked Then ' Key Type A res = .MF5x1.mfSaveKey(nSector, MifareReader.bKeyTypeConstants.KEY_A, TextBox1.Text) Else ' Key Type B res = .MF5x1.mfSaveKey(nSector, MifareReader.bKeyTypeConstants.KEY_B, TextBox1.Text) End If

If res Then ' show status Label1.Text = "Save Key To EEPROM:OK(" & CStr(nSector) & ")" Exit For Else Label1.Text = "Save Key To EEPROM:NG(" & CStr(nSector) & ")" End If Next retry Next nSector End With End Sub

Page 38: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

• 改變任一個 READER 上 sector 的 KEY• 試試是否可以讀取資料?

Page 39: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

存 KEY 入 TAG

• 卡片要先通過認證,才能執行動作

Page 40: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

Private CB3, CB1, CB0, CB2 As Short Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click CB0 = 0 CB1 = 0 CB2 = 0 CB3 = 4

TextBox2.Text = TextBox2.Text & New String("0", 12 - Len(TextBox2.Text)) TextBox4.Text = TextBox4.Text & New String("0", 12 - Len(TextBox4.Text))

Label12.Text = vbNullString

If MF5x1.mfAccessCondition(TextBox2.Text, TextBox4.Text, CB0, CB1, CB2, CB3) Then Label12.Text = "OK" Else If MF5x1.GNetErrorCode = 0 Then Label12.Text = "NG" Else Label12.Text = "NG(" & MF5x1.GNetErrorCodeStr & ")" End If End If End Sub

Page 41: MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5

• 改變任一個 TAG 上 sector 的 KEY• 試試是否可以讀取資料?