/// <summary> /// Read the bock at the specified address /// </summary> /// <returns> Bool to indicate success</returns> public static Boolean Mifair_Read_Block(Mifare_key KeyAB, int key_num, int block_num, out Byte[] BR) { byte[] retdata; BR = new byte[0]; if (MernokRFID.IsOpen()) { // send Read command if (KeyAB == Mifare_key.B) { key_num |= 0x80; } byte[] command = new byte[] { (byte)'R', (byte)block_num, (byte)key_num }; if (MernokRFID.SendRec(out retdata, command)) { if ((retdata.Length == 17) && ((retdata[0] == 0x86) || (retdata[0] == 0x96))) // Tag answer OK { BR = new byte[16]; Array.Copy(retdata, 1, BR, 0, 16); return(true); } else { return(false); } } else { return(false); } } else { MernokRFID.OpenRFID(Mode.Mifare); if (MernokRFID.IsOpen()) { return(Mifair_Read_Block(KeyAB, key_num, block_num, out BR)); } else { return(false); } } }
/// <summary> /// Write the block to a Mifare card at the specified address /// </summary> /// <returns> bool to indicate success</returns> public static Boolean Mifare_Write_Block(Mifare_key KeyAB, int key_num, int block_num, Byte[] data) { Byte[] BR = new Byte[19]; byte[] retdata; if (MernokRFID.IsOpen()) { // send Write command BR[0] = (byte)'W'; BR[1] = (byte)(block_num); BR[2] = (byte)((int)KeyAB | key_num); int len = Math.Min(16, data.Length); // copy as many bytes as possible. Array.Copy(data, 0, BR, 3, len); if (MernokRFID.SendRec(out retdata, BR)) { if ((retdata.Length == 1) && ((retdata[0] == 0x86) || (retdata[0] == 0x96))) // Tag answer OK { return(true); } else { return(false); } } else { return(false); } } else { MernokRFID.OpenRFID(Mode.Mifare); if (MernokRFID.IsOpen()) { return(Mifare_Write_Block(KeyAB, key_num, block_num, data)); } else { return(false); } } }