在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 刷卡签到系统(适用于考勤)

C# 刷卡签到系统(适用于考勤)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:216.96M
  • 下载次数:80
  • 浏览次数:1830
  • 发布时间:2019-10-16
  • 实例类别:C#语言基础
  • 发 布 人:2872456100
  • 文件格式:.zip
  • 所需积分:6
 相关标签: 系统

实例介绍

【实例简介】含视频讲解,但是得自建数据库

【实例截图】

from clipboard


from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Kuade.Framework
{
    /// <summary>
    /// 读卡器操作类
    /// </summary>
    public class CardReader
    {
        #region 刷卡器
        [DllImport("kernel32.dll")]
        static extern void Sleep(int dwMilliseconds);

        //=========================== System Function =============================
        [DllImport("hfrdapi.dll")]
        static extern int Sys_GetDeviceNum(UInt16 vid, UInt16 pid, ref UInt32 pNum);

        [DllImport("hfrdapi.dll")]
        static extern int Sys_GetHidSerialNumberStr(UInt32 deviceIndex, UInt16 vid, UInt16 pid, ref Char deviceString, UInt32 deviceStringLength);

        /// <summary>
        /// 打开读卡器
        /// </summary>
        /// <param name="device"></param>
        /// <param name="index"></param>
        /// <param name="vid"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        [DllImport("hfrdapi.dll")]
        static extern int Sys_Open(ref IntPtr device,
                                   UInt32 index,
                                   UInt16 vid,
                                   UInt16 pid);

        /// <summary>
        /// 判断读卡器是否已经打开
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        [DllImport("hfrdapi.dll")]
        static extern bool Sys_IsOpen(IntPtr device);

        /// <summary>
        /// 关闭读卡器
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        [DllImport("hfrdapi.dll")]
        static extern int Sys_Close(ref IntPtr device);

        /// <summary>
        /// 指示灯管理
        /// </summary>
        /// <param name="device">要操作的设备,由Sys_Open()获得</param>
        /// <param name="color">0:熄灭;1:红色;2:绿色;3:黄色</param>
        /// <returns>0:操作成功</returns>
        [DllImport("hfrdapi.dll")]
        static extern int Sys_SetLight(IntPtr device, byte color);

        /// <summary>
        /// 设置蜂鸣器
        /// </summary>
        /// <param name="device">要操作的设备,由Sys_Open()获得</param>
        /// <param name="msec">蜂鸣时限,单位是10毫秒</param>
        /// <returns></returns>
        [DllImport("hfrdapi.dll")]
        static extern int Sys_SetBuzzer(IntPtr device, byte msec);

        /// <summary>
        /// 设置天线状态
        /// </summary>
        /// <param name="device"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        [DllImport("hfrdapi.dll")]
        static extern int Sys_SetAntenna(IntPtr device, byte mode);

        /// <summary>
        /// 功能:设置读写器非接触工作方式
        /// </summary>
        /// <param name="device">要操作的设备,由Sys_Open()获得</param>
        /// <param name="type">'A':ISO14443A;'B':ISO14443B;'r':AT88RF020;'s':ST卡;'1':ISO15693
        /// </param>
        /// <returns>0 -> 操作成功</returns>
        [DllImport("hfrdapi.dll")]
        static extern int Sys_InitType(IntPtr device, byte type);


        //=========================== M1 Card Function =============================
        [DllImport("hfrdapi.dll")]
        static extern int TyA_Request(IntPtr device, byte mode, ref UInt16 pTagType);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_Anticollision(IntPtr device,
                                            byte bcnt,
                                            byte[] pSnr,
                                            ref byte pLen);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_Select(IntPtr device,
                                     byte[] pSnr,
                                     byte snrLen,
                                     ref byte pSize);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_Halt(IntPtr device);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_Authentication2(IntPtr device,
                                                 byte mode,
                                                 byte block,
                                                 byte[] pKey);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_Read(IntPtr device,
                                      byte block,
                                      byte[] pData,
                                      ref byte pLen);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_Write(IntPtr device, byte block, byte[] pData);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_InitValue(IntPtr device, byte block, Int32 value);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_ReadValue(IntPtr device, byte block, ref Int32 pValue);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_Decrement(IntPtr device, byte block, Int32 value);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_Increment(IntPtr device, byte block, Int32 value);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_Restore(IntPtr device, byte block);

        [DllImport("hfrdapi.dll")]
        static extern int TyA_CS_Transfer(IntPtr device, byte block);

        //==========================================================================
        IntPtr g_hDevice = (IntPtr)(-1); //g_hDevice must init as -1

        static char[] hexDigits = {
            '0','1','2','3','4','5','6','7',
            '8','9','A','B','C','D','E','F'};

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static byte GetHexBitsValue(byte ch)
        {
            byte sz = 0;
            if (ch <= '9' && ch >= '0')
                sz = (byte)(ch - 0x30);
            if (ch <= 'F' && ch >= 'A')
                sz = (byte)(ch - 0x37);
            if (ch <= 'f' && ch >= 'a')
                sz = (byte)(ch - 0x57);

            return sz;
        }

        #region byteHEX
        /// <summary>
        /// 单个字节转字字符.
        /// </summary>
        /// <param name="ib">字节.</param>
        /// <returns>转换好的字符.</returns>
        public static string byteHEX(Byte ib)
        {
            string _str = string.Empty;
            try
            {
                char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                'B', 'C', 'D', 'E', 'F' };
                char[] ob = new char[2];
                ob[0] = Digit[(ib >> 4) & 0X0F];
                ob[1] = Digit[ib & 0X0F];
                _str = new String(ob);
            }
            catch (Exception ex)
            {
                new Exception("对不起有错。");
            }
            return _str;

        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToHexString(byte[] bytes)
        {
            string hexString = string.Empty;
            for (var i = 0; i < bytes.Length; i  )
                hexString  = byteHEX(bytes[i]);

            return hexString;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theHex"></param>
        /// <returns></returns>
        public static byte[] ToDigitsBytes(string theHex)
        {
            byte[] bytes = new byte[theHex.Length / 2   (((theHex.Length % 2) > 0) ? 1 : 0)];
            for (var i = 0; i < bytes.Length; i  )
            {
                char lowbits = theHex[i * 2];
                char highbits;

                if ((i * 2   1) < theHex.Length)
                {
                    highbits = theHex[i * 2   1];
                }
                else
                {
                    highbits = '0';
                }

                int a = (int)GetHexBitsValue((byte)lowbits);
                int b = (int)GetHexBitsValue((byte)highbits);
                bytes[i] = (byte)((a << 4)   b);
            }

            return bytes;
        }
        #endregion

        /// <summary>
        /// 打开读卡器
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            try
            {
                string strError = "";
                int status = 0;

                //如果读卡器已经打开,则先关闭读卡器
                if (Sys_IsOpen(g_hDevice) == true)
                {
                    status = Sys_Close(ref g_hDevice);
                    if (status != 0)
                    {
                        strError = "Sys_Close failed !";
                        return false;
                    }
                }

                //连接读卡器
                status = Sys_Open(ref g_hDevice, 0, 0x0416, 0x8020);
                if (status != 0)
                {
                    strError = "Sys_Open failed !";
                    return false;
                }

                //设置读卡器读卡类型
                status = Sys_InitType(g_hDevice, (byte)'A');
                if (status != 0)
                {
                    strError = "Sys_InitType failed !";
                    return false;
                }
                Sleep(5); //Appropriate delay after Sys_InitType operating

                //设置读写器天线状态
                status = Sys_SetAntenna(g_hDevice, 1);
                if (status != 0)
                {
                    strError = "Sys_SetAntenna failed !";
                    return false;
                }
                Sleep(5); //Appropriate delay after Sys_SetAntenna operating

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 关闭读卡器
        /// </summary>
        public void Close()
        {
            Sys_Close(ref g_hDevice);
        }

        /// <summary>
        /// 蜂鸣
        /// </summary>
        public void Beep()
        {
            //蜂鸣,单位为10ms
            Sys_SetBuzzer(g_hDevice, 20);
        }

        /// <summary>
        /// 设置指示灯颜色
        /// </summary>
        public void SetLight(Color color)
        {
            byte lightColor = 1;

            if (color == Color.Red)
            {
                lightColor = 1;
            }
            else if (color == Color.Green)
            {
                lightColor = 2;
            }
            else if (color == Color.Yellow)
            {
                lightColor = 3;
            }
            else if (color == Color.Black)
            {
                lightColor = 0;
            }

            //0:熄灭;1:红色;2:绿色;3:黄色
            Sys_SetLight(g_hDevice, lightColor);
        }

        string CardID = ""; //卡号
        DateTime LastReadTime = DateTime.Now; //上次读取时间
        /// <summary>
        /// 读卡
        /// </summary>
        /// <returns></returns>
        public string Read()
        {
            try
            {
                string strError = "";
                int status = 0;

                //如果读卡器已经打开,则先关闭读卡器
                if (Sys_IsOpen(g_hDevice) == false)
                {
                    var IsOpen = Open();
                    if (IsOpen == false)
                    {
                        strError = "读卡器连接失败!";
                        return "";
                    }
                }

                byte mode = 0x52;
                ushort TagType = 0;
                byte bcnt = 0x04;//mifare 卡都用4
                byte[] dataBuffer = new byte[256];
                byte len = 255;
                byte size = 0;

                status = TyA_Request(g_hDevice, mode, ref TagType);//搜寻所有的卡
                if (status != 0)
                {
                    return "";
                }

                status = TyA_Anticollision(g_hDevice, bcnt, dataBuffer, ref len);//返回卡的序列号
                if (status != 0)
                {
                    return "";
                }

                status = TyA_Select(g_hDevice, dataBuffer, len, ref size);//锁定一张ISO14443-3 TYPE_A 卡
                if (status != 0)
                {
                    return "";
                }

                string NewID = "";
                for (int q = 0; q < len; q  )
                {
                    NewID  = byteHEX(dataBuffer[q]);
                }

                if (NewID != CardID || (DateTime.Now - LastReadTime).TotalSeconds > 5)
                {
                    LastReadTime = DateTime.Now;
                    CardID = NewID;

                    //蜂鸣,单位为10ms
                    status = Sys_SetBuzzer(g_hDevice, 20);
                    if (status != 0)
                    {
                        strError = "Sys_SetBuzzer failed !";
                        return "";
                    }

                    return NewID;
                }
            }
            catch (Exception ex)
            {
            }

            return "";
        }
    }
}

标签: 系统

实例下载地址

C# 刷卡签到系统(适用于考勤)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警