1 / 41

MIFARE_DEMO WITH PCR310, GIGATMS, Ltd.

MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5. Pcr310_fun.vb. Imports 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

braden
Download Presentation

MIFARE_DEMO WITH PCR310, GIGATMS, Ltd.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Pcr310_fun.vb Imports 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 Integer End Structure

  3. 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 = lLen End Function

  4. Pcr310_fun.vb Public 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

  5. Pcr310_fun.vb Public 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

  6. Pcr310_fun.vb Public 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

  7. Pcr310_fun.vb Public 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

  8. Pcr310_fun.vb Public 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

  9. Pcr310_fun.vb Public 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

  10. Pcr310_fun.vb Public 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

  11. 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

  12. 參考MS VB.COMPATIBILITY

  13. 需要三個dll檔案

  14. 透過參考加入dll

  15. 讀卡號

  16. Imports VB = Microsoft.VisualBasic Imports GIGATMS.NF.MifareReader Imports GIGATMS.NF.GNetPlus ‘ 宣告全域變數 ‘建立物件mf5x1 Public WithEvents MF5x1 As New GIGATMS.NF.MifareReader ‘卡號 Public Id As Integer

  17. 透過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 If End Sub

  18. 執行結果 出了甚麼問題?

  19. 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 = True End Sub

  20. 判斷卡片種類 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 If End Sub

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

  22. 讀取資料 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

  23. 讀取資料 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 If End Sub

  24. Private m_bNoChangeEvent As Boolean Function 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 If End Function

  25. 寫入資料 • BUTTON3

  26. 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

  27. '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

  28. 存入選擇的Sector(機器上) Imports GIGATMS.NF Private 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

  29. 存入所有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

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

  31. 存KEY入TAG • 卡片要先通過認證,才能執行動作

  32. 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

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

More Related