Dear all,

I need help on programming Mifare cards.

I want to be able to read and write to a 1k mifare card. I bought a ACR1281U dual reader and also have some documentation but have not understand how to read data and write data to Mifare cards.

I did appreciate your support on this, should anybody be able to do this.

Thank you.

usually a product like this comes with a software disc or you can download it from their website..if thats not the case, is there any part in that documentation that talks about developing or programing if so, upload it or send a url.
anyway this is how you read the data

  • connect it to your pc
  • add a SerialPort control to your VB form and a Combobox
  • get the name of the ports that are connected to your pc

    ComboBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames)

  • assign your devise to the serialport

    SerialPort1.PortName = ComboBox1.SelectedItem.ToString

  • open it

    SerialPort1.Open()

  • Receive data as bytes and convert to plain text

    Private Sub DataReceived(ByVal sender As Object, _
    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
    Handles SerialPort1.DataReceived
    If SerialPort1.BytesToRead > 0 Then
    SerialPort1.Read(your data "bytes")
    end if
    'convert bytes to plain text
    System.Text.Encoding.ASCII.GetString(your data)

good luck

Thank you Oussama,

Attached is code snippet I have from them, perhaps it can be a pointer to provide me assitance.

Attachments
Public Class MainMifareProg

    Public retCode, hContext, hCard, Protocol As Long
    Public connActive, autoDet As Boolean
    Public SendBuff(262), RecvBuff(262) As Byte
    Public SendLen, RecvLen, nBytesRet, reqType, Aprotocol As Integer
    Public dwProtocol As Integer
    Public cbPciLength As Integer
    Public RdrState As ModWinsCard64.SCARD_READERSTATE
    Public dwState, dwActProtocol As Long
    Public pioSendRequest, pioRecvRequest As ModWinsCard64.SCARD_IO_REQUEST

    Private Sub MainMifareProg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call InitMenu()

    End Sub

    Private Sub InitMenu()

        connActive = False
        cbReader.Items.Clear()
        cbReader.Text = ""
        mMsg.Items.Clear()
        displayOut(0, 0, "Program ready")
        bConnect.Enabled = False
        bInit.Enabled = True
        bReset.Enabled = False
        rbNonVolMem.Checked = False
        rbSource2.Checked = True
        rbVolMem.Checked = True
        tMemAdd.Text = ""
        tKey1.Text = ""
        tKey2.Text = ""
        tKey3.Text = ""
        tKey4.Text = ""
        tKey5.Text = ""
        tKey6.Text = ""
        gbLoadKeys.Enabled = False
        tBlkNo.Text = ""
        tKeyAdd.Text = ""
        gbAuth.Enabled = False
        tBinBlk.Text = ""
        tBinLen.Text = ""
        tBinData.Text = ""
        gbBinOps.Enabled = False
        tValAmt.Text = ""
        tValBlk.Text = ""
        tValSrc.Text = ""
        tValTar.Text = ""
        gbValBlk.Enabled = False

    End Sub

    Private Sub ClearBuffers()

        Dim indx As Long

        For indx = 0 To 262

            RecvBuff(indx) = &H0
            SendBuff(indx) = &H0

        Next indx

    End Sub

    Private Sub displayOut(ByVal errType As Integer, ByVal retVal As Long, ByVal PrintText As String)

        Select Case errType

            Case 0
                mMsg.ForeColor = Color.Teal
            Case 1
                mMsg.ForeColor = Color.Red
                PrintText = ModWinsCard64.GetScardErrMsg(retVal)
            Case 2
                mMsg.ForeColor = Color.Black
                PrintText = "<" + PrintText
            Case 3
                mMsg.ForeColor = Color.Black
                PrintText = ">" + PrintText

        End Select
        mMsg.Items.Add(PrintText)
        mMsg.ForeColor = Color.Black
        mMsg.Focus()

    End Sub

    Private Sub EnableButtons()

        bInit.Enabled = False
        bConnect.Enabled = True
        bReset.Enabled = True
        bClear.Enabled = True

    End Sub

    Public Sub LoadListToControl(ByVal Ctrl As ComboBox, ByVal ReaderList As String)

        Dim sTemp As String
        Dim indx As Integer

        indx = 1
        sTemp = ""
        Ctrl.Items.Clear()

        While (Mid(ReaderList, indx, 1) <> vbNullChar)

            While (Mid(ReaderList, indx, 1) <> vbNullChar)
                sTemp = sTemp + Mid(ReaderList, indx, 1)
                indx = indx + 1
            End While

            indx = indx + 1

            Ctrl.Items.Add(sTemp)

            sTemp = ""
        End While

    End Sub

    Private Sub bInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bInit.Click

        Dim sReaderList As String
        Dim ReaderCount As Integer
        Dim ctr, indx As Integer

        sReaderList = ""
        For ctr = 0 To 512
            sReaderList = sReaderList + vbNullChar
        Next

        ReaderCount = 512

        ' 1. Establish context and obtain hContext handle
        retCode = ModWinsCard64.SCardEstablishContext(ModWinsCard64.SCARD_SCOPE_USER, 0, 0, hContext)

        If retCode <> ModWinsCard64.SCARD_S_SUCCESS Then

            Call displayOut(1, retCode, "")

            Exit Sub

        End If

        ' 2. List PC/SC card readers installed in the system
        retCode = ModWinsCard64.SCardListReaders(hContext, "", sReaderList, ReaderCount)

        If retCode <> ModWinsCard64.SCARD_S_SUCCESS Then

            Call displayOut(1, retCode, "")

            Exit Sub

        End If

        ' Load Available Readers
        Call LoadListToControl(cbReader, sReaderList)
        cbReader.SelectedIndex = 0
        Call EnableButtons()

        ' Look for ACR128 PICC and make it the default reader in the combobox
        For indx = 1 To cbReader.Items.Count - 1

            cbReader.SelectedIndex = indx

            If InStr("ACR128 PICC", cbReader.Text) = 0 Then

                Exit Sub

            End If

        Next indx

        Exit Sub
        cbReader.SelectedIndex = 0

    End Sub

    Private Sub bConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bConnect.Click

        If connActive Then

            retCode = ModWinsCard64.SCardDisconnect(hCard, ModWinsCard64.SCARD_UNPOWER_CARD)

        End If

        ' Shared Connection
        retCode = ModWinsCard64.SCardConnect(hContext, cbReader.Text, 1, 0 Or 1, hCard, Protocol)

        If retCode <> ModWinsCard64.SCARD_S_SUCCESS Then

            ' check if ACR128 SAM is used and use Direct Mode if SAM is not detected
            If InStr(cbReader.Text, "ACR128U SAM") > 0 Then

                '1. Direct Connection
                retCode = ModWinsCard64.SCardConnect(hContext, cbReader.Text, ModWinsCard64.SCARD_SHARE_DIRECT, 0, hCard, Protocol)

                If retCode <> ModWinsCard64.SCARD_S_SUCCESS Then

                    Call displayOut(1, retCode, "")
                    connActive = False

                    Exit Sub

                Else

                    Call displayOut(0, 0, "Successful connection to " & cbReader.Text)

                End If

            Else

                Call displayOut(1, retCode, "")
                connActive = False
                Exit Sub

            End If

        Else

            Call displayOut(0, 0, "Successful connection to " & cbReader.Text)

        End If

        connActive = True
        gbLoadKeys.Enabled = True
        gbAuth.Enabled = True
        gbBinOps.Enabled = True
        gbValBlk.Enabled = True
        rbKType1.Checked = True

    End Sub

    Private Sub bClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClear.Click

        mMsg.Items.Clear()

    End Sub

    Private Sub bReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bReset.Click

        If connActive Then

            retCode = ModWinsCard64.SCardDisconnect(hCard, ModWinsCard64.SCARD_UNPOWER_CARD)

        End If

        retCode = ModWinsCard64.SCardReleaseContext(hCard)

        Call InitMenu()

    End Sub

    Private Sub bQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bQuit.Click

        ' terminate the application
        retCode = ModWinsCard64.SCardReleaseContext(hContext)
        retCode = ModWinsCard64.SCardDisconnect(hCard, ModWinsCard64.SCARD_UNPOWER_CARD)

        End

    End Sub

    Private Sub bLoadKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLoadKey.Click

        Dim tmpLong As Byte

        ' Check for valid inputs
        If (Not (rbNonVolMem.Checked) And Not (rbVolMem.Checked)) Then

            rbNonVolMem.Focus()
            Exit Sub

        End If

        If rbNonVolMem.Checked Then

            If (tMemAdd.Text = "" Or Not Byte.TryParse(tMemAdd.Text.Trim(), System.Globalization.NumberStyles.HexNumber, Nothing, tmpLong)) Then

                tMemAdd.Focus()
                tMemAdd.Text = ""
                Exit Sub

            End If

            If CDec(tMemAdd.Text) > 31 Then
                tMemAdd.Text = "31"
                Exit Sub

            End If

        End If

        If (tKey1.Text = "" Or Not Byte.TryParse(tKey1.Text.Trim(), System.Globalization.NumberStyles.HexNumber, Nothing, tmpLong)) Then

            tKey1.Focus()
            tKey1.Text = ""
            Exit Sub

        End If

        If (tKey2.Text = "" Or Not Byte.TryParse(tKey2.Text.Trim(), System.Globalization.NumberStyles.HexNumber, Nothing, tmpLong)) Then

            tKey2.Focus()
            tKey2.Text = ""
            Exit Sub

        End If

        If (tKey3.Text = "" Or Not Byte.TryParse(tKey3.Text.Trim(), System.Globalization.NumberStyles.HexNumber, Nothing, tmpLong)) Then

            tKey3.Focus()
            tKey3.Text = ""
            Exit Sub

        End If

        If (tKey4.Text = "" Or Not Byte.TryParse(tKey4.Text.Trim(), System.Globalization.NumberStyles.HexNumber, Nothing, tmpLong)) Then

            tKey4.Focus()
            tKey4.Text = ""
            Exit Sub

        End If

        If (tKey5.Text = "" Or Not Byte.TryParse(tKey5.Text.Trim(), System.Globalization.NumberStyles.HexNumber, Nothing, tmpLong)) Then

            tKey5.Focus()
            tKey5.Text = ""
            Exit Sub

        End If

        If (tKey6.Text = "" Or Not Byte.TryParse(tKey6.Text.Trim(), System.Globalization.NumberStyles.HexNumber, Nothing, tmpLong)) Then

            tKey6.Focus()
            tKey6.Text = ""
            Exit Sub

        End If

        Call ClearBuffers()
        SendBuff(0) = &HFF                              ' CLA
        SendBuff(1) = &H82                              ' INS

        If rbNonVolMem.Checked Then
            SendBuff(2) = &H20                          ' P1 : Non volatile memory
            SendBuff(3) = CDec(tMemAdd.Text)            ' P2 : Memory location

        Else

            SendBuff(2) = &H0                           ' P1 : Volatile memory
            SendBuff(3) = &H20                          ' P2 : Session Key        

        End If
        SendBuff(4) = &H6                               ' P3
        SendBuff(5) = CLng("&H" + tKey1.Text)           ' Key 1 value
        SendBuff(6) = CLng("&H" + tKey2.Text)           ' Key 2 value
        SendBuff(7) = CLng("&H" + t

That's it, copy the code and paste it in your vb project and you should add a reference to this "ModWinsCard64.SCARD".
now pay attention to this code

    Private Sub InitMenu()

        connActive = False
        cbReader.Items.Clear()
        cbReader.Text = ""
        mMsg.Items.Clear()
        displayOut(0, 0, "Program ready")
        bConnect.Enabled = False
        bInit.Enabled = True
        bReset.Enabled = False
        rbNonVolMem.Checked = False
        rbSource2.Checked = True
        rbVolMem.Checked = True
        tMemAdd.Text = ""
        tKey1.Text = ""
        tKey2.Text = ""
        tKey3.Text = ""
        tKey4.Text = ""
        tKey5.Text = ""
        tKey6.Text = ""
        gbLoadKeys.Enabled = False
        tBlkNo.Text = ""
        tKeyAdd.Text = ""
        gbAuth.Enabled = False
        tBinBlk.Text = ""
        tBinLen.Text = ""
        tBinData.Text = ""
        gbBinOps.Enabled = False
        tValAmt.Text = ""
        tValBlk.Text = ""
        tValSrc.Text = ""
        tValTar.Text = ""
        gbValBlk.Enabled = False

    End Sub

you need to add these controls to your form like "bConnect.Enabled = False" you need to add a button and name it bConnect and for this "tKey1.Text" add a textbox named tKey1 and for "mMsg.Items.Clear()" add a listbox mMsg etc..when you finish run your program and press connect and test your device..
with this code you can connect and display data, clear them and print them (side note: a Buffer is what holds your data to read them).
good luck

I did not understand your advice, please can you make it more simpler.

Thank you.

try the code in the file "MifareCardProg_1.txt" if you get any errors post it here

Edited 2 Years Ago by oussama_1

Thank you Oussama,

okay, which part of the script handle writing and reading from mifare card. that is just the challenge I have.

And perhaps if you have another code that you have used and work fine I did appreciate you send it to me.

hi...

I also have the same problem where I could not read 1k Mifare tag . I use the reader cr038 from here http://www.cytron.com.my/viewProduct.php?pcode=RFID-ICRW-CR038&name=Mifare%20Reader/Writer%20CR038 ...
I also tried the coding as above but still fail to read it.. and based on the datasheet(user manual) given the command to be used in hexadecimal .. can anyone help me how to do it?

Your help is greatly appreciated ... thank you

*This is one of the command given *

Initialize Port: 0x0101
Function: Set Baud Rate of CR038’s UART
Packet Format (Hexadecimal): AA BB 06 00 00 00 01 01 Baud XOR
AA BB = Header, 2 bytes.
06 00 = Packet length, 2 bytes, lower byte first. This indicate there are 6 bytes of data from Node
ID to XOR.
00 00 = Node ID, Serial number of CR038, 2 bytes, lower byte first. 00 00 mean broadcast, it
works for any ID.
01 01 = Function/Command Code for Initialize Port, 2 bytes, lower byte first.
Baud = Parameter for Baud rate. 1 byte.
● 0 = 4800 bps
● 1 = 9600 bps
● 2 = 14400 bps
● 3 = 19200 bps
● 4 = 28800 bps
● 5 = 38400 bps
● 6 = 57600 bps
● 7 = 115200 bps
XOR = result of exclusive OR operation from Node ID to Baud, in this example.
Example: To set the UART Baud rate to 19200. You will need to send command in the current
baud rate, not the desire baud rate. This is the command from host to CR038:
Command (Hexadecimal): AA BB 06 00 00 00 01 01 03 03
Reply from CR038 (Hexadecimal): AA BB 06 00 BF FF 01 01 00 40
BF FF = Node ID or Serial number of CR038, 2 bytes, lower byte 1st.
01 01 = Function/Command Code from host, 2 bytes, lower byte 1st.
00 = Status, 1 byte. 0 mean success, other than 0 mean fail.
40 = Result of XOR operation from Node ID to Status. Try it and you will get 0x40

Set Antenna Status: 0x010C
Function: Set Antenna status on CR038, needed to access (read or write) to Mifare card.
Packet Format (Hexadecimal): AA BB 06 00 00 00 0C 01 Antenna XOR
AA BB = Header, 2 bytes.
06 00 = Packet length, 2 bytes, lower byte first. This indicate there are 6 bytes of data from Node
ID to XOR.
00 00 = Node ID, Serial number of CR038, 2 bytes, lower byte first. 00 00 mean broadcast, it
works for any ID.
0C 01 = Function/Command Code for Set Antenna Status, 2 bytes, lower byte first.
Antenna = Parameter for Antenna status. 1 byte.
● 0 = Antenna OFF
● 1 = Antenna ON
XOR = result of exclusive OR operation from Node ID to Antenna, in this example.
Example: To ON (Activate) Antenna on CR038 (default is OFF). This is the command from host to
CR038:
Command (Hexadecimal): AA BB 06 00 00 00 0C 01 01 0C
Reply from CR038 (Hexadecimal): AA BB 06 00 BF FF 0C 01 00 4D
BF FF = Node ID or Serial number of CR038, 2 bytes, lower byte 1st. It can be any value.
0C 01 = Function/Command Code from host, 2 bytes, lower byte 1st.
00 = Status, 1 byte. 0 mean success, other than 0 mean fail.
4D = Result of XOR operation from Node ID to Status.
The Antenna on CR038 will be activated and you can further send command to access the Mifare
card.

Edited 2 Years Ago by ctrah

This article has been dead for over six months. Start a new discussion instead.