📄 st14c02 reader.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using GrabbaDotNet;
namespace ST14C02_Reader
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class ST14C02Reader : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem MainMenu;
private System.Windows.Forms.MenuItem ExitMenuItem;
private Grabba grabba = new Grabba();
private System.Windows.Forms.TextBox CardMemory;
private System.Windows.Forms.Button ReadCard;
private System.Windows.Forms.Button FillMemory;
private System.Windows.Forms.TextBox MemoryCharacter;
private Grabba myGrabba = new Grabba();
public ST14C02Reader()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Open the Grabba
if (myGrabba.Open() != 0)
{
// If the Grabba can not be openned then display an error message and abort
Application.Exit();
}
// add event handler
Grabba.GrabbaSmartCardInsertionEvent += new EventHandler(onSmartCardInserted);
Grabba.GrabbaSmartCardRemovalEvent += new EventHandler(onSmartCardRemoved);
Grabba.GrabbaDisconnectedEvent += new EventHandler(onGrabbaDisconnectedEvent);
Grabba.GrabbaReconnectedEvent += new EventHandler(onGrabbaReconnectedEvent);
// Enable the Grabba
myGrabba.SmartCardEnable();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.MainMenu = new System.Windows.Forms.MenuItem();
this.ExitMenuItem = new System.Windows.Forms.MenuItem();
this.CardMemory = new System.Windows.Forms.TextBox();
this.ReadCard = new System.Windows.Forms.Button();
this.FillMemory = new System.Windows.Forms.Button();
this.MemoryCharacter = new System.Windows.Forms.TextBox();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.MainMenu);
//
// MainMenu
//
this.MainMenu.MenuItems.Add(this.ExitMenuItem);
this.MainMenu.Text = "Main";
//
// ExitMenuItem
//
this.ExitMenuItem.Text = "Exit";
this.ExitMenuItem.Click += new System.EventHandler(this.ExitMenuItem_Click);
//
// CardMemory
//
this.CardMemory.Location = new System.Drawing.Point(8, 40);
this.CardMemory.Multiline = true;
this.CardMemory.Size = new System.Drawing.Size(224, 224);
this.CardMemory.Text = "Press the Read Card button to read all of the memory on the card...";
//
// ReadCard
//
this.ReadCard.Location = new System.Drawing.Point(8, 8);
this.ReadCard.Text = "Read card";
this.ReadCard.Click += new System.EventHandler(this.ReadCard_Click);
//
// FillMemory
//
this.FillMemory.Location = new System.Drawing.Point(88, 8);
this.FillMemory.Size = new System.Drawing.Size(112, 20);
this.FillMemory.Text = "Fill Memory with";
this.FillMemory.Click += new System.EventHandler(this.FillMemory_Click);
//
// MemoryCharacter
//
this.MemoryCharacter.Location = new System.Drawing.Point(208, 8);
this.MemoryCharacter.Size = new System.Drawing.Size(24, 20);
this.MemoryCharacter.Text = "A";
//
// ST14C02Reader
//
this.Controls.Add(this.MemoryCharacter);
this.Controls.Add(this.FillMemory);
this.Controls.Add(this.ReadCard);
this.Controls.Add(this.CardMemory);
this.Menu = this.mainMenu1;
this.Text = "ST14C02";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new ST14C02Reader());
}
private void onSmartCardInserted(object sender, System.EventArgs e)
{
CardMemory.Text += "Smartcard inserted \r\n";
}
private void onSmartCardRemoved(object sender, System.EventArgs e)
{
CardMemory.Text += "Smartcard removed \r\n";
}
private void onGrabbaReconnectedEvent(object sender, System.EventArgs e)
{
CardMemory.Text += "Grabba Reconnected!\r\n";
}
private void onGrabbaDisconnectedEvent(object sender, System.EventArgs e)
{
CardMemory.Text += "Grabba Disconnected!\r\n";
}
private void ExitMenuItem_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private bool CheckCardIsInserted()
{
uint rc;
Grabba.SmartCardSession scSession = new Grabba.SmartCardSession();
scSession.cardType = Grabba.SmartCardType.SC_ST14C02;
rc = myGrabba.SmartCardStartSession(ref scSession);
while (rc == myGrabba.grabbaSmartCardErrNoCardInserted)
{
if(MessageBox.Show("Please insert card now.", "MESSAGE",
MessageBoxButtons.OKCancel,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
return false;
rc = myGrabba.SmartCardStartSession(ref scSession);
}
if(rc != 0)
{
MessageBox.Show("Unable to access card. Error " + rc + ".", "ERROR");
return false;
}
return true;
}
private void DisplayCardContents()
{
int i;
uint rc;
if(CheckCardIsInserted())
{
byte[] data = new byte[0xFF];
string szString = "";
rc = myGrabba.St14c02ReadData((byte)0, (byte)0xff, ref data[0]);
if(rc != 0)
{
MessageBox.Show("Card access failed. Error code " + rc.ToString(), "ERROR");
return;
}
for(i = 0; i < 0xFF; i ++)
szString += ((char)data[i]).ToString();
CardMemory.Text = szString;
}
}
private void ReadCard_Click(object sender, System.EventArgs e)
{
DisplayCardContents();
}
private void FillMemory_Click(object sender, System.EventArgs e)
{
byte[] data = new byte[0xFF];
int i;
uint rc;
if(CheckCardIsInserted())
{
// Warn the user about overwriting the card memory
if(MessageBox.Show("Are you sure that you want to fill the memory with the data? This will overwrite all memory on the card.", "WARNING",
MessageBoxButtons.OKCancel,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
return;
for(i = 0; i < 0xFF; i ++)
data[i] = (byte)MemoryCharacter.Text.ToCharArray()[0];
// Write the String to the card
rc = myGrabba.St14c02WriteData((byte)0, (byte)0xFF, ref data[0]);
if(rc != 0)
{
MessageBox.Show("Card access failed. Error code " + rc.ToString(), "ERROR");
return;
}
DisplayCardContents();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -