CCID 设备通讯 (Windows 平台)

ccid 协议下载  

1. Windows提供了一套完整的接口,供ccid设备开发调用:

      头文件:Winscard.h 

lib库:   Winscard.lib

dll库: Winscard.dll

2. Winscard 提供的主要接口:

SCardEstablishContext //建立上下文
SCardListReaders			//检索所有的读卡器
SCardConnect					//连接设备
SCardTransmit					//设备传输
SCardDisconnect				//断开设备

这些接口MSDN介绍的比较详细。

3.  ccid简单的通讯的小例子 ccid_demo.cpp

ccid 设备通讯流程: 建立上下文--> 检索读卡器-->连接设备-->设备传输-->设备断开。根据这个流程,查MSDN相关接口,每个接口都附有实例片段,

下面这个实例就是,MSDN中的实例片段拼凑出来的,O(∩_∩)O~

// ccid_demo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Winscard.h>

void ccid_demo()
{
	SCARDCONTEXT    hSC;
	LONG            lReturn;
	// Establish the context.
	lReturn = SCardEstablishContext(SCARD_SCOPE_USER,
		NULL,
		NULL,
		&hSC);
	if ( SCARD_S_SUCCESS != lReturn )
		printf("Failed SCardEstablishContext\n");
	else
	{
		// Use the context as needed. When done,
		// free the context by calling SCardReleaseContext.
		// ...
	}

	/*
	** list reader
	*/
	LPTSTR          pmszReaders = NULL;
	LPTSTR          pReader;
	LONG            lReturn2;
	DWORD           cch = SCARD_AUTOALLOCATE;

	// Retrieve the list the readers.
	// hSC was set by a previous call to SCardEstablishContext.
	lReturn = SCardListReaders(hSC,
		NULL,
		(LPTSTR)&pmszReaders,
		&cch );
	switch( lReturn )
	{
	case SCARD_E_NO_READERS_AVAILABLE:
		printf("Reader is not in groups.\n");
		// Take appropriate action.
		// ...
		break;

	case SCARD_S_SUCCESS:
		// Do something with the multi string of readers.
		// Output the values.
		// A double-null terminates the list of values.
		pReader = pmszReaders;
		while ( '\0' != *pReader )
		{
			// Display the value.
			printf("Reader: %S\n", pReader );
			// Advance to the next value.
			pReader = pReader + wcslen(pReader) + 1;
		}
		// Free the memory.
		/*lReturn2 = SCardFreeMemory( hSC,
			pmszReaders );
		if ( SCARD_S_SUCCESS != lReturn2 )
			printf("Failed SCardFreeMemory\n");*/
		break;

	default:
		printf("Failed SCardListReaders\n");
		// Take appropriate action.
		// ...
		break;
	}

	SCARDHANDLE     hCardHandle;
	DWORD           dwAP;

	lReturn = SCardConnect( hSC, 
		pmszReaders,//(LPCTSTR)"Rainbow Technologies SCR3531 0",
		SCARD_SHARE_SHARED,
		SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
		&hCardHandle,
		&dwAP );
	if ( SCARD_S_SUCCESS != lReturn )
	{
		printf("Failed SCardConnect\n");
		exit(1);  // Or other appropriate action.
	}

	// Use the connection.
	// Display the active protocol.
	switch ( dwAP )
	{
	case SCARD_PROTOCOL_T0:
		printf("Active protocol T0\n"); 
		break;

	case SCARD_PROTOCOL_T1:
		printf("Active protocol T1\n"); 
		break;

	case SCARD_PROTOCOL_UNDEFINED:
	default:
		printf("Active protocol unnegotiated or unknown\n"); 
		break;
	}

	// Free the memory.
	lReturn2 = SCardFreeMemory( hSC,
		pmszReaders );
	if ( SCARD_S_SUCCESS != lReturn2 )
		printf("Failed SCardFreeMemory\n");


	#define COMMAND 					"\x00\x84\x00\x00\x08"
	unsigned char pbRecv[256]; 
	int dwRecv = sizeof(pbRecv); 
	SCARD_IO_REQUEST pioSendPci;
	pioSendPci.dwProtocol = dwAP;
	pioSendPci.cbPciLength = sizeof(SCARD_IO_REQUEST);
	//  Transmit the request.
	//  lReturn is of type LONG.
	//  hCardHandle was set by a previous call to SCardConnect.
	//  pbSend points to the buffer of bytes to send.
	//  dwSend is the DWORD value for the number of bytes to send.
	//  pbRecv points to the buffer for returned bytes.
	//  dwRecv is the DWORD value for the number of returned bytes.
	lReturn = SCardTransmit(hCardHandle,
		&pioSendPci,//SCARD_PCI_T0,
		(LPCBYTE)COMMAND,//pbSend,
		5,//dwSend,
		NULL,
		pbRecv,
		(LPDWORD)&dwRecv );
	if ( SCARD_S_SUCCESS != lReturn )
	{
		printf("Failed SCardTransmit\n");
		exit(1);   // or other appropriate error action
	}
	else
	{
	}


	// Remember to disconnect (by calling SCardDisconnect).
	// ...
	lReturn = SCardDisconnect(hCardHandle, 
		SCARD_LEAVE_CARD);
	if ( SCARD_S_SUCCESS != lReturn )
	{
		printf("Failed SCardDisconnect\n");
		exit(1);  // Or other appropriate action.
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	ccid_demo();
	return 0;
}

下载代码


    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值