驰卡A6电动读写卡机C#调用实例

2 篇文章 0 订阅
    public class NativeMethods
    {
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_Connect(UInt32 port, UInt32 baud, out IntPtr handle);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_Disconnect(IntPtr handle);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_GetSensorStatus(IntPtr handle, [Out] byte[] Status);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_GetCRCondition(IntPtr handle, [Out] byte[] Status);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_SetCardIn(IntPtr handle, byte bFrontSet, byte bRearSet);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_Initialize(IntPtr handle, byte bFrontSet, [Out] byte[] pbVerBuff, ref UInt32 pcbVerLength);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_DetectIccType(IntPtr handle, [Out] byte[] Status);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_MoveCard(IntPtr handle, byte bFrontSet);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_LedOn(IntPtr handle);
        [DllImport("A6CRTAPI.dll")]
        public static extern int A6_LedOff(IntPtr handle);

    }

    public class A6ConstByte
    {
        /*
        Pm1=0x31 禁止前端进卡
        Pm1=0x32 磁卡方式(磁信号+开关同时有效)进卡使能, 只允许磁卡从前端开闸门进卡
        Pm1=0x33 开关方式进卡使能,允许磁卡,IC卡,Mefare 1射频卡,双界面卡从前端开闸门进卡
        Pm1=0x34 磁信号方式进卡,针对薄磁卡等一些纸卡进卡
        Pm2=0x30 允许后端进卡
        Pm2=0x31 禁止后端进卡 */
        /// <summary>
        /// 禁止前端进卡
        /// </summary>
        public const byte FCI_PROHIBITED = 0x31;
        /// <summary>
        /// 仅允许进磁卡
        /// </summary>
        public const byte FCI_MAGCARD_ONLY = 0x32;
        /// <summary>
        /// 允许前端进卡
        /// </summary>
        public const byte FCI_ALLOWED = 0x33;
        /// <summary>
        /// 允许后端进卡
        /// </summary>
        public const byte RCI_ALLOWED = 0x30;
        /// <summary>
        /// 禁止后端进卡
        /// </summary>
        public const byte RCI_PROHIBITED = 0x31;
        public const byte RESET_NOACTION = 0x30;
        public const byte RESET_EJECT = 0x31;
        public const byte RESET_RETAIN = 0x32;
        /// <summary>
        /// 移动到前端不持卡位
        /// </summary>
        public const byte MOVE_TO_FRONT_NH = 0x30;
        /// <summary>
        /// 移动到前端持卡位
        /// </summary>
        public const byte MOVE_TO_FRONT = 0x31;
        /// <summary>
        /// 移动到射频位
        /// </summary>
        public const byte MOVE_TO_RF_POS = 0x2E;
        /// <summary>
        /// 移动到IC卡位
        /// </summary>
        public const byte MOVE_TO_IC_POS = 0x2F;
        /// <summary>
        /// 移动到后端持卡位
        /// </summary>
        public const byte MOVE_TO_REAR = 0x32;
        /// <summary>
        /// 移动到后端不持卡位(从后端弹出)。
        /// </summary>
        public const byte MOVE_TO_REAR_NH = 0x33;
    }

    internal class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            IntPtr handle = (IntPtr)0;
            var resInt = NativeMethods.A6_Connect(1, 9600, out handle);
            if (resInt == 0)
            {
                Console.WriteLine($"连接读卡机成功!!");
                Console.WriteLine($"连接读卡机句柄={handle}");
            }
            else
            {
                Console.WriteLine($"连接读卡机失败!!");
                Console.ReadKey();
                return;
            }

            byte[] SensorStatusBuff = new byte[8];
            byte[] VerBuff = new byte[128];
            UInt32 BuffLen = (uint)VerBuff.Length;

            resInt = NativeMethods.A6_Initialize(handle, A6ConstByte.RESET_EJECT, VerBuff, ref BuffLen);
            if (resInt == 0)
            {
                Console.WriteLine($"初始化读卡机成功!!");
            }
            else
            {
                Console.WriteLine($"初始化读卡机失败!!");
                Console.ReadKey();
                return;
            }
            resInt = NativeMethods.A6_GetCRCondition(handle, SensorStatusBuff);

            if (resInt == 0)
            {
                Console.WriteLine($"获取读卡机状态成功!!");
                if (SensorStatusBuff[0] == 0X46) Console.WriteLine($"卡机内有长卡(卡的长度长于标准卡长度)");
                if (SensorStatusBuff[0] == 0X47) Console.WriteLine($"卡机内有短卡(卡的长度短于标准卡长度)");
                if (SensorStatusBuff[0] == 0X48) Console.WriteLine($"卡机前端,不持卡位置有卡(相当于在前端完全弹出了卡)。");
                if (SensorStatusBuff[0] == 0X49) Console.WriteLine($"卡机前端持卡位置有卡。");
                if (SensorStatusBuff[0] == 0X4A) Console.WriteLine($"卡机内停卡位置有卡。");
                if (SensorStatusBuff[0] == 0X4B) Console.WriteLine($"卡机内IC卡操作位置有卡,并且IC卡触电已下落。");
                if (SensorStatusBuff[0] == 0X4C) Console.WriteLine($"卡机后端持卡位置有卡。");
                if (SensorStatusBuff[0] == 0X4D) Console.WriteLine($"卡机后端不持卡位置有卡(相当于在后端完全弹出了卡,没收了卡)。");
                if (SensorStatusBuff[0] == 0x4E) Console.WriteLine($"卡机内无卡。");
                if (SensorStatusBuff[1] == 0X49) Console.WriteLine($"卡机允许磁卡方式进卡,只允许磁卡开闸门进卡。");
                if (SensorStatusBuff[1] == 0X4A) Console.WriteLine($"卡机允许开关方式进卡,允许磁卡,IC卡,M1射频卡,双界面卡进卡。");
                if (SensorStatusBuff[1] == 0X4B) Console.WriteLine($"卡机允许磁信号方式进卡,允许纸磁卡,薄卡进卡。");
                if (SensorStatusBuff[1] == 0x4E) Console.WriteLine($"卡机禁止进卡。");
                if (SensorStatusBuff[2] == 0X4A) Console.WriteLine($"卡机允许后端进卡,允许磁卡,IC卡,M1射频卡,双面卡进卡。");
                if (SensorStatusBuff[2] == 0x4E) Console.WriteLine($"卡机禁止后端进卡。");
            }
            else
            {
                Console.WriteLine($"获取读卡机状态失败!!");
                Console.ReadKey();
                return;
            }
            //SensorStatusBuff = new byte[8];
            //resInt = NativeMethods.A6_GetSensorStatus(handle, SensorStatusBuff);
            //if (resInt == 0)
            //{
            //    Console.WriteLine($"获取读卡机传感器状态成功!!");

            //    if (SensorStatusBuff[1] == 0X31) Console.WriteLine("前端传感器探测到有卡片!");
            //    if (SensorStatusBuff[2] == 0X31) Console.WriteLine("IC卡位传感器探测到有卡片!");
            //    if (SensorStatusBuff[3] == 0X31) Console.WriteLine("射频卡位传感器探测到有卡片!");
            //    if (SensorStatusBuff[4] == 0X31) Console.WriteLine("后端传感器探测到有卡片!");
            //    if (SensorStatusBuff[6] == 0X31) Console.WriteLine("闸门已打开!");
            //    if (SensorStatusBuff[7] == 0X31) Console.WriteLine("开关检测到有卡片插入闸门!");
            //}
            //else
            //{
            //    Console.WriteLine($"获取读卡机传感器状态失败!!");
            //    Console.ReadKey();
            //    return;
            //}

            resInt = NativeMethods.A6_SetCardIn(handle, A6ConstByte.FCI_ALLOWED, A6ConstByte.RCI_ALLOWED);
            if (resInt == 0)
            {
                Console.WriteLine($"设置前端进卡成功!!");

            }
            else
            {
                Console.WriteLine($"设置前端进卡失败!!");
                Console.ReadKey();
                return;
            }

            bool run = true;
            bool isWait = false;
            int runTime = 0;
            while (run)
            {
                if (runTime > 60)
                {
                    Console.WriteLine("超时,退出等待!");
                    run = false;
                    break;
                }
                resInt = NativeMethods.A6_LedOn(handle);
                if (resInt == 0)
                {
                    Console.WriteLine($"打开灯光成功!!");

                }
                else
                {
                    Console.WriteLine($"打开灯光失败!!");
                }
                Console.WriteLine("等待插卡!! -->" + runTime);
                resInt = NativeMethods.A6_GetSensorStatus(handle, SensorStatusBuff);
                if (resInt == 0)
                {
                    for (int i = 0; i < SensorStatusBuff.Length; i++)
                    {
                        if (SensorStatusBuff[i] == 0X31)
                        {
                            run = false;
                            Console.WriteLine("已插卡!!");
                            break;
                        }
                    }
                }
                if (runTime > 0) { isWait = true; }
                runTime++;
                Thread.Sleep(200);
            }

            if (!isWait)
            {
                resInt = NativeMethods.A6_MoveCard(handle, A6ConstByte.MOVE_TO_RF_POS);
                if (resInt == 0)
                {
                    Console.WriteLine($"重新移动卡的位置成功!!");
                }
                else
                {
                    Console.WriteLine($"重新移动卡的位置失败!!");
                    Console.ReadKey();
                    return;
                }
            }

            var IccTypeBuff = new byte[3];

            resInt = NativeMethods.A6_DetectIccType(handle, IccTypeBuff);

            if (resInt == 0)
            {
                Console.WriteLine($"检测读卡器内卡的类型成功!!");
            }
            else
            {
                Console.WriteLine($"检测读卡器内卡的类型失败!!");
                Console.ReadKey();
                return;
            }

            resInt = NativeMethods.A6_MoveCard(handle, A6ConstByte.MOVE_TO_FRONT_NH);

            if (resInt == 0)
            {
                Console.WriteLine($"前端已弹卡");
                resInt = NativeMethods.A6_LedOff(handle);
                if (resInt == 0)
                {
                    Console.WriteLine($"关闭灯光成功!!");

                }
                else
                {
                    Console.WriteLine($"关闭灯光失败!!");
                }
            }
            else
            {
                Console.WriteLine($"前端弹卡失败!!");
                Console.ReadKey();
                return;
            }

            resInt = NativeMethods.A6_SetCardIn(handle, A6ConstByte.FCI_PROHIBITED, A6ConstByte.RCI_ALLOWED);
            if (resInt == 0)
            {
                Console.WriteLine($"设置前端禁止进卡成功!!");

            }
            else
            {
                Console.WriteLine($"设置前端禁止进卡失败!!");
                Console.ReadKey();
                return;
            }

            resInt = NativeMethods.A6_Disconnect(handle);
            if (resInt == 0)
            {
                Console.WriteLine($"关闭读卡器成功!!");

            }
            else
            {
                Console.WriteLine($"关闭读卡器失败!!");
                Console.ReadKey();
                return;
            }
            Console.ReadKey();
        }
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值