/// <summary> /// 连接 /// </summary> /// <returns></returns> public bool Connect() { if (ReaderIF == null || !ReaderIF.IsConnect) { return(false); } SygoleHFReaderIF.UserConfig cfg = null; EnumREStatus re = ReaderIF.ReadUserCfg(this.readerID, ref cfg); int reTryMax = 5; int tryCount = 1; while (re != EnumREStatus.RW_SUCCESS) { if (tryCount > reTryMax) { break; } System.Threading.Thread.Sleep(100); re = ReaderIF.ReadUserCfg(this.readerID, ref cfg); tryCount++; } if (re == EnumREStatus.RW_SUCCESS && cfg != null) { this.rfidCfg = cfg; isOpened = true; return(true); } else { isOpened = false; return(false); } }
public EnumREStatus GetUID(byte ReaderID, UserConfig userCfg, out byte[] UIDBytes) { this.currentSndCmd = 1; if (recvBufLen < 15) { // Console.WriteLine("GetUID2,recvBufLen={0}",recvBufLen); return(GetUID2(ReaderID, userCfg, out UIDBytes)); } else { Console.WriteLine("GetUID:接收缓存区长度:{0}", recvBufLen); lock (recvBufLock) { UIDBytes = null; EnumREStatus reStatus = EnumREStatus.RW_SUCCESS; RFPackage package = new RFPackage(); if (package.ByteStream2Package(recvBuffer)) { reStatus = RePackageCheck(package); if (reStatus != EnumREStatus.RW_SUCCESS) { Array.Clear(recvBuffer, 0, recvBuffer.Count()); return(reStatus); } if (package.DataStatus != 0x00) { Array.Clear(recvBuffer, 0, recvBuffer.Count()); return(EnumREStatus.RW_CMD_FAILED); } UIDBytes = new byte[8]; Array.Copy(package.dataBuf, UIDBytes, Math.Min(8, package.dataBuf.Count())); } else { reStatus = EnumREStatus.RW_FAILURE; } // Array.Clear(recvBuffer, 0, recvBuffer.Count()); // recvBufLen = 0; //responseOk = false; return(reStatus); } } }
public EnumREStatus GetUID2(byte ReaderID, UserConfig userCfg, out byte[] UIDBytes) { lock (sendLock) { UIDBytes = null; if (userCfg == null) { return(EnumREStatus.RW_NONE_USERCFG); } EnumREStatus reStatus = EnumREStatus.RW_SUCCESS; //发送命令 RFPackage package = new RFPackage(); package.CmdCode = 0x01; package.ReaderID = ReaderID; package.DataStatus = 0x00; package.AddrMode = userCfg.AddrMode; package.dataLen = 0; package.Bcc = 0; byte[] byteStream = package.Package2Bytes(); lock (recvBufLock) { //发送之前清空接收缓冲区 Array.Clear(recvBuffer, 0, recvBuffer.Count()); } recvBufLen = 0; recvBegin = false; responseOk = false; this.currentSndCmd = package.CmdCode; this.expectedRecvLen = 15; //serialPort.Write(byteStream, 0, byteStream.Count()); string reStr = ""; if (!Write(byteStream, ref reStr)) { Console.WriteLine(reStr); return(EnumREStatus.RW_FAILURE); } // string strSnd=DevAccess.SgrfidRW.bytes2hexString(byteStream,byteStream.Count(),1); // Console.WriteLine("发送目录请求命令" + strSnd); //recvAutoEvent.WaitOne(recvTimeOut); WaitRes(recvTimeOut); //查询接收状态 if (!responseOk) { reStatus = EnumREStatus.RW_NO_RESPONSE; // Console.WriteLine("接收超时"); return(reStatus); } package = new RFPackage(); lock (recvBufLock) { if (package.ByteStream2Package(recvBuffer)) { reStatus = RePackageCheck(package); if (reStatus != EnumREStatus.RW_SUCCESS) { return(reStatus); } if (package.DataStatus != 0x00) { return(EnumREStatus.RW_CMD_FAILED); } UIDBytes = new byte[8]; Array.Copy(package.dataBuf, UIDBytes, Math.Min(8, package.dataBuf.Count())); } } return(reStatus); } }
public EnumREStatus WriteMBlock(byte ReaderID, UserConfig userCfg, byte BlockIndex, byte[] sndData, byte dataLen) { lock (sendLock) { if (userCfg == null) { return(EnumREStatus.RW_NONE_USERCFG); } if (dataLen > (userCfg.BlockBytes * 8)) { return(EnumREStatus.RW_PARAM_ERROR); } byte blockNums = (byte)(dataLen / userCfg.BlockBytes); if (blockNums * userCfg.BlockBytes < dataLen) { blockNums++; } byte[] realSndData = new byte[blockNums * userCfg.BlockBytes]; Array.Clear(realSndData, 0, realSndData.Count()); Array.Copy(sndData, realSndData, dataLen); sndData = realSndData; dataLen = (byte)realSndData.Count(); // Console.WriteLine("总计:{0}块,每块长度:{1}", blockNums, userCfg.BlockBytes); EnumREStatus reStatus = EnumREStatus.RW_SUCCESS; //发送命令 RFPackage package = new RFPackage(); package.CmdCode = 0x24; package.ReaderID = ReaderID; package.DataStatus = 0x00; package.AddrMode = userCfg.AddrMode; package.dataLen = (byte)(2 + dataLen); package.dataBuf = new byte[package.dataLen]; package.dataBuf[0] = BlockIndex; package.dataBuf[1] = blockNums; Array.Copy(sndData, 0, package.dataBuf, 2, dataLen); package.Bcc = GetBCC(package.dataBuf, (byte)package.dataBuf.Count()); byte[] byteStream = package.Package2Bytes(); //发送之前清空接收缓冲区 Array.Clear(recvBuffer, 0, recvBuffer.Count()); recvBufLen = 0; recvBegin = false; responseOk = false; this.currentSndCmd = package.CmdCode; this.expectedRecvLen = 7; //serialPort.Write(byteStream, 0, byteStream.Count()); string reStr = ""; if (!Write(byteStream, ref reStr)) { Console.WriteLine(reStr); return(EnumREStatus.RW_FAILURE); } // string strSnd = DevAccess.SgrfidRW.bytes2hexString(byteStream, byteStream.Count(), 1); // Console.WriteLine("发送多块数据命令" + strSnd); //recvAutoEvent.WaitOne(recvTimeOut); WaitRes(recvTimeOut); //查询接收状态 if (!responseOk) { reStatus = EnumREStatus.RW_NO_RESPONSE; return(reStatus); } package = new RFPackage(); if (package.ByteStream2Package(recvBuffer)) { reStatus = RePackageCheck(package); if (reStatus != EnumREStatus.RW_SUCCESS) { return(reStatus); } if (package.DataStatus != 0x00) { return(EnumREStatus.RW_CMD_FAILED); } } return(reStatus); } }
/// <summary> /// 读多个块 /// </summary> /// <param name="ReaderID"></param> /// <param name="userCfg"></param> /// <param name="BlockIndex"></param> /// <param name="BlockNums"></param> /// <param name="BlockDatas"></param> /// <param name="len"></param> /// <returns></returns> public EnumREStatus ReadMBlock(byte ReaderID, UserConfig userCfg, byte BlockIndex, byte BlockNums, ref byte[] BlockDatas, ref byte len) { lock (sendLock) { // Console.WriteLine("块大小:" + userCfg.BlockBytes.ToString()); if (userCfg == null) { return(EnumREStatus.RW_NONE_USERCFG); } EnumREStatus reStatus = EnumREStatus.RW_SUCCESS; //发送命令 RFPackage package = new RFPackage(); package.CmdCode = 0x23; package.ReaderID = ReaderID; package.DataStatus = 0x00; package.AddrMode = userCfg.AddrMode; package.dataLen = 0x02; package.dataBuf = new byte[package.dataLen]; package.dataBuf[0] = BlockIndex; package.dataBuf[1] = BlockNums; package.Bcc = GetBCC(package.dataBuf, (byte)package.dataBuf.Count()); byte[] byteStream = package.Package2Bytes(); //发送之前清空接收缓冲区 Array.Clear(recvBuffer, 0, recvBuffer.Count()); recvBufLen = 0; recvBegin = false; responseOk = false; this.currentSndCmd = package.CmdCode; this.expectedRecvLen = (byte)(7 + BlockNums * userCfg.BlockBytes); // serialPort.Write(byteStream, 0, byteStream.Count()); DateTime dtStsnd = DateTime.Now; string reStr = ""; if (!Write(byteStream, ref reStr)) { Console.WriteLine(reStr); return(EnumREStatus.RW_FAILURE); } //recvAutoEvent.WaitOne(recvTimeOut); WaitRes(recvTimeOut); if (recvBufLen > 0) { BlockDatas = new byte[recvBufLen]; Array.Copy(recvBuffer, BlockDatas, recvBufLen); } //查询接收状态 if (!responseOk) { reStatus = EnumREStatus.RW_NO_RESPONSE; string strLog = string.Format("RFID {0}返回数据超时,命令发送时间:{1}", readerIP, dtStsnd.ToString("HH:mm:ss")); Console.WriteLine(strLog); return(reStatus); } package = new RFPackage(); if (package.ByteStream2Package(recvBuffer)) { reStatus = RePackageCheck(package); if (reStatus != EnumREStatus.RW_SUCCESS) { return(reStatus); } if (package.DataStatus != 0x00) { return(EnumREStatus.RW_CMD_FAILED); } len = package.dataLen; BlockDatas = new byte[len]; Array.Copy(package.dataBuf, BlockDatas, len); } return(reStatus); } }
/// <summary> /// 读单块 /// </summary> /// <param name="ReaderID"></param> /// <param name="BlockIndex">块号,从0开始编号</param> /// <param name="BlockDatas">接收数据缓存</param> /// <param name="len"></param> /// <returns></returns> public EnumREStatus ReadSBlock(byte ReaderID, UserConfig userCfg, byte BlockIndex, ref byte[] BlockDatas, ref byte len, ref byte[] recvByteArray) { lock (sendLock) { if (userCfg == null) { return(EnumREStatus.RW_NONE_USERCFG); } EnumREStatus reStatus = EnumREStatus.RW_SUCCESS; //发送命令 RFPackage package = new RFPackage(); package.CmdCode = 0x20; package.ReaderID = ReaderID; package.DataStatus = 0x00; package.AddrMode = userCfg.AddrMode; package.dataLen = 0x01; package.dataBuf = new byte[1]; package.dataBuf[0] = BlockIndex; package.Bcc = GetBCC(package.dataBuf, (byte)package.dataBuf.Count()); byte[] byteStream = package.Package2Bytes(); //发送之前清空接收缓冲区 Array.Clear(recvBuffer, 0, recvBuffer.Count()); recvBufLen = 0; recvBegin = false; responseOk = false; this.currentSndCmd = package.CmdCode; this.expectedRecvLen = 11;// (byte)(7 + this.userCfg.BlockBytes); //serialPort.Write(byteStream, 0, byteStream.Count()); string reStr = ""; if (!Write(byteStream, ref reStr)) { Console.WriteLine(reStr); return(EnumREStatus.RW_FAILURE); } //recvAutoEvent.WaitOne(recvTimeOut); WaitRes(recvTimeOut); if (recvBufLen > 0) { recvByteArray = new byte[recvBufLen]; Array.Copy(recvBuffer, recvByteArray, recvBufLen); } //查询接收状态 if (!responseOk) { reStatus = EnumREStatus.RW_NO_RESPONSE; return(reStatus); } package = new RFPackage(); if (package.ByteStream2Package(recvBuffer)) { reStatus = RePackageCheck(package); if (reStatus != EnumREStatus.RW_SUCCESS) { return(reStatus); } if (package.DataStatus != 0x00) { return(EnumREStatus.RW_CMD_FAILED); } len = package.dataLen; BlockDatas = new byte[len]; Array.Copy(package.dataBuf, BlockDatas, len); } return(reStatus); } }
public EnumREStatus ReadUserCfg(byte readerID, ref UserConfig cfg, ref byte[] recvByteArray) { cfg = null; lock (sendLock) { EnumREStatus reStatus = EnumREStatus.RW_SUCCESS; //发送命令 RFPackage package = new RFPackage(); package.CmdCode = 0xAE; package.ReaderID = readerID; package.DataStatus = 0x00; package.AddrMode = 0x00; package.dataLen = 0; package.dataBuf = null; package.Bcc = 0x00; byte[] byteStream = package.Package2Bytes(); //发送之前清空接收缓冲区 Array.Clear(recvBuffer, 0, recvBuffer.Count()); recvBufLen = 0; recvBegin = false; responseOk = false; this.currentSndCmd = package.CmdCode; this.expectedRecvLen = 14; //serialPort.Write(byteStream, 0, byteStream.Count()); string reStr = ""; if (!Write(byteStream, ref reStr)) { Console.WriteLine(reStr); return(EnumREStatus.RW_FAILURE); } //recvAutoEvent.WaitOne(recvTimeOut); WaitRes(recvTimeOut); if (recvBufLen > 0) { recvByteArray = new byte[recvBufLen]; Array.Copy(recvBuffer, recvByteArray, recvBufLen); } //查询接收状态 if (!responseOk) { reStatus = EnumREStatus.RW_NO_RESPONSE; return(reStatus); } package = new RFPackage(); if (package.ByteStream2Package(recvBuffer)) { reStatus = RePackageCheck(package); if (reStatus != EnumREStatus.RW_SUCCESS) { return(reStatus); } if (package.DataStatus != 0x00) { return(EnumREStatus.RW_FAILURE); } cfg = new UserConfig(); cfg.AddrMode = package.dataBuf[0]; cfg.ReaderID = package.dataBuf[1]; cfg.RFChipPower = package.dataBuf[2]; if (package.dataBuf[3] > 0) { cfg.NeedBcc = true; } else { cfg.NeedBcc = false; } cfg.ComPortType = package.dataBuf[4]; // cfg.BlockBytes = package.dataBuf[5]; cfg.AvailableType = package.dataBuf[6]; } return(reStatus); } }
public EnumREStatus WriteMBlock(byte ReaderID, UserConfig userCfg, byte BlockIndex, byte[] sndData, byte dataLen) { lock (sendLock) { if (userCfg == null) { return(EnumREStatus.RW_NONE_USERCFG); } if (dataLen > (userCfg.BlockBytes * 8)) { return(EnumREStatus.RW_PARAM_ERROR); } byte blockNums = (byte)(dataLen / userCfg.BlockBytes); EnumREStatus reStatus = EnumREStatus.RW_SUCCESS; //发送命令 RFPackage package = new RFPackage(); package.CmdCode = 0x24; package.ReaderID = ReaderID; package.DataStatus = 0x00; package.AddrMode = userCfg.AddrMode; package.dataLen = (byte)(2 + dataLen); package.dataBuf = new byte[package.dataLen]; package.dataBuf[0] = BlockIndex; package.dataBuf[1] = blockNums; Array.Copy(sndData, 0, package.dataBuf, 2, dataLen); package.Bcc = GetBCC(package.dataBuf, (byte)package.dataBuf.Count()); byte[] byteStream = package.Package2Bytes(); //发送之前清空接收缓冲区 Array.Clear(recvBuffer, 0, recvBuffer.Count()); recvBytes = 0; recvBegin = false; responseOk = false; this.currentSndCmd = package.CmdCode; this.expectedRecvLen = 7; serialPort.Write(byteStream, 0, byteStream.Count()); recvAutoEvent.WaitOne(recvTimeOut); //查询接收状态 if (!responseOk) { reStatus = EnumREStatus.RW_NO_RESPONSE; return(reStatus); } package = new RFPackage(); if (package.ByteStream2Package(recvBuffer)) { reStatus = RePackageCheck(package); if (reStatus != EnumREStatus.RW_SUCCESS) { return(reStatus); } if (package.DataStatus != 0x00) { return(EnumREStatus.RW_CMD_FAILED); } } return(reStatus); } }