Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.87+

PC/SC Check the Status of each Smart Card Reader (and USB token)

See more SCard Examples

Demonstrates how to list the smart card readers and USB tokens connected to the computer, and check the status of each.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local scard = chilkat.SCard.new()

-- First establish a context to the PC/SC Resource Manager
local success = scard:establish_context("user")
if not success then
    print(scard.last_error_text)
    return
end

local stReaders = chilkat.StringTable.new()
success = scard:list_readers(stReaders)
if not success then
    print(scard.last_error_text)
    return
end

local numReaders = stReaders.count
local i = 0
while i < numReaders do
    local reader = stReaders:string_at(i)

    print(i .. ": " .. reader)

    -- Connect to this reader.
    if scard:connect(reader, "shared", "no_preference") then
        -- Check the status to get the reader state, active protocol, and card ATR (if a card is inserted).
        if scard:check_status() then
            print("ReaderStatus: " .. scard.reader_status)
            print("CardAtr: " .. scard.card_atr)
            print("ActiveProtocol: " .. scard.active_protocol)
        else
            print(scard.last_error_text)
            print("CheckStatus failed.")
        end

        -- Disconnect from this reader.
        success = scard:disconnect("leave")
    else
        print(scard.last_error_text)
        print("Connect failed.")
    end

    print("---------------------------------------")
    i = i + 1
end

-- Sample output:

-- 0: Alcor Micro USB Smart Card Reader 0
-- ReaderStatus: specific
-- CardAtr: 3B7F96000080318065B0855956FB120FFE829000
-- ActiveProtocol: T0
-- ---------------------------------------
-- 1: Generic Smart Card Reader Interface 0
-- ReaderStatus: specific
-- CardAtr: 3BFC180000813180459067464A00641606F2727E00E0
-- ActiveProtocol: T1
-- ---------------------------------------
-- 2: SCM Microsystems Inc. SCR33x USB Smart Card Reader 0
-- ReaderStatus: specific
-- CardAtr: 3BDF96FF8131FE455A018048494443313158587300011B09
-- ActiveProtocol: T1
-- ---------------------------------------

-- Applications should always release the context when finished.
success = scard:release_context()
if not success then
    print(scard.last_error_text)
end