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