Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / Visual Basic

SmartCard Direct Serial Interface

Rate me:
Please Sign up or sign in to vote.
4.90/5 (15 votes)
2 Nov 2010CPOL3 min read 87.6K   2.4K   34   20
How to integrate SmartCard interface in an application

Introduction

This article describes a way of implementing a SmartCard control based application upon a Serial interface.
The hardware interface used is widely known as "Phoenix", and I'm going to describe the hardware architecture about this, and how to integrate it with a Visual Basic application needless to use PC/SC specification.
Otherwise, the target of this article is to show how to directly interact with the smartcard toward the hardware interface told before.

Each SmartCard vendor implements its own command set named APDU (Append Protocol Data Unit) based on ISO-7816 specification. In this way, we can explode each SmartCard type with the correct APDU commands set; in this way, we can use Mifare, TIBC or any other type of Smartcard as an RFID Tag for identity purposes by this VB interface.

To get it working, it's only necessary to feed the circuit with 5V continuous current and an RS-232 PC serial interface.

Interface

Phoenix interface is a serial interface with a 9 pin serial jack, and a 10 pin SmartCard connector (pins 4, 6 and 8 unplugged). This interface is just developed and it's the base of this project.
It's composed of a 10 pin SmartCard module, a MAX232 IC used to convert RS-232 (serial) levels to TTL voltages, a 3.579 Mhz quartz crystal used as pulse generator, a voltage regulator and several capacitors, resistors and leds used as pull-up/pull-down resistors, or voltage generators basically.

The serial port pin connections are shown in phoenix electronic schema:

phoenix.JPG

The idea is to connect the Tx and Rx serial pins with the SmartCard interface I/O pin managed by a bidirectional logic.
Otherwise, SmartCard reset pin is connected via MAX232 to RTS serial line, so reading the ATR is so easy as driving this line to inverse voltage levels during a few clock cycles.

SmartCard connector pinout is shown in this table:

Smartcard Module PinOut

Pin # Pin Name Pin Description
1 VCC +5v or 3.3v DC
2 Reset Card Reset
3 CLOCK Card Clock
5 GND Ground
7 I/O In/Out [Data]
9,10 AS Presence Switch

Using the Code

The program resident on host machine is responsible for the serial port initialization and reading/writing process. Getting finite states machine ready to accept commands, here is the sequence:

Once the serial port is initialized, getting the interface ready for reading/writing is composed of only 4 steps:

  1. Feeding the SmartCard; this is got by connecting RS-232 interface and Vcc SmartCard pin will be directly fed by Vcc Serial pin.
  2. Initializing serial communications; this interface needs to be initialized to 9600 bauds, equal parity, 8 data bits and 1 parity bit:

    Serial.JPG

  3. Reseting the SmartCard; you can do it by putting tension on serial 4 pin and then putting it off. After about 40000 cycles (depends on card), you can read the ATR at serial buffer register; so, 100 msg is enough time.
    VB.NET
    ObjSerial.RTSEnable = True
    ObjSerial.RTSEnable = False
    Sleep (100)
    buffer$ = ObjSerial.Input

    Translating ATR byte[] to strings of pairs is done by InterpretarHex function:

    VB.NET
    Private Function InterpretarHex(buffer, Optional conEspacios As Boolean) As String
    
    	Dim sol As String
    	Dim byteAux
      
    	If Len(buffer) > 0 Then
    		byteAux = Hex(Int(Asc(Mid(buffer, 1, 1))))
    		sol = byteAux
    		For i = 2 To Len(buffer)
    			byteAux = Hex(Int(Asc(Mid(buffer, i, 1))))
    			If Len(byteAux) = 1 Then byteAux = "0" + byteAux
    			If (conEspacios) Then
    				sol = sol + " " + byteAux
    			Else
    				sol = sol + byteAux
    			End If
    		Next i
    	End If
      
    	InterpretarHex = sol
      
    End Function	
  4. Reading serial buffer gets the Answer To Reset bytes (ATR).
    VB.NET
    buffer$ = ObjSerial.Input

ATR.JPG

Once having read serial buffer and having RTS pin at low level, the interface is ready for the transmission of the desired APDU command.
Here's an APDU example :

VB.NET
ObjSerial.RTSEnable = False

For i = 1 To (Len(txtCmd.Text) / 2)
    ObjSerial.Output = Chr(Comanda(Mid$(txtCmd.Text, i, 2)))
Next i

Command.JPG

VB.NET
buffer$ = ObjSerial.Input
txtResponse.Text = InterpretarHex(buffer$)

Response.JPG

This is a picture of the working project:

Project.JPG

Points of Interest

This software/hardware interface could be used to integrate SmartCard login in any application you could think about, or you can use it for SmartCard investigation projects.

History

  • 1st November, 2010: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Spain Spain
I am a software developer grown with programming paradigm evolution. Think that C is God's Programming Language.

Comments and Discussions

 
QuestionWhere i can download vb file? Pin
LytYrs12-Feb-14 3:07
LytYrs12-Feb-14 3:07 
QuestionQuestion Pin
PM12345678922-Jul-13 7:20
PM12345678922-Jul-13 7:20 
AnswerRe: Question Pin
Skynet_Code22-Jul-13 23:09
Skynet_Code22-Jul-13 23:09 
GeneralRe: Question Pin
PM12345678923-Jul-13 1:12
PM12345678923-Jul-13 1:12 
GeneralMy vote of 5 Pin
m_harriss17-Jul-13 21:15
m_harriss17-Jul-13 21:15 
GeneralRe: My vote of 5 Pin
Skynet_Code18-Jul-13 3:54
Skynet_Code18-Jul-13 3:54 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey3-Jul-13 19:34
professionalManoj Kumar Choubey3-Jul-13 19:34 
GeneralRe: My vote of 5 Pin
Skynet_Code3-Jul-13 20:23
Skynet_Code3-Jul-13 20:23 
GeneralMy Vote of 5 Pin
RaviRanjanKr17-Dec-10 22:51
professionalRaviRanjanKr17-Dec-10 22:51 
GeneralRe: My Vote of 5 Pin
Skynet_Code20-Dec-10 1:50
Skynet_Code20-Dec-10 1:50 
GeneralMy vote of 5 Pin
TweakBird9-Dec-10 22:30
TweakBird9-Dec-10 22:30 
GeneralRe: My vote of 5 Pin
Skynet_Code11-Dec-10 20:46
Skynet_Code11-Dec-10 20:46 
GeneralMy vote of 5 Pin
chorra pana5-Nov-10 3:09
chorra pana5-Nov-10 3:09 
GeneralRe: My vote of 5 Pin
Skynet_Code5-Nov-10 20:40
Skynet_Code5-Nov-10 20:40 
GeneralFantastic code Pin
chorra pana5-Nov-10 3:08
chorra pana5-Nov-10 3:08 
GeneralRe: Fantastic code Pin
Skynet_Code5-Nov-10 20:39
Skynet_Code5-Nov-10 20:39 
QuestionHow about USB ? Pin
Vitaly3-Nov-10 8:25
Vitaly3-Nov-10 8:25 
AnswerRe: How about USB ? Pin
Skynet_Code3-Nov-10 21:50
Skynet_Code3-Nov-10 21:50 
GeneralLooks great! Pin
Shane Story3-Nov-10 5:24
Shane Story3-Nov-10 5:24 
GeneralRe: Looks great! Pin
Skynet_Code3-Nov-10 21:42
Skynet_Code3-Nov-10 21:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.