⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardreaderwrapper.cs

📁 Smart Card communication include tx and rx
💻 CS
字号:
using System;
using System.Runtime.InteropServices;

namespace MifareReaderDemo
{
	/// <summary>
	/// Summary description for CardReaderWrapper.
	/// </summary>
	public class CardReaderWrapper
	{
		private static int hReader = 0;
		private CardReaderWrapper()
		{
		}

		[StructLayout(LayoutKind.Sequential)]
		struct CardID
		{
			[MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
			public byte[] Data;
		}
		[StructLayout(LayoutKind.Sequential)]
		struct LoadKey
		{
			public byte Sector;
			public byte Block;	
			public byte Keyab;
			[MarshalAs(UnmanagedType.ByValArray, SizeConst=40)]
			public byte[] Data;
			public byte RequestAll;
		}

		[DllImport("Mifare.dll")]
		private static extern int Mif_OpenPort(int iCom);
		[DllImport("Mifare.dll")]
		private static extern int Mif_ClosePort(int hReader);
		[DllImport("Mifare.dll")]
		private static extern int Mif_LEDOn(int hReader);
		[DllImport("Mifare.dll")]
		private static extern int Mif_BuzOn(int hReader);
		[DllImport("Mifare.dll")]
		private static extern int Mif_BuzOff(int hReader);
		[DllImport("Mifare.dll")]
		private static extern int Mif_LEDOff(int hReader);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Request(int hReader);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Anticol(int hReader, ref CardID id);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Select(int hReader, ref CardID id);
		[DllImport("Mifare.dll")]
		private static extern int Mif_DirectRead(int hReader, ref LoadKey key);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Halt(int hReader);

		[DllImport("Mifare.dll")]
		private static extern int Mif_RequestAll(int hReader);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Auth(int hReader, ref LoadKey Key);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Read(int hReader, ref LoadKey Key);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Write(int hReader, ref LoadKey Key);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Inc(int hReader, ref LoadKey Key);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Dec(int hReader, ref LoadKey Key);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Transfer(int hReader, ref LoadKey Key);
		[DllImport("Mifare.dll")]
		private static extern int Mif_Key(int hReader, ref LoadKey Key);


		public static bool OpenReader()
		{
			hReader = Mif_OpenPort(1);
	        if (hReader > 0) return true;
			hReader = Mif_OpenPort(2);
			if (hReader > 0) return true;
			return false;
		}
		public static bool CloseReader()
		{
			if (hReader != 0)	
				if (Mif_ClosePort(hReader) >= 0) return true;
			return false;
		}
		// Check if any card on the reader
		public static bool CheckCard()
		{
			int intReturn;
			intReturn = Mif_Request(hReader);
			if (intReturn != 1) return false;
			else return true;
		}
		public static string GetCardID()
		{
			string cardID = "";
			int intReturn;
			CardID id;
			id.Data = new byte[256];			
			intReturn = Mif_Anticol(hReader, ref id);
			if (intReturn != 1) return null;
			for (int i=0; i<4; i++)
				cardID = cardID + id.Data[i].ToString("X2");
			return cardID;
		}
		public static string RdOneBlock(byte Sector, byte Block)
		{
			//if (GetCardID() != null)
			if (true)
			{
				string data = "";
				LoadKey key;
				key.Keyab = 0;
				key.Block = Block;
				key.Sector = Sector;
				key.RequestAll = 0;
				key.Data = new byte[40];
				if (Mif_DirectRead(hReader, ref key) == 1)
				{
					for (int i=0; i<16; i++)
					{
						data = data + System.Convert.ToChar(key.Data[i]);
					}
					return data.Trim();
				}
			}
			return null;
		}
		public static void HaltReader()
		{
			Mif_Halt(hReader);
		}

		public static int WrOneBlock(byte Sector, byte Block,string data)
		{
			//if (GetCardID() != null)
			if (true)
			{

				CardID id;
				id.Data = new byte[256];	
		
				LoadKey key;
				key.Keyab = 0;
				key.Block = Block;
				key.Sector = Sector;
				key.RequestAll = 0;
				key.Data=new byte[40];

				if (data.Length>16)
				{
						return -1;
				}
				else
				{
					for (int i=0; i<data.Length; i++)
					{
						key.Data[i]= System.Convert.ToByte((data.ToCharArray())[i]);
					}
				}
				if (Mif_Request(hReader)!=1)
					return -2;

				if (Mif_Anticol(hReader, ref id)!=1)
					return -3;

				if (Mif_Select(hReader, ref id)!=1)
					return -4;

				if (Mif_Auth(hReader, ref key)!=1)
					return -5;

				if (Mif_Write(hReader, ref key) != 1)
					return -6;
				else
					return 1;
			}
		}

		public static string RdOneBlock(int number)
		{
			byte Sector= 0 , Block=0;
			if (number > 46 || number < 0)
				return "Error";

			if (number == 0 || number == 1)
			{
				Sector = 0;
				if (number == 0)
					Block = 1;
				else
					Block = 2;
			}
			else
			{
				Sector = (byte) ((number-2)/3 + 1);
				Block = (byte)(Sector*4 + number - ((Sector-1)*3+2));
			}
			return RdOneBlock(Sector, Block);
		}

		public static int WrOneBlock(int number, string data)
		{
			byte Sector= 0 , Block=0;
			if (number > 46 || number < 0)
				return -7;

			if (number == 0 || number == 1)
			{
				Sector = 0;
				if (number == 0)
					Block = 1;
				else
					Block = 2;
			}
			else
			{
				Sector = (byte)((number-2)/3 + 1);
				Block = (byte)(Sector*4 + number - ((Sector-1)*3+2));
			}


			return WrOneBlock(Sector, Block,data);
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -