两个星期以来,我一直致力于读取一个活跃的NFC TI芯片的标签和存储的链接。
我在读取ATR时也遇到了问题。
我的阅读器是Springcard H663。
我使用了winScard.dll,并成功地使用了SCardEstablishContext()和SCardConnect(),包括将它们作为包装器。
现在我有一个星期的问题,没有任何进展。
我不知道如何访问标记或存储的信息,也不知道应该为函数指定哪些值。
[StructLayout(LayoutKind.Sequential)]
public struct SCARD_IO_REQUEST
{
public int dwProtocol;
public int cbPciLength;
}和
[DllImport("WinScard.dll")]
public static extern int SCardTransmit(IntPtr hCard,
ref SCARD_IO_REQUEST pioSendPci,
ref byte[] pbSendBuffer,
int cbSendLength,
ref SCARD_IO_REQUEST pioRecvPci,
ref byte[] pbRecvBuffer,
ref int pcbRecvLength);是io结构和包装器的实现。
这是我尝试访问信息的方式:
internal static void _readtag(string readername)
{
IntPtr phCard = IntPtr.Zero;
IntPtr activeProtocoll = IntPtr.Zero;
try
{
get_readerconnection(readername,
(uint)PROTOCOL_T1,
phCard,
activeProtocoll); // SCardEstablishContext and SCardConnection
int Rueckgabewert;
SCARD_IO_REQUEST ioRequest = new SCARD_IO_REQUEST();
ioRequest.dwProtocol = (int)wrapper.PROTOCOL_T0;
ioRequest.cbPciLength = 8;
byte[] commandBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 };
byte[] reciveBytes = new byte[10];
int rcvLenght = 0;
Rueckgabewert = SCardTransmit(phCard,
ref ioRequest,
ref commandBytes,
commandBytes.Length,
ref ioRequest,
ref reciveBytes,
ref rcvLenght);
if (Rueckgabewert != 0)
Console.WriteLine("Failed querying tag UID: " + Error_in_String((uint)Rueckgabewert));
Console.WriteLine(Methoden.ByteToString(reciveBytes));
wrapper.SCardDisconnect(phCard, 0);
}
finally
{
wrapper.SCardReleaseContext(phCard);
}
return;为了完成,我向您展示了我的ByteToString函数:
public static string ByteToString(byte[] ByteArray)
{
ASCIIEncoding ascii = new ASCIIEncoding();
string multi_string = ascii.GetString(ByteArray);
return multi_string;
}还有我的get_readerconnection(...)函数:
internal static void get_readerconnection(string readername,
uint Reader_Protocol,
IntPtr phCard,
IntPtr ActiveProtocol)
{
try
{
phCard = IntPtr.Zero;
ActiveProtocol = IntPtr.Zero;
IntPtr hContext = get_hContext();
// using mode =2
// readerprotocol =3
int result = SCardConnect(hContext, readername, 2, Reader_Protocol, ref phCard, ref ActiveProtocol);
Error_in_String((uint)result);
}
catch
{
}
}我的问题是如何添加_readertag(..)函数来获取标签、链接或ATR。怎样才能显示结果呢?
我在网上搜索了一下,但大多数代码对我来说太高级了,MSDN对我没有帮助,而且代码项目的代码对我来说也太复杂了。
我将非常感谢你的帮助!
弗雷德里克
发布于 2014-02-12 19:07:08
我建议您对SCardTransmit函数使用以下定义:
[DllImport("winscard.dll")]
private static extern int SCardTransmit(IntPtr hCard,
IntPtr pioSendPci,
byte[] pbSendBuffer,
int cbSendLength,
IntPtr pioRecvPci,
byte[] pbRecvBuffer,
ref int pcbRecvLength);然后,您可以在包装器方法中使用它,如下所示:
public byte[] SCardTransmit(IntPtr hCard, int dwActiveProtocol,
byte[] sendData, int dataLen)
{
int pcbRecvLength = 255;
byte[] pbRecvBuffer = new byte[pcbRecvLength];
if (hCard == IntPtr.Zero)
throw new Exception("hCard = null");
IntPtr ioSend = IntPtr.Zero;
if (dwActiveProtocol == SCARD_PROTOCOL_T0)
{
IntPtr handle = LoadLibrary("winscard.dll");
IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci");
FreeLibrary(handle);
ioSend = pci;
}
else if (dwActiveProtocol == SCARD_PROTOCOL_T1)
{
IntPtr handle = LoadLibrary("winscard.dll");
IntPtr pci = GetProcAddress(handle, "g_rgSCardT1Pci");
FreeLibrary(handle);
ioSend = pci;
}
int res = SCardTransmit(hCard,
ioSend,
sendData, dataLen,
IntPtr.Zero,
pbRecvBuffer, ref pcbRecvLength);
if (res != 0)
throw new Exception("transmit error");
byte[] answer = new byte[pcbRecvLength];
Array.Copy(pbRecvBuffer, answer, pcbRecvLength);
return answer;
}关于ATR:您的卡是非接触式卡,所以它没有真正的ATR。但是,PC/SC为非接触式卡模拟ATR。您可以使用SCardGetStatusChange函数获取它。
关于与主动标签的通信,我建议您阅读NFC论坛的Type4标签操作规范(可能还有数字协议规范的相关部分)。您可以从NFC Forum's website获得这些规范。
https://stackoverflow.com/questions/21635447
复制相似问题