小程序UI界面:
UI界面底代码:
namespace WindowsFormsApplication1 { partial class Form1 { /// < summary > /// 必需的设计器变量。 /// < /summary > private System.ComponentModel.IContainer components = null; /// < summary > /// 清理所有正在使用的资源。 /// < /summary > /// < param name="disposing" >如果应释放托管资源,为 true;否则为 false。< /param > protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// < summary > /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// < /summary > private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); this.button8 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button9 = new System.Windows.Forms.Button(); this.textBox3 = new System.Windows.Forms.TextBox(); this.button11 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.textBox4 = new System.Windows.Forms.TextBox(); this.button12 = new System.Windows.Forms.Button(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.timer2 = new System.Windows.Forms.Timer(this.components); this.button10 = new System.Windows.Forms.Button(); this.button13 = new System.Windows.Forms.Button(); this.button14 = new System.Windows.Forms.Button(); this.button15 = new System.Windows.Forms.Button(); this.button16 = new System.Windows.Forms.Button(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox5 = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.textBox7 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.button17 = new System.Windows.Forms.Button(); this.textBox6 = new System.Windows.Forms.TextBox(); this.button18 = new System.Windows.Forms.Button(); this.button19 = new System.Windows.Forms.Button(); this.textBox8 = new System.Windows.Forms.TextBox(); this.button20 = new System.Windows.Forms.Button(); this.textBox9 = new System.Windows.Forms.TextBox(); this.timer3 = new System.Windows.Forms.Timer(this.components); this.timer4 = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(40, 45); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(105, 30); this.button1.TabIndex = 0; this.button1.Text = "轻松读卡"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(40, 81); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(105, 30); this.button2.TabIndex = 1; this.button2.Text = "轻松写卡"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(40, 350); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(105, 30); this.button3.TabIndex = 2; this.button3.Text = "改单区密码"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.ForeColor = System.Drawing.Color.Red; this.button4.Location = new System.Drawing.Point(40, 7); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(105, 30); this.button4.TabIndex = 3; this.button4.Text = "让设备发出声响"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // button5 // this.button5.ForeColor = System.Drawing.Color.Blue; this.button5.Location = new System.Drawing.Point(40, 120); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(105, 30); this.button5.TabIndex = 4; this.button5.Text = "写汉字至卡中"; this.button5.UseVisualStyleBackColor = true; this.button5.Click += new System.EventHandler(this.button5_Click); // // button6 // this.button6.ForeColor = System.Drawing.Color.Blue; this.button6.Location = new System.Drawing.Point(151, 120); this.button6.Name = "button6"; this.button6.Size = new System.Drawing.Size(105, 30); this.button6.TabIndex = 5; this.button6.Text = "从卡中读出汉字"; this.button6.UseVisualStyleBackColor = true; this.button6.Click += new System.EventHandler(this.button6_Click); // // button7 // this.button7.Location = new System.Drawing.Point(40, 189); this.button7.Name = "button7"; this.button7.Size = new System.Drawing.Size(105, 30); this.button7.TabIndex = 6; this.button7.Text = "每十秒读一次"; this.button7.UseVisualStyleBackColor = true; this.button7.Click += new System.EventHandler(this.button7_Click); // // button8 // this.button8.Location = new System.Drawing.Point(40, 222); this.button8.Name = "button8"; this.button8.Size = new System.Drawing.Size(429, 30); this.button8.TabIndex = 7; this.button8.Text = "读出设备全球唯一的设备编号,作为加密狗用"; this.button8.UseVisualStyleBackColor = true; this.button8.Click += new System.EventHandler(this.button8_Click); // // textBox1 // this.textBox1.ForeColor = System.Drawing.Color.Blue; this.textBox1.Location = new System.Drawing.Point(40, 156); this.textBox1.Name = "textBox1"; this.textBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes; this.textBox1.Size = new System.Drawing.Size(438, 21); this.textBox1.TabIndex = 8; this.textBox1.Text = "王小明说:天下风云出我辈 一入江湖岁月催,Yes2018"; this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; // // button9 // this.button9.ForeColor = System.Drawing.Color.Blue; this.button9.Location = new System.Drawing.Point(279, 120); this.button9.Name = "button9"; this.button9.Size = new System.Drawing.Size(190, 30); this.button9.TabIndex = 10; this.button9.Text = "发送字符至带显示的读卡器"; this.button9.UseVisualStyleBackColor = true; this.button9.Click += new System.EventHandler(this.button9_Click); // // textBox3 // this.textBox3.ForeColor = System.Drawing.Color.Red; this.textBox3.Location = new System.Drawing.Point(196, 313); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(116, 21); this.textBox3.TabIndex = 13; // // button11 // this.button11.ForeColor = System.Drawing.Color.Red; this.button11.Location = new System.Drawing.Point(40, 307); this.button11.Name = "button11"; this.button11.Size = new System.Drawing.Size(105, 32); this.button11.TabIndex = 11; this.button11.Text = "只读卡号"; this.button11.UseVisualStyleBackColor = true; this.button11.Click += new System.EventHandler(this.button11_Click); // // label1 // this.label1.AutoSize = true; this.label1.ForeColor = System.Drawing.Color.Red; this.label1.Location = new System.Drawing.Point(149, 316); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 14; this.label1.Text = "卡号:"; // // textBox4 // this.textBox4.BackColor = System.Drawing.Color.White; this.textBox4.Location = new System.Drawing.Point(151, 195); this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(87, 21); this.textBox4.TabIndex = 18; // // button12 // this.button12.ForeColor = System.Drawing.Color.Red; this.button12.Location = new System.Drawing.Point(364, 302); this.button12.Name = "button12"; this.button12.Size = new System.Drawing.Size(105, 32); this.button12.TabIndex = 19; this.button12.Text = "轮询读IC卡号"; this.button12.UseVisualStyleBackColor = true; this.button12.Click += new System.EventHandler(this.button12_Click); // // timer1 // this.timer1.Interval = 20; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // timer2 // this.timer2.Interval = 6000; this.timer2.Tick += new System.EventHandler(this.timer2_Tick); // // button10 // this.button10.Location = new System.Drawing.Point(151, 350); this.button10.Name = "button10"; this.button10.Size = new System.Drawing.Size(318, 30); this.button10.TabIndex = 20; this.button10.Text = "修改A、B密码及控制字"; this.button10.UseVisualStyleBackColor = true; this.button10.Click += new System.EventHandler(this.button10_Click_1); // // button13 // this.button13.ForeColor = System.Drawing.Color.Blue; this.button13.Location = new System.Drawing.Point(40, 392); this.button13.Name = "button13"; this.button13.Size = new System.Drawing.Size(164, 30); this.button13.TabIndex = 21; this.button13.Text = "写设备存储区1 4个字节"; this.button13.UseVisualStyleBackColor = true; this.button13.Click += new System.EventHandler(this.button13_Click); // // button14 // this.button14.ForeColor = System.Drawing.Color.Blue; this.button14.Location = new System.Drawing.Point(364, 392); this.button14.Name = "button14"; this.button14.Size = new System.Drawing.Size(105, 30); this.button14.TabIndex = 22; this.button14.Text = "读设备存储区1"; this.button14.UseVisualStyleBackColor = true; this.button14.Click += new System.EventHandler(this.button14_Click); // // button15 // this.button15.ForeColor = System.Drawing.Color.Blue; this.button15.Location = new System.Drawing.Point(40, 428); this.button15.Name = "button15"; this.button15.Size = new System.Drawing.Size(164, 30); this.button15.TabIndex = 23; this.button15.Text = "写设备存储区2 8个字节"; this.button15.UseVisualStyleBackColor = true; this.button15.Click += new System.EventHandler(this.button15_Click); // // button16 // this.button16.ForeColor = System.Drawing.Color.Blue; this.button16.Location = new System.Drawing.Point(364, 428); this.button16.Name = "button16"; this.button16.Size = new System.Drawing.Size(105, 30); this.button16.TabIndex = 24; this.button16.Text = "读设备存储区2"; this.button16.UseVisualStyleBackColor = true; this.button16.Click += new System.EventHandler(this.button16_Click); // // textBox2 // this.textBox2.ForeColor = System.Drawing.Color.Blue; this.textBox2.Location = new System.Drawing.Point(222, 398); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(57, 21); this.textBox2.TabIndex = 25; this.textBox2.Text = "01020304"; // // textBox5 // this.textBox5.ForeColor = System.Drawing.Color.Blue; this.textBox5.Location = new System.Drawing.Point(222, 434); this.textBox5.Name = "textBox5"; this.textBox5.Size = new System.Drawing.Size(108, 21); this.textBox5.TabIndex = 26; this.textBox5.Text = "0102030405060708"; // // label2 // this.label2.AutoSize = true; this.label2.ForeColor = System.Drawing.Color.Blue; this.label2.Location = new System.Drawing.Point(292, 401); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 27; this.label2.Text = "16进制数字"; // // textBox7 // this.textBox7.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.textBox7.Location = new System.Drawing.Point(170, 59); this.textBox7.Multiline = true; this.textBox7.Name = "textBox7"; this.textBox7.Size = new System.Drawing.Size(230, 52); this.textBox7.TabIndex = 29; this.textBox7.Text = "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F00010203040506070" + "8090A0B0C0D0E0F"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(168, 44); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(65, 12); this.label3.TabIndex = 30; this.label3.Text = "16进制数字"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(406, 62); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(47, 12); this.label4.TabIndex = 31; this.label4.Text = "第 0 块"; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(406, 77); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(47, 12); this.label5.TabIndex = 32; this.label5.Text = "第 1 块"; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(406, 93); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(47, 12); this.label6.TabIndex = 33; this.label6.Text = "第 2 块"; // // button17 // this.button17.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.button17.Location = new System.Drawing.Point(40, 267); this.button17.Name = "button17"; this.button17.Size = new System.Drawing.Size(105, 30); this.button17.TabIndex = 34; this.button17.Text = "读指定块数据"; this.button17.UseVisualStyleBackColor = true; this.button17.Click += new System.EventHandler(this.button17_Click); // // textBox6 // this.textBox6.ForeColor = System.Drawing.Color.Red; this.textBox6.Location = new System.Drawing.Point(151, 269); this.textBox6.Name = "textBox6"; this.textBox6.Size = new System.Drawing.Size(209, 21); this.textBox6.TabIndex = 35; // // button18 // this.button18.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.button18.Location = new System.Drawing.Point(364, 263); this.button18.Name = "button18"; this.button18.Size = new System.Drawing.Size(105, 30); this.button18.TabIndex = 36; this.button18.Text = "写指定块数据"; this.button18.UseVisualStyleBackColor = true; this.button18.Click += new System.EventHandler(this.button18_Click); // // button19 // this.button19.ForeColor = System.Drawing.Color.Red; this.button19.Location = new System.Drawing.Point(40, 476); this.button19.Name = "button19"; this.button19.Size = new System.Drawing.Size(164, 32); this.button19.TabIndex = 37; this.button19.Text = "开始侦听二代身份证UID"; this.button19.UseVisualStyleBackColor = true; this.button19.Click += new System.EventHandler(this.button19_Click); // // textBox8 // this.textBox8.ForeColor = System.Drawing.Color.Red; this.textBox8.Location = new System.Drawing.Point(210, 476); this.textBox8.Multiline = true; this.textBox8.Name = "textBox8"; this.textBox8.Size = new System.Drawing.Size(259, 32); this.textBox8.TabIndex = 38; // // button20 // this.button20.ForeColor = System.Drawing.Color.Red; this.button20.Location = new System.Drawing.Point(40, 524); this.button20.Name = "button20"; this.button20.Size = new System.Drawing.Size(164, 32); this.button20.TabIndex = 39; this.button20.Text = "开始侦听HID ICLASS CSN"; this.button20.UseVisualStyleBackColor = true; this.button20.Click += new System.EventHandler(this.button20_Click); // // textBox9 // this.textBox9.ForeColor = System.Drawing.Color.Red; this.textBox9.Location = new System.Drawing.Point(210, 525); this.textBox9.Multiline = true; this.textBox9.Name = "textBox9"; this.textBox9.Size = new System.Drawing.Size(259, 31); this.textBox9.TabIndex = 40; // // timer3 // this.timer3.Tick += new System.EventHandler(this.timer3_Tick); // // timer4 // this.timer4.Tick += new System.EventHandler(this.timer4_Tick); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(520, 577); this.Controls.Add(this.textBox9); this.Controls.Add(this.button20); this.Controls.Add(this.textBox8); this.Controls.Add(this.button19); this.Controls.Add(this.button18); this.Controls.Add(this.textBox6); this.Controls.Add(this.button17); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.textBox7); this.Controls.Add(this.label2); this.Controls.Add(this.textBox5); this.Controls.Add(this.textBox2); this.Controls.Add(this.button16); this.Controls.Add(this.button15); this.Controls.Add(this.button14); this.Controls.Add(this.button13); this.Controls.Add(this.button10); this.Controls.Add(this.button12); this.Controls.Add(this.textBox4); this.Controls.Add(this.label1); this.Controls.Add(this.textBox3); this.Controls.Add(this.button11); this.Controls.Add(this.button9); this.Controls.Add(this.textBox1); this.Controls.Add(this.button8); this.Controls.Add(this.button7); this.Controls.Add(this.button6); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "IC卡读写动态库OUR_MIFARE.dll使用示例"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button7; private System.Windows.Forms.Button button8; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button9; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Button button11; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.Button button12; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Timer timer2; private System.Windows.Forms.Button button10; private System.Windows.Forms.Button button13; private System.Windows.Forms.Button button14; private System.Windows.Forms.Button button15; private System.Windows.Forms.Button button16; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox5; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox7; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Button button17; private System.Windows.Forms.TextBox textBox6; private System.Windows.Forms.Button button18; private System.Windows.Forms.Button button19; private System.Windows.Forms.TextBox textBox8; private System.Windows.Forms.Button button20; private System.Windows.Forms.TextBox textBox9; private System.Windows.Forms.Timer timer3; private System.Windows.Forms.Timer timer4; } }
小程序代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; //using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; //调用动态库一定要加入这个引用 using System.Text;//一定要加入这个 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public const byte BLOCK0_EN = 0x01;//操作第0块 public const byte BLOCK1_EN = 0x02;//操作第1块 public const byte BLOCK2_EN = 0x04;//操作第2块 public const byte NEEDSERIAL = 0x08;//仅对指定序列号的卡操作 public const byte EXTERNKEY = 0x10; public const byte NEEDHALT = 0x20;//读卡或写卡后顺便休眠该卡,休眠后,卡必须拿离开感应区,再放回感应区,才能进行第二次操作。 //------------------------------------------------------------------------------------------------------------------------------------------------------ //外部函数声明:让设备发出声响 [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdbeep", CallingConvention = CallingConvention.StdCall)] static extern byte pcdbeep(UInt32 xms);//xms单位为毫秒 //------------------------------------------------------------------------------------------------------------------------------------------------------ //只读卡号 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccrequest", CallingConvention = CallingConvention.StdCall)] public static extern byte piccrequest(byte[] serial);//devicenumber用于返回编号 //------------------------------------------------------------------------------------------------------------------------------------------------------ //读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限 [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdgetdevicenumber", CallingConvention = CallingConvention.StdCall)] static extern byte pcdgetdevicenumber(byte[] devicenumber);//devicenumber用于返回编号 //------------------------------------------------------------------------------------------------------------------------------------------------------ //轻松读卡 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccreadex", CallingConvention = CallingConvention.StdCall)] static extern byte piccreadex(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] picckey, byte[] piccdata0_2); //参数:说明 //ctrlword:控制字 //serial:卡序列号数组,用于指定或返回卡序列号 //area:指定读卡区号 //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。 //picckey:指定卡密码,6个字节,卡出厂时的初始密码为6个0xff //piccdata0_2:用于返回卡该区第0块到第2块的数据,共48个字节. //------------------------------------------------------------------------------------------------------------------------------------------------------ //轻松写卡 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccwriteex", CallingConvention = CallingConvention.StdCall)] static extern byte piccwriteex(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] picckey, byte[] piccdata0_2); //参数:说明 //ctrlword:控制字 //serial:卡序列号数组,用于指定或返回卡序列号 //area:指定读卡区号 //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。 //picckey:指定卡密码,6个字节,卡出厂时的初始密码为6个0xff //piccdata0_2:用于返回卡该区第0块到第2块的数据,共48个字节. //------------------------------------------------------------------------------------------------------------------------------------------------------ //密码认证方式1,用外部密码认证,必须指定外部密码。本函数必须在piccrequest或piccrequestex函数执行之后运行,并且要紧接着调用,中途不能调用其他函数。 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccauthkey1", CallingConvention = CallingConvention.StdCall)] static extern byte piccauthkey1(byte[] serial, byte area, byte keyA1B0, byte[] picckey); //参数:说明 //serial:卡序列号数组,用于指定或返回卡序列号 //area:指定读卡区号 //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。 //picckey:指定卡密码,6个字节,卡出厂时的初始密码为6个0xff //------------------------------------------------------------------------------------------------------------------------------------------------------ //读出一块的数据,也就是16个字节。必须在执行piccrequest或 Piccrequestex函数,接着执行piccauthkey1或 piccauthkey2函数,然后执行piccread才能成功读出一块的数据。 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccread", CallingConvention = CallingConvention.StdCall)] static extern byte piccread(byte block, byte[] blockdata); //参数:说明 //block:指定读卡块号 //blockdata:用于返回卡该块数据,共16个字节. //------------------------------------------------------------------------------------------------------------------------------------------------------ //写一块的数据,也就是16个字节。必须在执行piccrequest或 Piccrequestex函数,接着执行piccauthkey1或 piccauthkey2函数,然后执行piccread才能成功读出一块的数据。 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccwrite", CallingConvention = CallingConvention.StdCall)] static extern byte piccwrite(byte block, byte[] blockdata); //参数:说明 //block:指定写卡块号 //blockdata:写块数据缓冲,共16个字节. //------------------------------------------------------------------------------------------------------------------------------------------------------ //修改卡单区的密码 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccchangesinglekey", CallingConvention = CallingConvention.StdCall)] static extern byte piccchangesinglekey(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] piccoldkey, byte[] piccnewkey); //参数:说明 //ctrlword:控制字 //serial:卡序列号数组,用于指定或返回卡序列号 //area:指定读卡区号 //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。 //piccoldkey://旧密码 //piccnewkey://新密码. //------------------------------------------------------------------------------------------------------------------------------------------------------ //修改卡A/B密码及控制字 [DllImport("OUR_MIFARE.dll", EntryPoint = "piccchangesinglekeyex", CallingConvention = CallingConvention.StdCall)] static extern byte piccchangesinglekeyex(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] piccoldkey, byte[] piccnewkey); //参数:说明 //ctrlword:控制字 //serial:卡序列号数组,用于指定或返回卡序列号 //area:指定读卡区号 //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。 //piccoldkey://旧密码 //piccnewkey://新密码. //------------------------------------------------------------------------------------------------------------------------------------------------------ //发送显示内容到读卡器 [DllImport("OUR_MIFARE.dll", EntryPoint = "lcddispfull", CallingConvention = CallingConvention.StdCall)] static extern byte lcddispfull(string lcdstr); //参数:说明 //lcdstr:显示内容 //------------------------------------------------------------------------------------------------------------------------------------------------------ //写设备存储区1 [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdsetcustomizedata1", CallingConvention = CallingConvention.StdCall)] public static extern byte pcdsetcustomizedata1(byte[] readerdata);//devicenumber用于传递写数据 //------------------------------------------------------------------------------------------------------------------------------------------------------ //读设备存储区1 [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdgetcustomizedata1", CallingConvention = CallingConvention.StdCall)] public static extern byte pcdgetcustomizedata1(byte[] readerdata);//devicenumber用于传递写数据 //------------------------------------------------------------------------------------------------------------------------------------------------------ //写设备存储区2 [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdsetcustomizedata2", CallingConvention = CallingConvention.StdCall)] public static extern byte pcdsetcustomizedata2(byte[] readerdata);//devicenumber用于传递写数据 //------------------------------------------------------------------------------------------------------------------------------------------------------ //读设备存储区2 [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdgetcustomizedata2", CallingConvention = CallingConvention.StdCall)] public static extern byte pcdgetcustomizedata2(byte[] readerdata, byte[] devno);//devicenumber用于传递写数据 //------------------------------------------------------------------------------------------------------------------------------------------------------ //读二代身份证UID [DllImport("OUR_MIFARE.dll", EntryPoint = "sfzrequest", CallingConvention = CallingConvention.StdCall)] public static extern byte sfzrequest(byte[] serial);//devicenumber用于返回UID号 //------------------------------------------------------------------------------------------------------------------------------------------------------ //读HID ICLASS CSN [DllImport("OUR_MIFARE.dll", EntryPoint = "iso15693iclassreadcsn", CallingConvention = CallingConvention.StdCall)] public static extern byte iso15693iclassreadcsn(byte[] serial);//devicenumber用于返回CSN号 public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e)//轻松读卡 { byte status;//存放返回值 byte myareano;//区号 byte authmode;//密码类型,用A密码或B密码 byte myctrlword;//控制字 byte[] mypicckey = new byte[6];//密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] mypiccdata = new byte[48]; //卡数据缓冲 //控制字指定,控制字的含义请查看本公司网站提供的动态库说明 myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //指定区号 myareano = 8;//指定为第8区 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定密码 mypicckey[0] = 0xff; mypicckey[1] = 0xff; mypicckey[2] = 0xff; mypicckey[3] = 0xff; mypicckey[4] = 0xff; mypicckey[5] = 0xff; status = piccreadex(myctrlword, mypiccserial, myareano, authmode, mypicckey, mypiccdata); //在下面设定断点,然后查看mypiccserial、mypiccdata, //调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata, //开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。 //处理返回函数 switch (status) { case 0: pcdbeep(20); string readinf = ""; for (int i = 0; i < 48;i++ ) { readinf = readinf + mypiccdata[i].ToString("X2"); } textBox7.Text = readinf; MessageBox.Show("操作成功,数据已显示在右框栏中!","示例程序"); break; case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } //返回解释 /* REQUEST 8//寻卡错误 READSERIAL 9//读序列吗错误 SELECTCARD 10//选卡错误 LOADKEY 11//装载密码错误 AUTHKEY 12//密码认证错误 READ 13//读卡错误 WRITE 14//写卡错误 NONEDLL 21//没有动态库 DRIVERORDLL 22//动态库或驱动程序异常 DRIVERNULL 23//驱动程序错误或尚未安装 TIMEOUT 24//操作超时,一般是动态库没有反映 TXSIZE 25//发送字数不够 TXCRC 26//发送的CRC错 RXSIZE 27//接收的字数不够 RXCRC 28//接收的CRC错 */ } private void button2_Click(object sender, EventArgs e)//轻松写卡 { byte i; byte status;//存放返回值 byte myareano;//区号 byte authmode;//密码类型,用A密码或B密码 byte myctrlword;//控制字 byte[] mypicckey = new byte[6];//密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] mypiccdata = new byte[48]; //卡数据缓冲 //控制字指定,控制字的含义请查看本公司网站提供的动态库说明 myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //指定区号 myareano = 8;//指定为第8区 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定密码 mypicckey[0] = 0xff; mypicckey[1] = 0xff; mypicckey[2] = 0xff; mypicckey[3] = 0xff; mypicckey[4] = 0xff; mypicckey[5] = 0xff; //指定卡数据 string writinf = textBox7.Text.Trim() + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; for (i = 0; i < 48; i++) { mypiccdata[i] = Convert.ToByte(Convert.ToInt32(writinf.Substring(i * 2, 2), 16)); } status = piccwriteex(myctrlword, mypiccserial, myareano, authmode, mypicckey, mypiccdata); //在下面设定断点,然后查看mypiccserial、mypiccdata, //调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata, //开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。 //处理返回函数 switch (status) { case 0: pcdbeep(50); MessageBox.Show("操作成功,右栏数据已写入卡中!", "示例提示"); break; case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } //返回解释 /* REQUEST 8//寻卡错误 READSERIAL 9//读序列吗错误 SELECTCARD 10//选卡错误 LOADKEY 11//装载密码错误 AUTHKEY 12//密码认证错误 READ 13//读卡错误 WRITE 14//写卡错误 NONEDLL 21//没有动态库 DRIVERORDLL 22//动态库或驱动程序异常 DRIVERNULL 23//驱动程序错误或尚未安装 TIMEOUT 24//操作超时,一般是动态库没有反映 TXSIZE 25//发送字数不够 TXCRC 26//发送的CRC错 RXSIZE 27//接收的字数不够 RXCRC 28//接收的CRC错 */ } private void button4_Click(object sender, EventArgs e) { pcdbeep(50); } private void button3_Click(object sender, EventArgs e) { byte status;//存放返回值 byte myareano;//区号 byte authmode;//密码类型,用A密码或B密码 byte myctrlword;//控制字 byte[] piccoldkey = new byte[6];//旧密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] piccnewkey = new byte[6]; //新密码. //控制字指定,控制字的含义请查看本公司网站提供的动态库说明 myctrlword = 0; //指定区号 myareano = 8;//指定为第8区 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定旧密码 piccoldkey[0] = 0xff; piccoldkey[1] = 0xff; piccoldkey[2] = 0xff; piccoldkey[3] = 0xff; piccoldkey[4] = 0xff; piccoldkey[5] = 0xff; //指定新密码,注意:指定新密码时一定要记住,否则有可能找不回密码,导致该卡报废。 piccnewkey[0] = 0xff; piccnewkey[1] = 0xff; piccnewkey[2] = 0xff; piccnewkey[3] = 0xff; piccnewkey[4] = 0xff; piccnewkey[5] = 0xff; status = piccchangesinglekey(myctrlword, mypiccserial, myareano, authmode, piccoldkey, piccnewkey); //在下面设定断点,然后查看mypiccserial、mypiccdata, //调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata, //开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。 //处理返回函数 switch (status) { case 0: MessageBox.Show("操作成功,密码已被修改!"); break; case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } //返回解释 /* REQUEST 8//寻卡错误 READSERIAL 9//读序列吗错误 SELECTCARD 10//选卡错误 LOADKEY 11//装载密码错误 AUTHKEY 12//密码认证错误 READ 13//读卡错误 WRITE 14//写卡错误 NONEDLL 21//没有动态库 DRIVERORDLL 22//动态库或驱动程序异常 DRIVERNULL 23//驱动程序错误或尚未安装 TIMEOUT 24//操作超时,一般是动态库没有反映 TXSIZE 25//发送字数不够 TXCRC 26//发送的CRC错 RXSIZE 27//接收的字数不够 RXCRC 28//接收的CRC错 */ } private void button8_Click(object sender, EventArgs e)//读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限 { byte[] devno = new byte[4]; if (pcdgetdevicenumber(devno) == 0) { pcdbeep(50); MessageBox.Show("设备的序列号为:"+System.Convert.ToString(devno[0]) + "-" + System.Convert.ToString(devno[1]) + "-" + System.Convert.ToString(devno[2]) + "-" + System.Convert.ToString(devno[3]),"示例程序"); //ShowMessage(IntToStr(devno[0]) + "-" + IntToStr(devno[1]) + "-" + IntToStr(devno[2]) + "-" + IntToStr(devno[3])); } } private void button9_Click(object sender, EventArgs e) { string strls; strls = textBox1.Text; lcddispfull(strls); } private void button7_Click(object sender, EventArgs e) { if (button7.Text == "每十秒读一次") { pcdbeep(5); byte[] devno = new byte[4]; if (pcdgetdevicenumber(devno) == 0) { textBox4.Text = System.Convert.ToString(devno[0]) + "-" + System.Convert.ToString(devno[1]) + "-" + System.Convert.ToString(devno[2]) + "-" + System.Convert.ToString(devno[3]); } else { textBox4.Text = ""; } button12.Text = "轮询读IC卡号"; timer1.Enabled = false; timer2.Enabled = true; button7.Text = "暂停"; } else { timer2.Enabled = false; button7.Text = "每十秒读一次"; } } private void button11_Click(object sender, EventArgs e) { string cardstr; byte status;//Store the return value byte[] mypiccserial = new byte[4];//card serial Int64 cardnumdec; status = piccrequest(mypiccserial); if (status == 0) { cardnumdec = mypiccserial[3]; cardnumdec = cardnumdec * 256; cardnumdec = cardnumdec + mypiccserial[2]; cardnumdec = cardnumdec * 256; cardnumdec = cardnumdec + mypiccserial[1]; cardnumdec = cardnumdec * 256; cardnumdec = cardnumdec + mypiccserial[0]; cardstr = "0000000000" + Convert.ToString(cardnumdec); textBox3.Text = cardstr.Substring(cardstr.Length-10) ; pcdbeep(38); } } private void button5_Click(object sender, EventArgs e) { string strls; byte i; byte status;//存放返回值 byte myareano;//区号 byte authmode;//密码类型,用A密码或B密码 byte myctrlword;//控制字 byte[] mypicckey = new byte[6];//密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] mypiccdata = new byte[48]; //卡数据缓冲 //控制字指定,控制字的含义请查看本公司网站提供的动态库说明 myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //指定区号 myareano = 8;//指定为第8区 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定密码 mypicckey[0] = 0xff; mypicckey[1] = 0xff; mypicckey[2] = 0xff; mypicckey[3] = 0xff; mypicckey[4] = 0xff; mypicckey[5] = 0xff; strls = textBox1.Text + " "; byte[] strlsansi = System.Text.Encoding.GetEncoding(936).GetBytes(strls);//转换为Ansi for (i = 0; i < 48; i++) { mypiccdata[i] = strlsansi[i]; } status = piccwriteex(myctrlword, mypiccserial, myareano, authmode, mypicckey, mypiccdata); //在下面设定断点,然后查看mypiccserial、mypiccdata, //调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata, //开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。 //处理返回函数 switch (status) { case 0: pcdbeep(50); MessageBox.Show("操作成功,下栏文字信息已写入卡中!","示例提示"); break; case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } } private void button6_Click(object sender, EventArgs e) { byte status;//存放返回值 byte myareano;//区号 byte authmode;//密码类型,用A密码或B密码 byte myctrlword;//控制字 byte[] mypicckey = new byte[6];//密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] mypiccdata = new byte[48]; //卡数据缓冲 //控制字指定,控制字的含义请查看本公司网站提供的动态库说明 myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //指定区号 myareano = 8;//指定为第8区 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定密码 mypicckey[0] = 0xff; mypicckey[1] = 0xff; mypicckey[2] = 0xff; mypicckey[3] = 0xff; mypicckey[4] = 0xff; mypicckey[5] = 0xff; status = piccreadex(myctrlword, mypiccserial, myareano, authmode, mypicckey, mypiccdata); //在下面设定断点,然后查看mypiccserial、mypiccdata, //调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata, //开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。 //处理返回函数 switch (status) { case 0: string getdata = Encoding.Default.GetString(mypiccdata); MessageBox.Show("读卡操作成功,卡内信息:"+getdata, "示例程序"); break; case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } } private void timer2_Tick(object sender, EventArgs e) { byte[] devno = new byte[4]; if (pcdgetdevicenumber(devno) == 0) { pcdbeep(20); textBox4.Text = System.Convert.ToString(devno[0]) + "-" + System.Convert.ToString(devno[1]) + "-" + System.Convert.ToString(devno[2]) + "-" + System.Convert.ToString(devno[3]); } else { textBox4.Text = ""; } } private void button10_Click(object sender, EventArgs e) { } private void button12_Click(object sender, EventArgs e) { if (button12.Text == "轮询读IC卡号") { button7.Text = "每十秒读一次"; timer2.Enabled = false; timer1.Enabled = true; button12.Text = "暂停"; } else { timer1.Enabled = false; button12.Text = "轮询读IC卡号"; } } private void timer1_Tick(object sender, EventArgs e) { byte status;//Store the return value byte[] mypiccserial = new byte[4];//card serial Int64 cardnumdec; status = piccrequest(mypiccserial); if (status == 0) { cardnumdec = mypiccserial[3]; cardnumdec = cardnumdec * 256; cardnumdec = cardnumdec + mypiccserial[2]; cardnumdec = cardnumdec * 256; cardnumdec = cardnumdec + mypiccserial[1]; cardnumdec = cardnumdec * 256; cardnumdec = cardnumdec + mypiccserial[0]; string cardstr = "0000000000" + Convert.ToString(cardnumdec); textBox3.Text = cardstr.Substring(cardstr.Length - 10); pcdbeep(38); } } private void Form1_Load(object sender, EventArgs e) { } private void button10_Click_1(object sender, EventArgs e) { byte status;//存放返回值 byte myareano;//区号 byte authmode;//密码类型,用A密码或B密码 byte myctrlword;//控制字 byte[] piccoldkey = new byte[6];//旧密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] piccnewkey = new byte[17]; //新密码. //控制字指定,控制字的含义请查看本公司网站提供的动态库说明 myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //指定区号 myareano = 8;//指定为第8区 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定旧密码 piccoldkey[0] = 0xff; piccoldkey[1] = 0xff; piccoldkey[2] = 0xff; piccoldkey[3] = 0xff; piccoldkey[4] = 0xff; piccoldkey[5] = 0xff; //指定新密码,注意:指定新密码时一定要记住,否则有可能找不回密码,导致该卡报废。 piccnewkey[0] = 0xff; //A密码 piccnewkey[1] = 0xff; piccnewkey[2] = 0xff; piccnewkey[3] = 0xff; piccnewkey[4] = 0xff; piccnewkey[5] = 0xff; piccnewkey[6] = 0xff; //控制字,除非您确定了解控制字的含意,否则不要更改,错改卡将报废 piccnewkey[7] = 0x07; piccnewkey[8] = 0x80; piccnewkey[9] = 0x69; piccnewkey[10] = 0xff; //B密码 piccnewkey[11] = 0xff; piccnewkey[12] = 0xff; piccnewkey[13] = 0xff; piccnewkey[14] = 0xff; piccnewkey[15] = 0xff; piccnewkey[16] = 0x03; //'3是表示同时更改A、B、 密码权限访问字,为2表示密码权限访问字不更改,只改A、B密码,为0表示只改A密码 status = piccchangesinglekeyex(myctrlword, mypiccserial, myareano, authmode, piccoldkey, piccnewkey); //在下面设定断点,然后查看mypiccserial、mypiccdata, //调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata, //开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。 //处理返回函数 switch (status) { case 0: pcdbeep(50); MessageBox.Show("操作成功,密码已被修改!", "示例提示"); break; case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } } private void button14_Click(object sender, EventArgs e) { byte status;//Store the return value byte[] memo1 = new byte[4];//machinmemo1 string readinf; status = pcdgetcustomizedata1(memo1); if (status == 0) { readinf = ""; for (int i = 0; i < 4;i++ ) { readinf = readinf + memo1[i].ToString("X2"); } textBox2.Text = readinf; pcdbeep(38); } } private void button13_Click(object sender, EventArgs e) { byte[] memo1 = new byte[4]; string writinf; writinf = textBox2.Text.Trim()+"00000000"; for (int i=0;i< 4;i++) { memo1[i] = Convert.ToByte(Convert.ToInt32(writinf.Substring(i * 2, 2),16)); } byte status = pcdsetcustomizedata1(memo1); switch ( status) { case 0: pcdbeep(50); MessageBox.Show("操作成功,信息已写入设备!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } } private void button15_Click(object sender, EventArgs e) { byte[] memo1 = new byte[8]; string writinf; writinf = textBox5.Text.Trim() + "0000000000000000"; for (int i = 0; i < 8; i++) { memo1[i] = Convert.ToByte(Convert.ToInt32(writinf.Substring(i * 2, 2), 16)); } byte status = pcdsetcustomizedata2(memo1); switch (status) { case 0: pcdbeep(50); MessageBox.Show("操作成功,信息已写入设备!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } } private void button16_Click(object sender, EventArgs e) { byte status;//Store the return value byte[] memo1 = new byte[8];//machinmemo1 byte[] devno = new byte[4]; string readinf; status = pcdgetcustomizedata2(memo1,devno); if (status == 0) { readinf = ""; for (int i = 0; i < 8; i++) { readinf = readinf + memo1[i].ToString("X2"); } textBox5.Text = readinf; pcdbeep(38); MessageBox.Show("设备的序列号为:" + System.Convert.ToString(devno[0]) + "-" + System.Convert.ToString(devno[1]) + "-" + System.Convert.ToString(devno[2]) + "-" + System.Convert.ToString(devno[3]), "示例程序"); } } private void button17_Click(object sender, EventArgs e) { byte status;//存放返回值 byte myareano;//区号 byte myblock; //块号 byte authmode;//密码类型,用A密码或B密码 byte[] mypicckey = new byte[6];//密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] mypiccdata = new byte[48]; //卡数据缓冲 byte[] myblockdata=new byte[16]; //块数据缓冲 //指定区号 myareano = 8;//指定为第8区 myblock =(byte)( myareano * 4 + 0); //块号为区号*4+0、1、2、3,其中第3块为密码控制块 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定密码 mypicckey[0] = 0xff; mypicckey[1] = 0xff; mypicckey[2] = 0xff; mypicckey[3] = 0xff; mypicckey[4] = 0xff; mypicckey[5] = 0xff; status = piccrequest(mypiccserial); if(status==0) { status = piccauthkey1(mypiccserial, myareano, authmode, mypicckey); if (status == 0) { status = piccread(myblock, myblockdata); if (status == 0) { string readinf = ""; for (int i = 0; i < 16; i++) { readinf = readinf + myblockdata[i].ToString("X2"); } textBox6.Text = readinf; MessageBox.Show("操作成功,数据已显示在右框栏中!", "示例程序"); return; } } } switch (status) { case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } } private void button18_Click(object sender, EventArgs e) { byte status;//存放返回值 byte myareano;//区号 byte myblock; //块号 byte authmode;//密码类型,用A密码或B密码 byte[] mypicckey = new byte[6];//密码 byte[] mypiccserial = new byte[4];//卡序列号 byte[] mypiccdata = new byte[48]; //卡数据缓冲 byte[] myblockdata = new byte[16]; //块数据缓冲 int i; //指定区号 myareano = 8;//指定为第8区 myblock = (byte)(myareano * 4 + 0); //块号为区号*4+0、1、2、3,其中第3块为密码控制块 //批定密码模式 authmode = 1;//大于0表示用A密码认证,推荐用A密码认证 //指定密码 mypicckey[0] = 0xff; mypicckey[1] = 0xff; mypicckey[2] = 0xff; mypicckey[3] = 0xff; mypicckey[4] = 0xff; mypicckey[5] = 0xff; i = myblock % 4; if (i== 3) { if (MessageBox.Show(" 你确定要修改密码及控制块数据吗?如写入错误的信息此扇区将报废。", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) != DialogResult.Yes) { return; } } //指定卡数据 string writinf = textBox6.Text.Trim() + "00000000000000000000000000000000"; for (i = 0; i < 16; i++) { myblockdata[i] = Convert.ToByte(Convert.ToInt32(writinf.Substring(i * 2, 2), 16)); } status = piccrequest(mypiccserial); if (status == 0) { status = piccauthkey1(mypiccserial, myareano, authmode, mypicckey); if (status == 0) { status = piccwrite(myblock, myblockdata); } } switch (status) { case 0: pcdbeep(50); MessageBox.Show("操作成功!", "示例程序"); break; case 8: MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示"); break; case 9: MessageBox.Show("读序列码错误!", "示例提示"); break; case 10: MessageBox.Show("选卡错误!", "示例提示"); break; case 11: MessageBox.Show("装载密码错误!", "示例提示"); break; case 12: MessageBox.Show("卡密码认证错误!", "示例提示"); break; case 13: MessageBox.Show("读卡错误!", "示例提示"); break; case 14: MessageBox.Show("写卡错误!", "示例提示"); break; case 21: MessageBox.Show("没有动态库!", "示例提示"); break; case 22: MessageBox.Show("动态库或驱动程序异常!", "示例提示"); break; case 23: MessageBox.Show("驱动程序错误或尚未安装!", "示例提示"); break; case 24: MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示"); break; case 25: MessageBox.Show("发送字数不够!", "示例提示"); break; case 26: MessageBox.Show("发送的CRC错!", "示例提示"); break; case 27: MessageBox.Show("接收的字数不够!", "示例提示"); break; case 28: MessageBox.Show("接收的CRC错!", "示例提示"); break; default: MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status, "示例提示"); break; } } private void button19_Click(object sender, EventArgs e) { if (button19.Text == "开始侦听二代身份证UID") { timer3.Enabled = true; button19.Text = "暂停"; } else { timer3.Enabled = false; button19.Text = "开始侦听二代身份证UID"; textBox8.Text = ""; } } private void timer3_Tick(object sender, EventArgs e) { byte[] uidno = new byte[8]; byte status=sfzrequest(uidno); switch (status) { case 0: pcdbeep(20); Int64 cardnumdec = uidno[3] * 256 * 256 * 256 + uidno[2] * 256 * 256 + uidno[1] * 256 + uidno[0] ; textBox8.Text = "读取16进制身份证UID:" + uidno[7].ToString("X2") + "-" + uidno[6].ToString("X2") + "-" + uidno[5].ToString("X2") + "-" + uidno[4].ToString("X2") + "-" + uidno[3].ToString("X2") + "-" + uidno[2].ToString("X2") + "-" + uidno[1].ToString("X2") + "-" + uidno[0].ToString("X2") + ",转8H10D卡号:" + cardnumdec.ToString("D10"); break; case 8: textBox8.Text = "请刷二代身份证..."; break ; case 23: textBox8.Text = "读写器未定连接或目录内的库文件错误!"; break ; } } private void timer4_Tick(object sender, EventArgs e) { byte[] uidno = new byte[8]; byte status = iso15693iclassreadcsn(uidno); switch (status) { case 0: pcdbeep(20); Int64 cardnumdec = uidno[3] * 256 * 256 * 256 + uidno[2] * 256 * 256 + uidno[1] * 256 + uidno[0]; textBox9.Text = "读取16进制卡号:" + uidno[0].ToString("X2") + "-" + uidno[1].ToString("X2") + "-" + uidno[2].ToString("X2") + "-" + uidno[3].ToString("X2") + ",转8H10D卡号:" + cardnumdec.ToString("D10"); break; case 8: textBox9.Text = "请刷HID ICLass ..."; break; case 23: textBox9.Text = "读写器未定连接或目录内的库文件错误!"; break; } } private void button20_Click(object sender, EventArgs e) { if (button20.Text == "开始侦听HID ICLASS CSN") { timer4.Enabled = true; button20.Text = "暂停"; } else { timer4.Enabled = false; button20.Text = "开始侦听HID ICLASS CSN"; textBox9.Text = ""; } } } } 审核编辑 黄宇
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
IC
+关注
关注
36文章
5957浏览量
175734 -
usb
+关注
关注
60文章
7951浏览量
264914 -
小程序
+关注
关注
1文章
238浏览量
12154
发布评论请先 登录
相关推荐
ACR39U-H1 应用程序编程接口 ISO 7816卡读写器
`ACR39U-H1应用程序编程接口ISO 7816卡读写器 ACR39U-H1安装简单、使用方便,能够轻松集成进计算机环境。它符合PC/SC和CCID标准,驱动程序与Windows®
发表于 08-01 16:15
ic卡读写器怎么用
IC卡读写器有人也称读卡器。IC 卡具有存储容量大、保密性好、体积小、便于携带等特点, 可广泛用于金融、交通、税务、社会保险、工商管理、医疗
评论