Mifare Application Programming Guide
Mifare Application Programming Guide
TM970014 REV.H
January 16 2009
Table of Contents
Overview .................................................................................................................................... 5
2
MIFARE® Card Access Scheme (MFAP Flow Chart)
Change Card
Request Standard Request Standard Request All
Anitcollision Loop
Present
NO Select Card
YES
Change Sector
Authentication
Anitcollision Loop
(With Sector)
(Get Card S/N)
Halt
Value Read Block Write Block
(Return to
Operator
Send Card S/N to PC Auto Mode)
1. Data Format that MF5 sends the card’s Serial No. to Host in Auto Mode
<STX>CARD-SERIAL NO.<CR><ETX>
STX=02h, CR=0Dh, ETX=03h
2. The MF5 will send 0x1B (ESC) to Host when card removed. (May 23, 2008)
Anticollision Loop:
Get the Serial No. from the card that answers the request by [Anticollision] command in order to
select the card for operation.
3
Select Card:
Select an individual card for operation by [Select] command. The operation can be made on only
one card at one time. This is a necessary step if there are multiple cards within the reading range
of MF5.
Authentication:
After the selection, use the corresponding keys for the Authentication procedure to access the
selected Sector/Block of the card. After Authentication, memory operation may be performed.
Note: Use the [Save Key] command to pre-save the corresponding keys of each sector to MF5, which may reduce the risk
Read/Write Block
As far as MIFARE® Standard Card (1K) is concerned, there are 4 Blocks in each Sector and
16-Byte memory in each Block. [Select] the Block and send [Read/Write] command for memory
operation.
Value Operator
Arithmetic operation for electronic purse application. The Command Set includes:
1.Format
2.Read Value
3.Increase Value (with Transfer)
4.Decrease Value (with Transfer)
Like the Read/Write Block, select the Block and [Format] it before performing Value Operator
command.
Halt:
Use either [Authentication] command to access other Sectors or [Halt] Command to terminate the
operation of the card. In the latter case, the card must be withdrawn from the reading range of
MF5 in order to perform the next operation.
Summary:
1. It takes 3 steps to pick out a card for operation - Request, Anticollision and Select.
2. An Authentication command has to be carried out before any memory operation.
3. If any mistake occurs during operation, go back to step Request and operate again.
4. Pre-save the Keys of each Sector to MF5 to avoid the risk of interception.
5. The card must be pulled out of the reading range of MF5 after Halt command.
4
MF5A ActiveX Control Programming Guide
Overview
With MF5A ActiveX Control, it is not necessary to study how to communicate with MF5, neither to
write any program for communication protocol to perform the operation of MIFARE® card. MF5
Active Control is automatically registered to the computer when MF5 Demo Software is installed.
The file name is MF5Ax ActiveX Control Module.
Note: Reference can be also made to the VB Source Code of MF5 Demo Software in CD.
Customer Application
Mifare
Query
Command
GNETPlus Protocol
Control
Mifare
RS232 Control
RWD Device
5
The Properties, Methods & Events of MF5A ActiveX Control
Properties:
CommPort
Settings
PortOpen
mfCurrentClass / mfCurrentClassStr
GNetErrorCode / GNetErrorCodeStr
Busy
GetVersion
CurrentAddr
Methods:
EnumCommPort
SetSlaveAddr
mfRequest / mfRequestEx
mfAnticollision
mfSelectCard / mfSelectCardEx
mfAuthenticate / mfAnticollision2
mfRead / mfReadEx / mfReadHex
mfWrite / mfWriteEx / mfWriteHex
mfGetValue / mfGetValueEx
mfSetValue / mfSetValueEx
mfValueSet
mfHalt
mfSaveKey
mfAccessCondition
Polling
Reset
Events:
OnPort
OnCardEvent
6
RS232 Properties, Methods and Events
Property CommPort
Description Sets and returns the Comm Port number (Default COM1)
Syntax Object.CommPort [= Integer]
Parameter Integer , COM PORT number , 1=COM1, 2=COM2 …
Property Settings
Description Sets and returns the Settings value (Default 19200,N,8,1)
Syntax Object.Settings [= String]
Parameter String; [Baudrate][, Parity][, Data Bits][, Stop Bits]
VB Example MF5Ax1.Settings = "19200,N,8,1"
MF5Ax1.Settings = "19200"
MF5Ax1.Settings = "N,8,1"
Property PortOpen
Description Sets and returns the open status of the Comm Port
Syntax Object.PortOpen [= Boolean]
Parameter Boolean, TRUE=Port Open, FALSE=Port Close
Method EnumCommPort
Description List all available Comm Ports (including the Virtual ones)
Syntax String = Object.EnumCommPort(short index)
Parameter Return String, COM Port Name, (Examples "COM1"…)
Index, 0~255
VB Example Dim szPort as String, i as integer
For i = 0 to 255
szPort = MF5x1.EnumCommPort(i)
If szPort <> vbNullString Then
...
Else „ If szPort is vbNullString , to exit the for loop
Exit For
End If
Next i
Event OnPort
Description When a USB Virtual Comm Port is removed during connection, this event will inform the
program that it is removed and cannot be used any more.
Syntax Private Sub MF5x1_OnPort(ByVal Action As MF5AXLib.CommPortEventConstants, ByVal
CommPort As Integer)
Select Case Action
Case comEvPlugin: ShowMsg "COM" & CommPort & " is plug-in"
Case comEvRemove: ShowMsg "COM" & CommPort & " is remove"
Case comEvRemoveClosed: ShowMsg "COM" & CommPort & " is remove & closed"
End Select
End Sub
7
Mifare Access Properties, Methods and Events
Property mfCurrentClass (Read Only)
Description Return current Card Class number
Syntax Short = Object.mfCurrentClass
Parameter
8
Method mfRequest
Description Send Request command and return the Card Class number.
Syntax Short = Object.mfRequest
Parameter Return card class number
Note Use the method to check card into reader RF range.
Method mfAnticollision
Description Send Anticollision command and return the Card S/N (Serial Number).
Syntax Long = Object.mfAnticollision
Parameter Return card S/N, It is a long data type.
Method mfSelectCard
Description Send Select Card command and return Card memory size (unit kbits)
Property mfAuthenticate
Description Select a Sector and Authenticate with key
Syntax Boolean = Object.mfAuthenticate(Sector, KeyType, szKey)
Parameter Sector : Short, for Sector number
KeyType: Short, for KEY_A(60h) or KEY_B(61h)
szKey: HEX String, 12 Hex Codes
9
Method mfRead
Description Read a Block data from Mifare Card.
Syntax Boolean = Object.mfRead(Block, pBuffer, nSize)
Parameter Block: Short, Block number
pBuffer: Long, Buffer Address pointer.
nSize: Short, Buffer size, Max.16 Bytes.
VB Examples (To Read Block 2 Data from card)
Dim blkBuffer(0 to 15) as BYTE, bResult as Boolean
bResult = MF5x1.mfRead(2, VarPtr(blkBuffer), lenB(blkBuffer))
Note: If program by VB, you can use the "VarPtr" to got the variable
long address pointer.
Method mfWrite
Note: If programming by VB, you can use the "VarPtr" to get the variable
long address pointer.
Method mfValueSet
Description Operate the value block for increase or decrease in old value.
Syntax Boolean = Object.mfValueSet(Block, Opt, Value)
Parameter Block : Short, Block Number
Opt : Short, MF_INC(Increase) or MF_DEC(Decrease)
Value : Long, operate value.
10
Method mfHalt
Description To halt the current selected card, this card must leave the reader's RF range.
Syntax Boolean = Object.mfHalt
Parameter
Method mfAutoMode
Description To halt the current selected card, this card must leave the reader's RF range.
Syntax Boolean = Object.mfAutoMode(Boolean)
Parameter TRUE=Enable, FALSE=Disable
Method mfSaveKey
Description Save the sector key to reader.
Syntax Boolean = Object.mfSaveKey(KeyType, nSector, szKey)
Parameter KeyType : Short, For KEY_A or KEY_B
nSector : Short, For Sector number
szKey : String, 12 HEX Codes.
11
Method mfAccessCondition / mfAccessConditionEx
12
Method mfGetAccessCondition
Description The reader sends the event to the host when the card inserted or
removed.
Syntax Boolean = Object.mfGetAccessCondition(*CB0, *CB1, *CB2, *CB3, *GPB)
Parameter CB0, CB1, CB2, CB3: Point to a short variable, that receives the
CB0~CB3 access conditions value.
GPB: Point to a short variable, that receives the MAD GPB value.
Description The reader sends the event to the host when the card inserted or
removed. (For Auto Mode only)
Syntax
Parameter iEvent, short type:
MF_CARD_REMOVE = Card Remove
MF_CARD_PRESENT = Card Insert
13
GNetPlus Properties and Method (Common)
Property Busy (Read Only)
Parameter
Parameter
Method SetSlaveAddr
Description Return current machine firmware version.
Parameter
Method GetVersion
Parameter
Method Polling
Method Reset
14
Mifare Application Protocol
MFAP Block:
HOST DEVICE
Mifare Mifare
Application Device
Query Response
GNetPlus GNetPlus
Communication Communication
Protocol Protocol
RS232 RS232
15
MFAP Query Function Code Table (20h~2Fh)
Query (Master/Host) Response (Slave/Device)
Desc Func Len Data Bytes Func Len Data Bytes
Request 20h 0 ACK 2 Card Class Type
(Integer)
Anti-collision 21h 0 ACK 4 Card Serial Number
(Long)
Select Card 22h 4 Card Serial Number (Long) ACK 1 Card Memory Size
1
Authenticate 23h 2 KEY_TYPE + SECTOR ACK 0
2
Read a Block 24h 1 Block# ACK 16 Block Data
Write a Block 25h 17 Block# + Block Data ACK 0
3
Set Value 26h 6 Block# + OPT + Value (Long) ACK 0 with Transfer
Read Value 27h 1 Block# ACK 4 Value (Long)
Create a Value 28h 1 Block# ACK 0
Block
Access Condition 29h 16 KEYA4+CB0+CB1+CB2+CB3+KEYB4 ACK 0
4 4
Access Condition 29h 17 KEYA +CB0+CB1+CB2+CB3+GPB+KEYB ACK 0
Halt 2Ah 0 ACK 0
4
Save Key 2Bh 8 KEY_TYPE + SECTOR + KEY ACK 0
Get Second S/N 2Ch 0 ACK 4 Card second S/N
(Long)
Get Access 2Dh 0 ACK 5 CB0,CB1,CB2,CB3,GPB
Condition
Authenticate + Key 2Eh 8 KEY_TYEP + SECTOR + KEY4 ACK 0
RequestAll 2Fh 0 ACK 2 Card Class Type
(Integer)
SetValueEx 0x32 6 Block# + OPT3+Value (Long) ACK 0 without Transfer
Transfer 0x33 1 Block#
Restore 0x34 1 Block#
GetSector 0x3D 2 AID5# ACK 1 Return Sector# By AID
RF Power On/Off 0x3E 1 0=OFF, 1=ON ACK 0
AutoMode 3F 1 0:Disable, 1:Enable ACK Current Mode
Note:
4. KEYA, KEYB, KEY: KEY VALUE, Size=6 Bytes. Example: (LSB) 2C 1B 30 26 3A B7 (MSB)
Remark:
1. Authenticate (23h): Must save key (2Bh) before authenticating the sector.
4. Access Condition: About CB0,CB1,CB2 and CB3, please see page 11.
16
ANNEX. A - GNetPlus Protocol Examples (ASCII Mode)
The ASCII Mode is easy to send command from HyperTerminal (or other communication Terminal) to Reader,
®
and easy to learn the MIFARE operation.
P.S:
1. ACK = 06h
2. NAK = 15h
17
ANNEX. B - Error Code
18
ANNEX. C - WebISP - Firmware Upgrade Utility (Internet Version)
Install the WebISP (include in CD-ROM) in your Windows System first (It may need to
reboot your system) and follow the steps as below: (First of all, you need to connect
the reader or programmer to PC, and make sure they were power-on)
Step 1: Input your account
(UserName and Password)
Note:
Contact us to get your
account when needed.
19
Step 4: Wait for the
updating to finish. And
repeat step 2 to update
other readers or
programmers.
Update finished
20
ANNEX. D - History
22