示例#1
0
        private void btnWrite_Click(object sender, EventArgs e)
        {
            NfcOperate nfcOperate = new NfcOperate();

            //string nfcId = nfcOperate.ReadNfcId(bConnectedDevice);
            nfcOperate.nfcDataWrite(bConnectedDevice, textBoxData.Text.Trim());
        }
示例#2
0
        private void btnRead_Click(object sender, EventArgs e)
        {
            textBoxData.Text = string.Empty;
            NfcOperate nfcOperate = new NfcOperate();

            //string nfcId = nfcOperate.ReadNfcId(bConnectedDevice);
            textBoxData.Text = nfcOperate.nfcDataRead(bConnectedDevice);
        }
示例#3
0
        /// <summary>
        /// 将数据写入NFC卡,从0区0块开始写入,直到写完。
        /// </summary>
        /// <param name="mass">区</param>
        /// <param name="submass">块1,2,3</param>
        /// <param name="context">将要写入NFC卡的数据,小于32个字符</param>
        public void WriteBlock(bool bConnectedDevice, int mass, int submass, string context)
        {
            NfcOperate nfcOperate = new NfcOperate();
            short      icdev      = 0x0000;
            int        status;
            byte       mode  = 0x60;//mode = 0x61; //密钥类型
            string     mima  = "FFFFFFFFFFFF";
            byte       secnr = 0x00;
            byte       adr;
            int        i;

            if (!bConnectedDevice)
            {
                MessageBox.Show("请连接NFC读写器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            secnr = Convert.ToByte(mass);                        //cbxMass2这个是区
            adr   = (byte)(Convert.ToByte(submass) + secnr * 4); //这个是块cbxSubmass2

            if (submass == 4)
            {
                if (DialogResult.Cancel == MessageBox.Show("不允许写每个区的最后一块", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
                {
                    return;
                }
            }

            IntPtr keyBuffer = Marshal.AllocHGlobal(1024);

            //txtInputKey2.Text是每个扇区的密码FFFFFFFFFFFF
            byte[] bytesKey = nfcOperate.ToDigitsBytes(mima);
            for (i = 0; i < bytesKey.Length; i++)
            {
                Marshal.WriteByte(keyBuffer, i, bytesKey[i]);
            }
            status = Er302Helper.rf_M1_authentication2(icdev, mode, (byte)(secnr * 4), keyBuffer);
            Marshal.FreeHGlobal(keyBuffer);
            if (status != 0)
            {
                MessageBox.Show("rf_M1_authentication2 failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //
            byte[] bytesBlock;
            if (submass == 1)
            {
                bytesBlock = nfcOperate.ToDigitsBytes(context);
            }
            else if (submass == 2)
            {
                bytesBlock = nfcOperate.ToDigitsBytes(context);
            }
            else if (submass == 3)
            {
                bytesBlock = nfcOperate.ToDigitsBytes(context);
            }
            else
            {
                String strCompont = context;
                bytesBlock = nfcOperate.ToDigitsBytes(strCompont);
            }

            IntPtr dataBuffer = Marshal.AllocHGlobal(1024);

            for (i = 0; i < bytesBlock.Length; i++)
            {
                Marshal.WriteByte(dataBuffer, i, bytesBlock[i]);
            }
            status = Er302Helper.rf_M1_write(icdev, adr, dataBuffer);
            Marshal.FreeHGlobal(dataBuffer);

            if (status != 0)
            {
                MessageBox.Show("NFC写入数据出错!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
示例#4
0
        // This command will convert the input text string to Ascii hex array then write into the NTAG chip page by page.
        public void nfcDataWrite(bool bConnectedDevice, string context)
        {
            NfcOperate nfcOperate = new NfcOperate();
            short      icdev      = 0x0000;
            int        status;
            byte       npage;
            int        i;

            if (!bConnectedDevice)
            {
                MessageBox.Show("请连接NFC读写器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int Len;

            Len = context.Trim().Length;
            Len = Len + 3;  // add Encode bytes(2) and tailer code byte(1)

            IntPtr dataBuffer = Marshal.AllocHGlobal(1024);

            npage = (byte)(Convert.ToByte(4));
            byte[] page4_buf = new byte[] { 0x01, 0x03, 0xa0, 0x10 }; ////Page4_buf[4]={0x01,0x03,0xa0,0x10};//0c
            for (i = 0; i < page4_buf.Length; i++)
            {
                Marshal.WriteByte(dataBuffer, i, page4_buf[i]);
            }
            status = Er302Helper.rf_ul_write(icdev, npage, dataBuffer);

            if (status != 0)
            {
                MessageBox.Show("Write page 4 failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            npage = (byte)(Convert.ToByte(5));
            byte[] page5_buf = new byte[] { 0x44, 0x03, (byte)(Len + 4), 0xd1 };
            for (i = 0; i < page5_buf.Length; i++)
            {
                Marshal.WriteByte(dataBuffer, i, page5_buf[i]);
            }
            status = Er302Helper.rf_ul_write(icdev, npage, dataBuffer);

            if (status != 0)
            {
                MessageBox.Show("Write page 5 failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            npage = (byte)(Convert.ToByte(6));
            byte[] page6_buf = new byte[] { 0x01, (byte)Len, 0x54, 0x02 };
            for (i = 0; i < page6_buf.Length; i++)
            {
                Marshal.WriteByte(dataBuffer, i, page6_buf[i]);
            }
            status = Er302Helper.rf_ul_write(icdev, npage, dataBuffer);

            if (status != 0)
            {
                MessageBox.Show("Write page 6 failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int tPage = 0;   // Total Pages

            tPage = Len / 4;
            int j = Len % 4;
            int k = 0;  // fill zero counter

            if (j > 0)
            {
                tPage = tPage + 1;
                k     = 4 - j;
            }
            string sZero     = "00";
            string sFillTail = string.Empty;

            if (k > 0)
            {
                for (i = 0; i < k; i++)
                {
                    sFillTail += sZero;
                }
            }

            byte[] bEncode = new byte[] { 0x65, 0x6e };
            string sEncode = string.Empty;

            for (i = 0; i < bEncode.Length; i++)
            {
                sEncode += Convert.ToString(bEncode[i], 16);
            }

            byte[] bTrailer = new byte[] { 0xfe };
            string sTailer  = string.Empty;

            for (i = 0; i < bTrailer.Length; i++)
            {
                sTailer += Convert.ToString(bTrailer[i], 16);
            }

            byte[] bytesBlock;
            bytesBlock = Encoding.ASCII.GetBytes(context.Trim());// Convert the string input to ascii code bytes.
            string sInput = string.Empty;

            for (i = 0; i < bytesBlock.Length; i++)
            {
                sInput += Convert.ToString(bytesBlock[i], 16);
            }

            string sComb = string.Empty;

            sComb     += sEncode;
            sComb     += sInput;
            sComb     += sTailer;
            sComb     += sFillTail;
            bytesBlock = nfcOperate.ToDigitsBytes(sComb);    // convert to hex byte string

            int ofs;

            for (j = 7; j < tPage + 7; j++)
            {
                ofs = (j - 7) * 4;
                for (i = 0; i < 4; i++)
                {
                    Marshal.WriteByte(dataBuffer, i, bytesBlock[i + ofs]);
                }                                                          // copy to pointer buffer
                status = Er302Helper.rf_ul_write(icdev, (byte)j, dataBuffer);
                if (status != 0)
                {
                    MessageBox.Show("NFC写入失败!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            Marshal.FreeHGlobal(dataBuffer);
            int color = 2;// 2:red led light ,1: blue light,0: led off

            Er302Helper.rf_light(icdev, (byte)color);
            int delaytime = 50;

            Er302Helper.rf_beep(icdev, (byte)delaytime);
            color = 0;
            Er302Helper.rf_light(icdev, (byte)color);
            color = 1;
            Er302Helper.rf_light(icdev, (byte)color);
            MessageBox.Show("NFC写入成功!", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }