示例#1
0
        //向指定扇区写入指定数据
        private static async Task InitValue(byte blockNum, int value)
        {
            await AuthKey(0);

            //await reader.ChangeToISO14443AAsync();
            byte[] data = { 0, 0, 0, 1 };
            var    info = await i14443a.ValueAsync(OperCode.Increase, 4, data, 5);

            Console.WriteLine(info.GetSendByteStr());
            Console.WriteLine(info.GetRecvByteStr());
            Console.WriteLine(info.GetStatusStr());



            //if (!await AuthKey((byte)(blockNum / 4))) return;
            //byte[] data = BitConverter.GetBytes(value);
            //var info = await i14443a.InitValueAsync(blockNum, data);
            //Console.WriteLine(info.GetSendByteStr());
            //Console.WriteLine(info.GetRecvByteStr());
            //if (info.ReturnValue == ReturnMessage.Success)
            //{
            //    Console.WriteLine("值块" + blockNum.ToString() + "初始化成功");
            //}
            //else
            //{
            //    Console.WriteLine(info.GetStatusStr());
            //}
        }
示例#2
0
        /// <summary>
        /// 让程序处于工作状态,等待刷卡并扣钱
        /// </summary>
        /// <param name="coin">每次刷卡扣的钱数</param>
        /// <returns></returns>
        private async Task Work(int coin, byte blockNum)
        {
            byte[] data = BitConverter.GetBytes(coin * 100);
            while (!ts.IsCancellationRequested)
            {
                // 选卡
                byte[] uid = await Select();

                if (uid != null)
                {
                    if (CompareUID(uid, operUID)) //如果此卡之前已经扣过钱且未离开感应区,则不操作
                    {
                        continue;
                    }
                }
                else
                {
                    operUID = null; //说明卡片已经离开感应区
                    continue;
                }
                //证实
                if (!await AuthKey((byte)(blockNum / 4)))
                {
                    continue;
                }
                //读取剩余钱数,判断卡内剩余的钱是否够扣
                var info1 = await i14443a.ReadValueAsync(blockNum);

                if (info1.ReturnValue == ReturnMessage.Success)
                {
                    int remain = BitConverter.ToInt32(info1.ValueData, 0);
                    if (remain < coin * 100)
                    {
                        string money = Convert.ToString(remain / 100);
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            txtMsg.AppendText("\r\n卡内余额不足,现剩余:" + money + " 块钱。");
                        }));
                        operUID = uid;
                        continue;
                    }
                }

                var info = await i14443a.ValueAsync(OperCode.Decrease, blockNum, data, blockNum); //扣钱

                if (info.ReturnValue != ReturnMessage.Success)
                {
                    continue;
                }
                operUID = uid;                   //记录被扣钱的标签UID,以防下次重复扣钱
                await reader.BeepAsync(5, 0, 1); //蜂鸣器响一声

                this.Dispatcher.Invoke(new Action(() =>
                {
                    txtMsg.AppendText("\r\n成功刷卡,扣除" + coin.ToString() + "块钱。");
                }));
                //读取剩余钱数
                info1 = await i14443a.ReadValueAsync(blockNum);

                if (info1.ReturnValue == ReturnMessage.Success)
                {
                    int    remain = BitConverter.ToInt32(info1.ValueData, 0);
                    string money  = Convert.ToString(remain / 100);
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        txtMsg.AppendText("\r\n卡内剩余:" + money + " 块钱。");
                    }));
                }
                Thread.Sleep(300);
            }
        }