⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sle4428demo.cs

📁 SDKs-And-Demos-CS.NET-2.1.1b10 Graba的sdk
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

using GrabbaDotNet;

namespace SLE4428_Demo_.NET
{
	/// <summary>
	/// Summary description for SLE4428DemoForm.
	/// </summary>
	public class SLE4428DemoForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button UpdateName;
		private System.Windows.Forms.Button UpdateIDNumber;
		private System.Windows.Forms.TextBox IDNumber;
		private System.Windows.Forms.TextBox Name;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem MainMenu;
		private System.Windows.Forms.MenuItem ExitMenuItem;
		private System.Windows.Forms.Button RefreshButton;
		private System.Windows.Forms.Label label4;

		
		private const int ID_SIZE =	32;
		private const int NAME_SIZE = 32;
		private const int ID_ADDRESS = 0x60;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label ATR;
		private const int NAME_ADDRESS = ID_ADDRESS + ID_SIZE;
		
        Grabba myGrabba;
	
		public SLE4428DemoForm()
		{
			InitializeComponent();

            myGrabba = new Grabba();

			// Open the Grabba
            if (myGrabba.Open() != myGrabba.errNone)
			{
				// 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.UpdateName = new System.Windows.Forms.Button();
			this.UpdateIDNumber = new System.Windows.Forms.Button();
			this.IDNumber = new System.Windows.Forms.TextBox();
			this.Name = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.MainMenu = new System.Windows.Forms.MenuItem();
			this.ExitMenuItem = new System.Windows.Forms.MenuItem();
			this.RefreshButton = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.ATR = new System.Windows.Forms.Label();
			// 
			// UpdateName
			// 
			this.UpdateName.Location = new System.Drawing.Point(88, 200);
			this.UpdateName.Size = new System.Drawing.Size(96, 20);
			this.UpdateName.Text = "Update Name";
			this.UpdateName.Click += new System.EventHandler(this.UpdateName_Click);
			// 
			// UpdateIDNumber
			// 
			this.UpdateIDNumber.Location = new System.Drawing.Point(88, 136);
			this.UpdateIDNumber.Size = new System.Drawing.Size(116, 20);
			this.UpdateIDNumber.Text = "Update ID Number";
			this.UpdateIDNumber.Click += new System.EventHandler(this.UpdateIDNumber_Click);
			// 
			// IDNumber
			// 
			this.IDNumber.Location = new System.Drawing.Point(88, 112);
			this.IDNumber.Size = new System.Drawing.Size(144, 20);
			this.IDNumber.Text = "IDNumber";
			// 
			// Name
			// 
			this.Name.Location = new System.Drawing.Point(88, 176);
			this.Name.Size = new System.Drawing.Size(144, 20);
			this.Name.Text = "Name";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 112);
			this.label3.Size = new System.Drawing.Size(64, 30);
			this.label3.Text = "ID Number:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(24, 176);
			this.label4.Size = new System.Drawing.Size(56, 20);
			this.label4.Text = "Name:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// 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);
			// 
			// Refresh
			// 
			this.RefreshButton.Location = new System.Drawing.Point(88, 240);
			this.RefreshButton.Size = new System.Drawing.Size(96, 20);
			this.RefreshButton.Text = "Refresh";
			this.RefreshButton.Click += new System.EventHandler(this.RefreshButton_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Size = new System.Drawing.Size(224, 70);
			this.label1.Text = "Enter a card, and type in the ID Number and the Name and press the update button " +
				"to write the data to the card. Press the Refresh button to read from the card.";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 83);
			this.label2.Size = new System.Drawing.Size(64, 20);
			this.label2.Text = "ATR:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// ATR
			// 
			this.ATR.Location = new System.Drawing.Point(88, 80);
			this.ATR.Size = new System.Drawing.Size(144, 20);
			// 
			// SLE4428DemoForm
			// 
			this.Controls.Add(this.ATR);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.RefreshButton);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.Name);
			this.Controls.Add(this.IDNumber);
			this.Controls.Add(this.UpdateIDNumber);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.UpdateName);
			this.Menu = this.mainMenu1;
			this.Text = "SLE4428 Demo";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Application.Run(new SLE4428DemoForm());
		}

        private void onSmartCardInserted(object sender, System.EventArgs e)
        {
            Name.Text += "Smartcard inserted \r\n";
        }
        private void onSmartCardRemoved(object sender, System.EventArgs e)
        {
            Name.Text += "Smartcard removed \r\n";
        }

        private void onGrabbaReconnectedEvent(object sender, System.EventArgs e)
        {
            Name.Text += "Grabba Reconnected!\r\n";
        }

        private void onGrabbaDisconnectedEvent(object sender, System.EventArgs e)
        {
            Name.Text += "Grabba Disconnected!\r\n";
        }

        string ReadStringFromCard(byte nAddress, byte nSizeOfData)
		{
			byte[] data = new byte[256];
			byte[] protectionBytes = new byte[4];
			uint i, rc;
			string szString = "";

            rc = myGrabba.Sle4428ReadData(nAddress, nSizeOfData, ref data[0]);
			if(rc != 0)
			{
				MessageBox.Show("Card access failed. Error code " + rc.ToString(), "ERROR");
				return "";
			}
			for(i = 0; i < nSizeOfData; i ++)
				szString += ((char)data[i]).ToString();

			return szString;
		}

		int WriteStringToCard(byte nAddress, byte nSizeOfData, string szString)
		{
			byte[] data = new byte[265];
			uint i, rc;

			for(i = 0; i < szString.Length; i ++)
				data[i] = (byte)(szString.ToCharArray())[i];
			data[i] = 0;

			// Write the String to the card
            rc = myGrabba.Sle4428WriteData(nAddress, nSizeOfData, ref data[0]);
			if(rc != 0)
			{
				MessageBox.Show("Card access failed. Error code " + rc.ToString(), "ERROR");
				return 0;
			}			
			MessageBox.Show("Memory updated successfully.", "MESSAGE");
			return 1;
		}

		private void ExitMenuItem_Click(object sender, System.EventArgs e)
		{
			Application.Exit();			
		}

		private void RefreshButton_Click(object sender, System.EventArgs e)
		{
			RefreshDisplay();
		}

		private void RefreshDisplay()
		{			
			uint i, rc;
            byte pbErrorCount = new byte();

            Grabba.SmartCardSession scSession = new Grabba.SmartCardSession();
            scSession.cardType = Grabba.SmartCardType.SC_SLE4432;

            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;

                rc = myGrabba.SmartCardStartSession(ref scSession);
			}
            if (rc != myGrabba.errNone)
			{	
				MessageBox.Show("Unable to access card. Error " + rc + ".", "ERROR");
				return;
			}
			ATR.Text = "";
            for (i = 0; i < scSession.ATRLen; i++)
                ATR.Text += scSession.ATR[i].ToString() + " ";

			// Perform PSC Verification
			if(myGrabba.Sle4428CheckCode(0xFF, 
									0xFF, 
                                    ref pbErrorCount) == myGrabba.grabbaSmartCardErrIncorrectPassword)
			{
				MessageBox.Show("Incorrect Pin used. You have " + pbErrorCount.ToString() + " attempts to attempt to access this card.", "ERROR");
			}
			
			// Read the name and id from the card
			Name.Text = ReadStringFromCard(NAME_ADDRESS, NAME_SIZE);
			IDNumber.Text = ReadStringFromCard(ID_ADDRESS, ID_SIZE);
		}

		private void UpdateName_Click(object sender, System.EventArgs e)
		{
			WriteStringToCard(NAME_ADDRESS, NAME_SIZE, Name.Text);
		}

		private void UpdateIDNumber_Click(object sender, System.EventArgs e)
		{
			WriteStringToCard(ID_ADDRESS, ID_SIZE, IDNumber.Text);
		}


	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -