代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;  //调用动态库一定要加入这个引用
namespace iccardreader
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  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.Label label1;
  /// <summary>
  /// 必需的设计器变量。
  //常量定义
  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;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button6;//用函数参数中的密码组进行读写卡时的认证,而不是用藏于读写器的芯片内的密码来认证。
  public const byte NEEDHALT = 0x20;//读卡或写卡后顺便休眠该卡,休眠后,卡必须拿离开感应区,再放回感应区,才能进行第二次操作。

//------------------------------------------------------------------------------------------------------------------------------------------------------
  //外部函数声明:让设备发出声响
  [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdbeep")]
  static extern byte pcdbeep(ulong xms);//xms单位为毫秒

//------------------------------------------------------------------------------------------------------------------------------------------------------   
  //读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限
  [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdgetdevicenumber")]
  static extern byte pcdgetdevicenumber(byte[] devicenumber);//devicenumber用于返回编号

//------------------------------------------------------------------------------------------------------------------------------------------------------
  //轻松读卡
  [DllImport("OUR_MIFARE.dll", EntryPoint = "piccreadex")]
  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")]
  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个字节.

//------------------------------------------------------------------------------------------------------------------------------------------------------
  //修改卡单区的密码
  [DllImport("OUR_MIFARE.dll", EntryPoint = "piccchangesinglekey")]
  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://新密码.

//------------------------------------------------------------------------------------------------------------------------------------------------------
  //发送显示内容到读卡器
  [DllImport("OUR_MIFARE.dll", EntryPoint = "lcddispfull")]
  static extern byte lcddispfull(string lcdstr);
  //参数:说明
  //lcdstr:显示内容
 
  /// </summary>
  private System.ComponentModel.Container components = null;

public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

//
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

/// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

#region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   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.label1 = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button6 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(8, 16);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(104, 32);
   this.button1.TabIndex = 0;
   this.button1.Text = "轻松读卡";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(120, 16);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(104, 32);
   this.button2.TabIndex = 1;
   this.button2.Text = "轻松写卡";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(344, 16);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(104, 32);
   this.button3.TabIndex = 3;
   this.button3.Text = "让设备发出声响";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(232, 16);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(104, 32);
   this.button4.TabIndex = 2;
   this.button4.Text = "改单区密码";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(8, 64);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(440, 32);
   this.button5.TabIndex = 4;
   this.button5.Text = "读出设备全球唯一的设备编号,作为加密狗用";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 112);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(432, 24);
   this.label1.TabIndex = 5;
   this.label1.Text = "建议将OUR_MIFARE.dll和ICUSB.DLL复制到应用程序同一目录下";
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(8, 152);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(264, 21);
   this.textBox1.TabIndex = 6;
   this.textBox1.Text = "天下风云出我辈 一入江湖岁月催";
   //
   // button6
   //
   this.button6.Location = new System.Drawing.Point(288, 144);
   this.button6.Name = "button6";
   this.button6.Size = new System.Drawing.Size(160, 32);
   this.button6.TabIndex = 7;
   this.button6.Text = "发送字符至读卡器显示";
   this.button6.Click += new System.EventHandler(this.button6_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(456, 198);
   this.Controls.Add(this.button6);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

}
  #endregion

/// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

private void button1_Click(object sender, System.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:
     MessageBox.Show("操作成功,数据已返回在mypiccdata数组中");
     break;
     //......
    case 8:
     MessageBox.Show("请将卡放在感应区");
     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 button3_Click(object sender, System.EventArgs e)//让设备发出声音
  {
   pcdbeep(50);
  }

private void button2_Click(object sender, System.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;

//指定卡数据
   for (i = 0; i < 48; i++)
   {
    mypiccdata[i] = i;
   }

status = piccwriteex(myctrlword, mypiccserial, myareano, authmode, mypicckey, mypiccdata);
   //在下面设定断点,然后查看mypiccserial、mypiccdata,
   //调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata,
   //开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。
   //处理返回函数
   switch (status)
   {
    case 0:
     MessageBox.Show("操作成功,mypiccdata数组中的数据已写入卡中");
     break;
     //......
    case 8:
     MessageBox.Show("请将卡放在感应区");
     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, System.EventArgs e)//修改单区密码
  {
   byte i;
   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;

}

//返回解释
   /*
   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 button5_Click(object sender, System.EventArgs e)//读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限
  {
   byte[] devno = new byte[4];
   if (pcdgetdevicenumber(devno) == 0)
   {
    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 button6_Click(object sender, System.EventArgs e)
  {
   string strls;
   strls = textBox1.Text;
   lcddispfull(strls);
  }
 }
}

转载于:https://blog.51cto.com/reader/595384

IC卡读写器c#源码相关推荐

  1. 公交IC卡读写器设计指南

    采用PHILIPS公司的Mifaue卡作IC卡,设计以射频技术为核心,以单片机为控制器的IC卡读写器在公交自动收费系统中的应用.制作的IC卡读写器可以实现制卡.售卡.自动收费等功能,具有安全.实用.方 ...

  2. IC-14W网络IC卡读写器_银河麒麟桌面操作系统V10适配测试报告

    银河麒麟操作系统产品NeoCertify 认证测试报告 系统版本:银河麒麟桌面操作系统V10 厂商名称:广州荣士电子有限公司 认证产品:IC-14W网络IC卡读写器 测试日期:2022-11-04 麒 ...

  3. IC卡读写器技术及应用

    前言 IC卡读写器又叫IC卡读写机具,它是遵循一定的协议,与IC卡建立通信关系,对IC卡进行读写操作的工具.随着IC卡在社会各领域的广泛应用,各种各样的IC卡读写机具也应运而生.为了帮助大家对IC卡读 ...

  4. java做的web系统 m1 读卡器 结合_IE浏览器接入IC卡读写器实现M1卡的读写功能

    基于ActiveX技术的ocx控件是实现网页上读写 IC卡的一个比较不错的方法,因为ocx是com组件,通过其发布的接口,IE可以实现所有的功能.ActiveX不仅能适应大部分编程语言,做到与编程语言 ...

  5. 关于IC卡读写器的应用

    IC卡读写器的应用领域 IC卡具有存储容量大,保密性好,体积小,便于携带的性能特点,可以广泛应用于门禁管理.会议签到.车辆管理.一卡通.身份验证识别等相关行业.本文介绍了一种基于射频识别技术的RFID ...

  6. ic卡读写器有哪些分类?如何使用ic卡读写器?

    ic卡读写器是读写器中的一种,通过ic卡读写器可对ic卡的信息进行读取.为增进大家对ic卡读写器的认识,本文将对ic卡读写器的分类.ic卡读写器的使用方法等内容予以介绍.如果你对ic卡读写器具有兴趣, ...

  7. IC卡读写器VB源代码

    IC卡读写器VB使用界面 详细代码 '广州荣士电子有限公司,联系电话020-82318861,82322852,定制13826029618 网址http://www.icmcu.com '读卡函数声明 ...

  8. Windows操作系统下用vmware虚拟ubuntu系统测试USB IC卡读写器的说明

    读写器介绍:RFID读写器NFC发卡器WEB可编程NDEF文本/网址/智能海报/电话/启动-淘宝网 (taobao.com) 一.打开windows计算机管理 / 服务 / VMware USB Ar ...

  9. JavaScript WebSocket NFC读写器示例源码

    读写器介绍: RFID读写器NFC发卡器WEB可编程NDEF文本/网址/智能海报/电话/启动-淘宝网 (taobao.com)https://item.taobao.com/item.htm?spm= ...

  10. RS232串口IC卡读写器控制协议

    //发卡器介绍: https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.26.12fce728BgBT3g&id=1 ...

最新文章

  1. linux 开机提示 Kernel panic - not syncing: Attempted to kill init! 解决方案
  2. Java定时多线程任务实现(TimerTask)
  3. DHL全球货运与第四范式达成合作
  4. opencv学习笔记11:图像滤波(均值,方框,高斯,中值)
  5. 教你怎么买虚拟空间(转)
  6. 在python中是否可以使用if作为变量名_在Python中可以使用if 作为变量名_python使用符号 标示注释...
  7. PMP读书笔记(第12章)
  8. linux设备资源分配,基于Linux 简化 AMP 配置使其更方便更动态地分配资源
  9. input 关闭输入法
  10. 学习手记——技术博文汇总(持续更新)
  11. 【C语言】C语言入门经典题目(范围广,内容多)
  12. Linux命令行大全(第二版)
  13. 力科(Lecroy)示波器专用波形文件(轨迹文件/trace文件/.trc文件)在MATLAB上的解析与回写
  14. 泛泛而谈的渲染学习记录(二)—— 高斯模糊卷积的线性分解理解
  15. 查看win10电脑电池损耗
  16. Vue组件设置缓存kepp-alive 后如何获取数据
  17. java.util.regex.PatternSyntaxException: Unclosed counted closure near index 14
  18. MATLAB 将图像随机裁剪为N*N的图像块
  19. js将数组转换为带间隔符的字符串
  20. 基于simulink的PID控制器设计

热门文章

  1. mysql 递归查询所有父级
  2. 谷粒学院笔记day01
  3. 软件项目管理案例教程第四版 答案
  4. 扇贝编程python骗局-原来用Python薅羊毛这么简单!后悔错过了那么多机会
  5. IKM 线上测试JavaScript
  6. 10款屏幕取色器/颜色拾取工具软件介绍及下载地址
  7. 软件需求规格说明书范例
  8. linux操作系统入门教学
  9. 深信服SCSA安全认证工程师
  10. python中的颜色及其编码