private bool GetTagUID() { //only can inventery 1 tag, because the reader is a shit UInt16 byteLen = 0; byte[] ary_data = new byte[9]; //the first byte is DSFID, and the other 8 byte containers the UID data try { st = ISO15693Commands.rf_inventory(ReaderInfo.icdev, 0x36, 0x00, 0x00, out byteLen, ary_data); if (st != 0) { MessageBox.Show("未发现单个标签"); return(false); } else { Array.Copy(ary_data, 1, tagUIDbyte, 0, 8); byte[] msbFstUID = new byte[8]; Array.Copy(tagUIDbyte, msbFstUID, 8); Array.Reverse(msbFstUID); tagUIDstring = CCommondMethod.ByteArrayToString(msbFstUID, 0, 8); return(true); } } catch (Exception) { return(false); } }
/* * write data to tag, the first two byte contains data length, * the develop note says the reader can doing write 10 blocks one time, * but reality is it only can write 1 block one time. * totally shit */ private bool WriteData(string data) { try { byte[] dataLen = BitConverter.GetBytes(Convert.ToInt16(data.Length)); byte[] dataBytes = Encoding.ASCII.GetBytes(data); byte[] writenDataAll = new byte[data.Length + 4]; dataLen.CopyTo(writenDataAll, 2); //data length stored in the third and forth byte dataBytes.CopyTo(writenDataAll, 4); //usefal data starts from next block--the fifth byte int i_totalBytes = data.Length + 4; //UOM is byte st = 0; byte blockIndex = 0; int byteIndex = 0; while (i_totalBytes > 0 && st == 0) { //calculate the byte number of writen data, the max number is 10 block = 40 bytes byte byteNumber = i_totalBytes > 4 ? (byte)4 : (byte)i_totalBytes; byte[] writenData = new byte[4];//the minimum writen unit is block Array.Copy(writenDataAll, byteIndex, writenData, 0, byteNumber); st = ISO15693Commands.rf_writeblock(ReaderInfo.icdev, 0x22, blockIndex, (byte)1, m_btTagUID, (byte)4, writenData); if (st != 0) { return(false); } byteIndex += byteNumber; blockIndex += 1; i_totalBytes -= byteNumber; System.Threading.Thread.Sleep(20); } return(true); } catch (Exception) { return(false); } }
private ErrorCode ReadData() { // the application note says, max block number per read is 10 blocks. try { byte[] rtnData = new byte[4]; //read first block, get the data length byte rtnLen = 0; st = ISO15693Commands.rf_readblock(ReaderInfo.icdev, 0x22, 0x00, (byte)1, tagUIDbyte, out rtnLen, rtnData); if (st != 0) { //MessageBox.Show("error"); return(ErrorCode.ReadFail); } else { //bool b_readLengthData = true; byte[] lenthData = new byte[2]; //the first two bytes stored data length Array.Copy(rtnData, 2, lenthData, 0, 2); Int32 i_totalBytes = BitConverter.ToInt16(lenthData, 0) + 4; if (i_totalBytes == 4) { return(ErrorCode.TagHasNoData); } readBuffer = new byte[i_totalBytes - 4]; st = 0; byte blockIndex = 1; int byteIndex = 0; while (i_totalBytes > 0 && st == 0) { byte blockLen = 0; //if (i_totalBytes % 4 == 0) //{ // blockLen = (byte)(i_totalBytes / 4); //} //else //{ // blockLen = (byte)(i_totalBytes / 4 + 1); //} blockLen = (byte)((i_totalBytes + 3) / 4); //calculate block number required, max number is 10 byte blockNumber = blockLen > (byte)10 ? (byte)10 : blockLen; //byte byteNumber = 0; byte[] readData = new byte[blockNumber * 4]; st = ISO15693Commands.rf_readblock(ReaderInfo.icdev, 0x22, blockIndex, blockNumber, tagUIDbyte, out rtnLen, readData); if (st == 0) { int leftDataLength = readBuffer.Length - byteIndex; int copyDataLength = leftDataLength > readData.Length ? readData.Length : leftDataLength; //if (b_readLengthData) //{ // Array.Copy(readData, 2, readBuffer, byteIndex, copyDataLength == readData.Length ? copyDataLength - 2 : copyDataLength); // //b_readLengthData = true; //} //else //{ Array.Copy(readData, 0, readBuffer, byteIndex, copyDataLength); //} } else { return(ErrorCode.ReadFail); } byteIndex += rtnLen; //if (b_readLengthData) //{ // byteIndex -= 2; // b_readLengthData = false; //} blockIndex += blockNumber; i_totalBytes -= rtnLen; System.Threading.Thread.Sleep(20); } return(ErrorCode.ReadSuccessful); } } catch (Exception) { return(ErrorCode.OtherException); } }