A PC/SC wrapper to communicate with smartcards

7 releases

0.3.4 May 4, 2017
0.3.3 Apr 18, 2017
0.3.2 Feb 15, 2017
0.2.0 Dec 23, 2016
0.1.0 Dec 22, 2016
Download history 28/week @ 2020-03-18 7/week @ 2020-03-25 29/week @ 2020-04-08 29/week @ 2020-04-15 2/week @ 2020-04-29 1/week @ 2020-05-06 21/week @ 2020-05-13 7/week @ 2020-05-20 14/week @ 2020-05-27 14/week @ 2020-06-03 1/week @ 2020-06-17 1/week @ 2020-06-24 15/week @ 2020-07-01

51 downloads per month

MIT license

417 lines

This is an early version library, everything can break at any moment.


This library enables communication between a PC and a smartcard (SC), using PC/SC drivers.

How to use this library

Add the crate

In your Cargo.toml:

smartcard = "0.3"

In your main.rs or lib.rs:

extern crate smartcard;

Example code

In the main.rs:

extern crate smartcard;

use smartcard::logic::Context;
use smartcard::parameters::{ShareMode, Protocol};
use smartcard::errors::*;

use std::sync::Arc;

fn run() -> Result<()> {
    //First we create the resource manager context. I think of it as 'the driver'.
    let context = Arc::new(try!(Context::establish_context_auto()));

    //The context allows to list all available card readers.
    let mut readers = try!(context.list_readers());

    println!("{} readers found:", readers.len());
    for r in readers.iter() {
        println!("- {}", r.get_name());

    //Let's get the first reader.
    let reader = try!(readers.pop().ok_or(format!("no readers found")));

    //From the reader, we can connect to its smartcard this way.
    let card = try!(reader.connect_to(context, ShareMode::Auto, Protocol::Auto));
    //we use an Arc<Context> so that even if we
    //the context still exists while the card is alive

    //Now that we have a card available, we can send commands to it.
    //select app on my card
    let cmd_vec = vec![0x00, 0xA4, 0x04, 0x00, 0x0B, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00];
    let answer = try!(card.send_raw_command(&cmd_vec, 256));//256 is the maximum size of the expected answer

    println!("Answer: {:?}", answer);//I get 0x90 0x00, perfect!

fn main() {
    match run() {
        Ok(_) => {},
        Err(e) => println!("An error occured: {}.", e.to_string())

Supported platform and dependencies

The pcsc-sys crate is used to link with the drivers. It should work on Linux, Windows and MacOS but I only tested it on Linux.

On ubuntu based systems:

sudo apt-get install pcscd libpcsclite1

Optionnally you can install the always useful pcsc-tools package:

sudo apt-get install pcsc-tools

With this tools you can detect your readers with


PRs welcome.


You can check pcsc-rust for another library with the same goal. I reused the pcsc-sys crate from this project for the lib bindings (starting from smartcard-rs v 0.3.0 ). pcsc-rust should have better performances than smartcard-rs at the cost of a slightly less ergonomic API (in my opinion). In pcsc-rust the Card and Context are linked with lifetimes to prevent inappropriate Context dropping (instead of Arc in smartcard-rs case), which can cause self borrowing issues in some applications.