在VB.NET应用程序上使用ACR122U获取RFID标签的UID

时间:2018-03-10 04:27:37

标签: vb.net nfc

我们需要获取放置在NFC读卡器上的NFC的UID:ACR122u。 使用SDK中的示例代码和Reader,我们能够将阅读器与我们的VB.net应用程序连接起来。但是当我们尝试发送APDU命令时,我们会收到一个空的Recvbuffer。

这是我们用来发送和recv APDU命令的代码(这是使用Winscard.dll):     公共功能SendAPDU()

    Dim indx As Integer
    Dim tmpStr As String

    pioSendRequest.dwProtocol = &H1
    pioSendRequest.cbPciLength = Len(pioSendRequest)

    tmpStr = ""

    For indx = 0 To SendLen - 1

        tmpStr = tmpStr + Microsoft.VisualBasic.Right("00" & Hex(SendBuff(indx)), 2) + " "

    Next indx

    Call displayOut(2, 0, tmpStr)

    retCode = ModWinsCard.SCardTransmit(hCard, pioSendRequest, SendBuff(4), SendLen, pioSendRequest, RecvBuff(0), RecvLen)

    If retCode <> ModWinsCard.SCARD_S_SUCCESS Then

        Call displayOut(2, retCode, "")
        SendAPDU = retCode
        Exit Function
    End If

    tmpStr = ""

    For indx = 0 To RecvLen - 1

        tmpStr = tmpStr + Microsoft.VisualBasic.Right("00" & Hex(RecvBuff(indx)), 2) + " "

    Next indx

    Call displayOut(3, 0, tmpStr)

    SendAPDU = retCode
End Function

此处还有按下按钮发送APDU命令的代码。     Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click         Dim tmpStr As String         Dim indx As Integer

    ' Validate Inputs
    '//        tBinData.Text = ""

    If tBinBlk.Text = "" Then

        tBinBlk.Focus()
        Exit Sub

    End If

    If CInt(tBinBlk.Text) > 319 Then

        tBinBlk.Text = "319"
        Exit Sub

    End If

    If tBinLen.Text = "" Then

        tBinLen.Focus()
        Exit Sub
    ElseIf CInt(tBinLen.Text) > 16 Then
        tBinLen.Text = "16"
        tBinLen.Focus()
        Exit Sub

    End If


    Call ClearBuffers()
    'Read Binary Block command
    SendBuff(0) = &HFF                              'Class
    SendBuff(1) = &HB0                              'INS
    SendBuff(2) = &H0                               'P1
    SendBuff(3) = CInt("&H" & tBinBlk.Text) 'P2 : Block number
    SendBuff(4) = CInt(tBinLen.Text)                  'Le : Number of bytes to read

    SendLen = 5
    RecvLen = CInt(tBinLen.Text) + 2

    retCode = SendAPDU()
    MsgBox(RecvLen)
    MsgBox(Hex(RecvBuff(0)))
    MsgBox(Hex(RecvBuff(1)))
    MsgBox(Hex(RecvBuff(2)))
    MsgBox(Hex(RecvBuff(3)))
    MsgBox(Hex(RecvBuff(4)))
    MsgBox(Hex(RecvBuff(5)))
    MsgBox(Hex(RecvBuff(6)))
    MsgBox(Hex(RecvBuff(7)))

    If retCode <> ModWinsCard.SCARD_S_SUCCESS Then
        Exit Sub
    Else
        For indx = RecvLen - 2 To RecvLen - 1
            'tmpStr = tmpStr & Right$("00" & Hex(RecvBuff(index)), 2)
            tmpStr = tmpStr + Microsoft.VisualBasic.Right("00" & Hex(RecvBuff(indx)), 2) + " "

        Next indx
        displayOut(3, 0, tmpStr)
        'Check for response
        If tmpStr.Trim = "90 00" Then
            tmpStr = ""
            For indx = 0 To RecvLen - 3

                'tmpstr = tempstr & Right$(Chr(RecvBuff(index)), 2)
                tmpStr = tmpStr + Chr(RecvBuff(indx))
                'tmpStr = tmpStr + Microsoft.VisualBasic.Right("00" & Hex(RecvBuff(indx)), 2) + " "

            Next indx

            tBinData.Text = tmpStr
        Else
            Call displayOut(4, 0, "Read block error!")
        End If
    End If

End Sub

我们的代码中有什么问题吗?或者我们如何发送APDU命令时出错。 我们在Windows 10 x64上运行它,只是想添加它以防它与我们的问题相关。

0 个答案:

没有答案