C# Winform 多线程监听IC卡读卡器

本文介绍了如何在C# Winform应用中使用多线程实现IC卡读卡器的自动监听读卡功能。通过创建读卡器SDK引用包,并封装成多线程读卡类,在FormClosed事件中处理,实现用户友好的连续读卡体验。代码注释提供了详细说明。
摘要由CSDN通过智能技术生成

 本文原创,转载请注明出处!

    我们使用IC读卡器时,一般可分为手动读卡和自动监听读卡两种方式。

    其中手动读卡根据IC设备厂商提供的Demo修改一下便可实现,相对简单,本文不做过多介绍。

    而自动监听读卡,看起来效果要好的多,用户使用起来也要方便一下。其实现的逻辑大家应该都知道,就是通过开一个多线程来轮训IC卡读卡的结果,下面就详细讲一下具体开发过程。(注:本文的读卡器型号为URF-R330,其他设备原理相同)

  • 建立读卡器SDK引用包,如果有不明白的,可以到设备官网或百度下载设备接口的文档,直接上代码
    /// <summary>
    /// common 的摘要说明。
    /// </summary>
    public class common
    {
        public common()
        {
        }

        public int icdev; // 通讯设备标识符
        [DllImport("mwrf32.dll", EntryPoint = "rf_init", SetLastError = true,
         CharSet = CharSet.Auto, ExactSpelling = false,
         CallingConvention = CallingConvention.StdCall)]
        //说明:初始化串口通讯接口
        public static extern int rf_init(Int16 port, int baud);

        [DllImport("mwrf32.dll", EntryPoint = "rf_exit", SetLastError = true,
         CharSet = CharSet.Auto, ExactSpelling = false,
         CallingConvention = CallingConvention.StdCall)]
        //说明:    关闭通讯口
        public static extern Int16 rf_exit(int icdev);

        [DllImport("mwrf32.dll", EntryPoint = "rf_get_status", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:     
        public static extern Int16 rf_get_status(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] state);

        [DllImport("mwrf32.dll", EntryPoint = "rf_beep", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:     
        public static extern Int16 rf_beep(int icdev, int msec);

        [DllImport("mwrf32.dll", EntryPoint = "rf_load_key", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:     
        public static extern Int16 rf_load_key(int icdev, int mode, int secnr, [MarshalAs(UnmanagedType.LPArray)]byte[] keybuff);


        [DllImport("mwrf32.dll", EntryPoint = "rf_changeb3", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:     修改扇区密码
        public static extern Int16 rf_changeb3(int icdev, int _SecNr, [MarshalAs(UnmanagedType.LPArray)]byte[] _KeyA, int _B0, int _B1, int _B2, int _B3, int _Bk, [MarshalAs(UnmanagedType.LPArray)]byte[] _KeyB);


        [DllImport("mwrf32.dll", EntryPoint = "rf_load_key_hex", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:     
        public static extern Int16 rf_load_key_hex(int icdev, int mode, int secnr, [MarshalAs(UnmanagedType.LPArray)]byte[] keybuff);


        [DllImport("mwrf32.dll", EntryPoint = "a_hex", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvent
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值