예제 #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="data"></param>
        /// <param name="block_num"></param>
        public static void WriteData(string epc, string pwd, string data, int block_num)
        {
            if (!IsOper)
            {
                throw new Exception("请先打开串口");
            }
            if (string.IsNullOrEmpty(epc) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(data))
            {
                throw new Exception("请补全信息");
            }
            if (pwd.Length != 8)
            {
                throw new Exception("密码长度要为8位");
            }
            if (data.Length % 4 != 0)
            {
                throw new Exception("密码长度要为4的倍数");
            }
            byte WrittenDataNum = 0;
            byte ENum           = Convert.ToByte(epc.Length / 4);
            byte EPClen         = Convert.ToByte(ENum * 2);

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

            bPassWord = RFIDResources.HexStringToByteArray(pwd);
            byte Wnum = Convert.ToByte(data.Length / 4);

            byte[] WriteData = new byte[Wnum * 2];
            WriteData = RFIDResources.HexStringToByteArray(data);
            byte WriteDataLen = Convert.ToByte(Wnum * 2);

            byte BNum = Convert.ToByte(block_num.ToString(), 16);

            int flag = StaticClassReaderB.WriteCard_G2(ref ComAdr, bEPC,
                                                       RFIDResources.MemUser, BNum, WriteDataLen, WriteData,
                                                       bPassWord, 0, 0, 0, WrittenDataNum, EPClen, ref errcode, handle);

            if (flag != 0)
            {
                if (errcode > -1)
                {
                    throw new Exception(RFIDResources.GetErrorCodeDesc(errcode));
                }
                else
                {
                    throw new Exception(RFIDResources.GetReturnCodeDesc(flag));
                }
            }
        }
예제 #3
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);
        }
예제 #4
0
        /// <summary>
        /// 修改密码 销毁密码
        /// 谨慎操作 用于销毁标签
        /// </summary>
        /// <param name="epc"></param>
        /// <param name="old_pwd"></param>
        /// <param name="new_pwd"></param>
        public static void ChangeAdminPwd(string epc, string old_pwd, string new_pwd)
        {
            if (!IsOper)
            {
                throw new Exception("请先打开串口");
            }
            if (string.IsNullOrEmpty(epc) || string.IsNullOrEmpty(old_pwd) || string.IsNullOrEmpty(new_pwd))
            {
                throw new Exception("请补全信息");
            }
            if (old_pwd.Length != 8 || new_pwd.Length != 8)
            {
                throw new Exception("密码长度要为8位");
            }
            byte WrittenDataNum = 0;
            byte ENum           = Convert.ToByte(epc.Length / 4);
            byte EPClen         = Convert.ToByte(ENum * 2);

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

            bPassWord = RFIDResources.HexStringToByteArray(old_pwd);
            byte Wnum = Convert.ToByte(new_pwd.Length / 4);

            byte[] WriteData = new byte[Wnum * 2];
            WriteData = RFIDResources.HexStringToByteArray(new_pwd);
            byte WriteDataLen = Convert.ToByte(Wnum * 2);

            int flag = StaticClassReaderB.WriteCard_G2(ref ComAdr, bEPC,
                                                       RFIDResources.MemReser, 0x0, WriteDataLen, WriteData,
                                                       bPassWord, 0, 0, 0, WrittenDataNum, EPClen, ref errcode, handle);

            if (flag != 0)
            {
                if (errcode > -1)
                {
                    throw new Exception(RFIDResources.GetErrorCodeDesc(errcode));
                }
                else
                {
                    throw new Exception(RFIDResources.GetReturnCodeDesc(flag));
                }
            }

            SetRegiSec(epc, new_pwd);
        }
예제 #5
0
        /// <summary>
        /// 打开指定端口
        /// </summary>
        /// <param name="port"></param>
        public static void OpenCard(int port)
        {
            if (IsOper)
            {
                throw new Exception("串口已打开");
            }
            int flag = StaticClassReaderB.OpenComPort(port, ref ComAdr,
                                                      Convert.ToByte(RFIDResources._57600bps), ref handle);

            if (flag == 0)
            {
                IsOper = true;
            }
            else
            {
                throw new Exception(RFIDResources.GetReturnCodeDesc(flag));
            }
        }
예제 #6
0
        /// <summary>
        /// 写标签
        /// </summary>
        /// <param name="epc"></param>
        /// <param name="pwd"></param>
        public static void WriteEPC(string epc, string pwd)
        {
            if (!IsOper)
            {
                throw new Exception("请先打开串口");
            }
            if (string.IsNullOrEmpty(epc))
            {
                throw new Exception("EPC不能为空");
            }
            if (epc.Length % 2 != 0)
            {
                throw new Exception("EPC长度必须是2的倍数");
            }
            if (string.IsNullOrEmpty(pwd))
            {
                throw new Exception("密码不能为空");
            }
            if (pwd.Length != 8)
            {
                throw new Exception("密码长度要为8位");
            }

            byte epcLen = Convert.ToByte(epc.Length / 2);
            byte eNum   = Convert.ToByte(epc.Length / 4);

            byte[] EPC = new byte[eNum];
            EPC       = RFIDResources.HexStringToByteArray(epc);
            bPassWord = RFIDResources.HexStringToByteArray(pwd);
            int flag = StaticClassReaderB.WriteEPC_G2(ref ComAdr, bPassWord, EPC, epcLen,
                                                      ref errcode, handle);

            if (flag != 0)
            {
                if (errcode > -1)
                {
                    throw new Exception(RFIDResources.GetErrorCodeDesc(errcode));
                }
                else
                {
                    throw new Exception(RFIDResources.GetReturnCodeDesc(flag));
                }
            }
        }
예제 #7
0
        private static void SetRegiSec(string epc, string pwd)
        {
            if (string.IsNullOrEmpty(epc) || string.IsNullOrEmpty(pwd))
            {
                throw new Exception("请补全信息");
            }
            if (pwd.Length != 8)
            {
                throw new Exception("密码长度要为8位");
            }

            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);

            List <byte> lis = new List <byte>()
            {
                0x00, 0x01, 0x02, 0x04
            };

            foreach (byte b in lis)
            {
                int flag = StaticClassReaderB.SetCardProtect_G2(ref ComAdr, bEPC, b, 0x02, bPassWord
                                                                , 0, 0, 0, EPClen, ref errcode, handle);
                if (flag != 0)
                {
                    if (errcode > -1)
                    {
                        throw new Exception(RFIDResources.GetErrorCodeDesc(errcode));
                    }
                    else
                    {
                        throw new Exception(RFIDResources.GetReturnCodeDesc(flag));
                    }
                }

                Thread.Sleep(10);
            }
        }