Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
PC/SC Get Card UID
See more SCard Examples
Sends the APDU command to get a card's UID.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.BinData,
Chilkat.SCard;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
scard: TSCard;
bdRecv: TBinData;
numBytes: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
scard := TSCard.Create;
// First establish a context to the PC/SC Resource Manager
success := scard.EstablishContext('user');
if (success = False) then
begin
WriteLn(scard.LastErrorText);
Exit;
end;
// Use your own smart card reader name here.
success := scard.Connect('ACS ACR122 0','shared','no_preference');
if (success = False) then
begin
WriteLn(scard.LastErrorText);
Exit;
end;
WriteLn('Connected reader: ' + scard.ConnectedReader);
WriteLn('Active protocol: ' + scard.ActiveProtocol);
WriteLn('ATR: ' + scard.CardAtr);
WriteLn('Reader Status: ' + scard.ReaderStatus);
// Send the APDU command 0xFF, 0xCA, 0x00, 0x00, 0x00
bdRecv := TBinData.Create;
success := scard.TransmitHex(scard.ActiveProtocol,'FFCA000000',bdRecv,32);
if (success = True) then
begin
WriteLn('Received: ' + bdRecv.GetEncoded('hex'));
// The UID is the returned data without the final 2 bytes.
numBytes := bdRecv.NumBytes;
if (numBytes > 2) then
begin
WriteLn('UID: ' + bdRecv.GetEncodedChunk(0,numBytes - 2,'hex'));
end;
end
else
begin
WriteLn(scard.LastErrorText);
end;
// Disconnect from this reader.
success := scard.Disconnect('leave');
if (success = False) then
begin
WriteLn(scard.LastErrorText);
end;
// Applications should always release the context when finished.
success := scard.ReleaseContext();
if (success = False) then
begin
WriteLn(scard.LastErrorText);
end;
scard.Free;
bdRecv.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.