Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MIFARE_DEMO WITH PCR310, GIGATMS, Ltd. 進國科技大學資管系 饒瑞佶 2010/5."— Presentation transcript:

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

2

3

4 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

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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 參考 MS VB.COMPATIBILITY

15

16

17 需要三個 dll 檔案

18 透過參考加入 dll

19

20 讀卡號

21 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

22 透過 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

23 執行結果 出了甚麼問題?

24 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

25 判斷卡片種類 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

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

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

28 讀取資料 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 TextBox3.Text = tostring1(Label8.Text) Else Label9.Text = MF5x1.GNetErrorCodeStr TextBox3.Text=“” Button3.Enabled = False Exit Sub End If End Sub

29 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 Function

30

31 寫入資料 BUTTON3

32

33 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

34 '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

35

36 存入選擇的 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

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

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

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

40 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 Sub

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


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

Similar presentations


Ads by Google