title: Which APDU to use to read/write records on MIFARE Ultralight NFC tag?
body: <p>I am creating a VBA software to read and write NFC tags. I managed to communicate with the tag to obtain basic data (for example I got its ID). But when I send an APDU for a READ RECORD command, it does not work. I suppose I need to send a SELECT FILE command before; but I think I need the correct DF for this.</p>
  
  <p>I tried the "00 B2 01 05 00" for the READ RECORD command and "00 A4 00 00 00" for the SELECT FILE command.</p>
  
  <pre><code>VBA
  Sub test()
  Dim hContext As Long
  Dim hCard As Long
  Dim retval As Long
  Dim readers As String * 256
  Dim groups As String * 256
  Dim activeprotocol As Long
  Dim readerlen As Long
  Dim scard_protocol_t0_or_t1 As Long
  Dim scard_share_shared As Long
  Dim APDU() As Byte
  Dim recvbuf() As Byte
  Dim recvbuff(256) As ByteArray
  Dim recvlen As Long
  Dim iosendreq As SCARD_IO_REQUEST
  Dim iorecvreq As SCARD_IO_REQUEST
  Dim bytRecvAttr As ByteArray
  Dim LenAPDU As Long
  
  scard_protocol_t0_or_t1 = 1 Or 2
  scard_share_shared = 2 '2  1
  
  retval = SCardEstablishContext(SCARD_SCOPE_USER, 0, 0, hContext)
  If retval &lt;&gt; 0 Then
      MsgBox "erreur n." &amp; CStr(retval)
  End If
  
  readerlen = 256
  
  retval = SCardListReaders(hContext, groups, readers, readerlen)
  If retval &lt;&gt; 0 Then
      MsgBox "erreur n. " &amp; CStr(retval)
  End If
  
  
  Do While hCard = 0
      retval = SCardConnect(hContext, readers, scard_share_shared,         scard_protocol_t0_or_t1, hCard, activeprotocol)
      DoEvents
  Loop
  If retval &lt;&gt; 0 Then MsgBox "erreur n. " &amp; CStr(retval)
  
  recvlen = 256
  
  
  Do While State &lt; 2
      retval = SCardStatus(hCard, readers, readerlen, State,     scard_protocol_t0_or_t1, recvbuff(0), recvlen)
      If retval &lt;&gt; 0 Then MsgBox "Erreur n. " &amp; CStr(retval)
      DoEvents
  Loop
  
  
  'retval = SCardGetAttrib(hCard, SCARD_ATTR_ICC_PRESENCE, bytRecvAttr, lRecLen)
  'If retval &lt;&gt; 0 Then MsgBox "Erreur n. " &amp; CStr(retval)
  
  
  
  iosendreq.dwProtocol = activeprotocol
  iosendreq.dwPciLength = Len(iosendreq)
  iorecvreq.dwProtocol = activeprotocol
  iorecvreq.dwPciLength = Len(iorecvreq)
  
  
  'recherche de file
  ReDim APDU(7)
  APDU(0) = &amp;H0    'cla  80
  APDU(1) = &amp;HA4  'INs  B2-read record
  APDU(2) = &amp;H4   'P1 1-PREMIER RECORD
  APDU(3) = &amp;H0   'P2   5-tous les records
  APDU(4) = &amp;H2   'longueur de data
  APDU(5) = &amp;H0   'a changer pour trouver ID du DF
  APDU(6) = &amp;H0   'a changer pour trouver ID du DF
  APDU(7) = &amp;H0   'Le
  
  
  LenAPDU = (UBound(APDU) + 1)
  If APDU(LenAPDU - 1) = 0 Then
      ReDim recvbuf(255)
  Else
      ReDim recvbuf(APDU(LenAPDU - 1) + 2)
  End If
  recvlen = UBound(recvbuf) + 1
  
  'this is a loop to test the different DFs
  For x = 0 To 255
      For y = 1 To 255
          APDU(5) = x
          APDU(6) = y
          retval = SCardTransmit(hCard, iosendreq, APDU(0), LenAPDU, iorecvreq, recvbuf(0), recvlen)
          If recvbuf(4) &gt; 0 Or recvlen &gt; 2 Then Exit For
      Next
  Next
  
  Debug.Print Hex(recvbuf(0)) &amp; "-" &amp; Hex(recvbuf(1)) &amp; "-" &amp; Hex(recvbuf(2)) &amp; "-" &amp; Hex(recvbuf(3)) &amp; "-" &amp; Hex(recvbuf(4)) &amp; "-" &amp; Hex(recvbuf(5)) &amp; "-" &amp; Hex(recvbuf(6)) &amp; "-" &amp; Hex(recvbuf(7)) &amp; " " &amp; recvlen &amp; " CLA:" &amp; APDU(0) &amp; " INS:" &amp; APDU(1) &amp; " P1:" &amp; APDU(2) &amp; " P2:" &amp; APDU(3)
  
  
  If retval &lt;&gt; 0 Then MsgBox "Erreur n. " &amp; CStr(Hex(retval))
  
  retval = SCardDisconnect(hCard, scad_leave_card)
  If retval &lt;&gt; 0 Then MsgBox "erreur n. " &amp; CStr(retval)
  
  retval = SCardReleaseContext(hContext)
  If retval &lt;&gt; 0 Then MsgBox "erreur n. " &amp; CStr(retval)
  
  End Sub
  </code></pre>