private void SCardTransmit() { uint maxRecvDataLen = 256; var recvBuffer = new byte[maxRecvDataLen + 2]; var sendBuffer = new byte[] { 0xff, 0xca, 0x00, 0x00, 0x00 }; // ← IDmを取得するコマンド NfcApi.SCARD_IO_REQUEST ioRecv = new NfcApi.SCARD_IO_REQUEST(); ioRecv.cbPciLength = 255; int pcbRecvLength = recvBuffer.Length; int cbSendLength = sendBuffer.Length; IntPtr handle = NfcApi.LoadLibrary("Winscard.dll"); IntPtr pci = NfcApi.GetProcAddress(handle, "g_rgSCardT1Pci"); NfcApi.FreeLibrary(handle); uint ret = NfcApi.SCardTransmit(hCard, pci, sendBuffer, cbSendLength, ioRecv, recvBuffer, ref pcbRecvLength); if (ret != NfcConstant.SCARD_S_SUCCESS) { throw new ApplicationException("NFCカードへの送信に失敗しました。code = " + ret); } // 受信データからIDmを抽出する // recvBuffer = IDm + SW1 + SW2 (SW = StatusWord) // SW1 = 0x90 (144) SW1 = 0x00 (0) で正常だが、ここでは見ていない cardId = BitConverter.ToString(recvBuffer, 0, pcbRecvLength - 2); }
int transmit(IntPtr hCard, byte[] sendBuffer, byte[] recvBuffer) { NfcApi.SCARD_IO_REQUEST ioRecv = new NfcApi.SCARD_IO_REQUEST(); ioRecv.cbPciLength = 255; int pcbRecvLength = recvBuffer.Length; int cbSendLength = sendBuffer.Length; IntPtr SCARD_PCI_T1 = getPciT1(); uint ret = NfcApi.SCardTransmit(hCard, SCARD_PCI_T1, sendBuffer, cbSendLength, ioRecv, recvBuffer, ref pcbRecvLength); if (ret != NfcConstant.SCARD_S_SUCCESS) { throw new ApplicationException("カードへの送信に失敗しました。code = " + ret); } return(pcbRecvLength); // 受信したバイト数(recvBufferに受け取ったバイト数) }