示例#1
0
        /// <summary>
        /// 读取标签
        /// 读取出范围内所有标签
        /// </summary>
        /// <returns></returns>
        public static List <string> ReadEPC()
        {
            if (IsOper == false)
            {
                throw new Exception("请先打开串口");
            }
            List <string> epc_lis  = new List <string>();
            int           CardNum  = 0;
            int           Totallen = 0;

            byte[] EPC     = new byte[5000];
            int    fCmdRet = StaticClassReaderB.Inventory_G2(ref ComAdr, 0, 0, 0, EPC, ref Totallen, ref CardNum, port);

            if ((fCmdRet == 1) | (fCmdRet == 2) | (fCmdRet == 3) | (fCmdRet == 4) | (fCmdRet == 0xFB))//代表已查找结束,
            {
                byte[] daw = new byte[Totallen];
                Array.Copy(EPC, daw, Totallen);
                string temps = RFIDResources.ByteArrayToHexString(daw);

                int m = 0;
                for (int CardIndex = 0; CardIndex < CardNum; CardIndex++)
                {
                    byte   EPClen = daw[m];
                    string sEPC   = temps.Substring(m * 2 + 2, EPClen * 2);
                    epc_lis.Add(sEPC);
                    m = m + EPClen + 1;
                    if (sEPC.Length != EPClen * 2)
                    {
                        break;
                    }
                }
            }
            return(epc_lis);
        }
示例#2
0
        /// <summary>
        /// 按块读数据
        /// 0-19 每块4个字节
        /// </summary>
        /// <param name="epc"></param>
        /// <param name="pwd"></param>
        /// <param name="block_num"></param>
        /// <returns></returns>
        public static string ReadData(string epc, string pwd, int block_num)
        {
            if (!IsOper)
            {
                throw new Exception("请先打开串口");
            }
            if (string.IsNullOrEmpty(epc) || string.IsNullOrEmpty(pwd))
            {
                throw new Exception("请补全信息");
            }
            if (pwd.Length != 8)
            {
                throw new Exception("密码长度要为8位");
            }
            if (block_num < 0 || block_num > 19)
            {
                throw new Exception("读取地址必须在0-19之间");
            }

            byte ENum   = Convert.ToByte(epc.Length / 4);
            byte EPCLen = Convert.ToByte(epc.Length / 2);

            byte[] bEPC = new byte[ENum];
            bEPC = RFIDResources.HexStringToByteArray(epc);

            bPassWord = RFIDResources.HexStringToByteArray(pwd);
            byte BNum = Convert.ToByte(block_num.ToString(), 16);

            byte[] bData = new byte[320];

            int flag = StaticClassReaderB.ReadCard_G2(ref ComAdr, bEPC, RFIDResources.MemUser, BNum
                                                      , 1, bPassWord, 0, 0, 0, bData, EPCLen, ref errcode, handle);

            if (flag != 0)
            {
                if (errcode > -1)
                {
                    throw new Exception(RFIDResources.GetErrorCodeDesc(errcode));
                }
                else
                {
                    throw new Exception(RFIDResources.GetReturnCodeDesc(flag));
                }
            }
            byte[] daw = new byte[1 * 2];
            Array.Copy(bData, daw, 1 * 2);
            string str = RFIDResources.ByteArrayToHexString(daw);

            return(str);
        }