2

两周以来,我一直致力于读取有源 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 对我没有帮助,而且代码项目上的代码对我来说也很复杂。

我将非常感谢您的帮助!

弗雷德里克

4

1 回答 1

1

I would suggest that you use the following definition for the SCardTransmit function:

[DllImport("winscard.dll")]
private static extern int SCardTransmit(IntPtr hCard,
                                       IntPtr pioSendPci,
                                       byte[] pbSendBuffer,
                                       int cbSendLength,
                                       IntPtr pioRecvPci,
                                       byte[] pbRecvBuffer,
                                       ref int pcbRecvLength);

Then you could use it in a wrapper method like this:

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;
}

Regarding the ATR: Your card is a contactless card, so it does not have a real ATR. However, PC/SC emulates an ATR for contactless cards. You can get it using the SCardGetStatusChange function.

Regarding communication with the active tag, I suggest you read the NFC Forum's Type 4 Tag Operation specification (and possibly the relevant parts of the Digital Protocol specification too). You can get these specifications from the NFC Forum's website.

于 2014-02-12T11:07:08.153 回答