/// <summary> /// 关闭设备 /// </summary> /// <returns></returns> public void CloseDevice() { if (hCard != 0) { runResult = PCSC.SCardDisconnect(hCard, 2); } if (hContext != 0) { runResult = PCSC.SCardReleaseContext(hContext); } }
/// <summary> /// 发送APDU指令 /// </summary> /// <param name="SendMessageBuffer">发送的指令</param> /// <param name="SW">返回的SW状态码</param> /// <returns></returns> public byte[] SendAPDU(byte[] SendMessageBuffer, byte[] SW) { byte[] receiveMessageByte = new byte[256]; int receiveLength = 256; runResult = PCSC.SCardTransmit(hCard , SCARD_PCI_T1 , SendMessageBuffer , SendMessageBuffer.Length , null, receiveMessageByte, ref receiveLength); if (runResult != SCARD_SUCCESS) { errMsg = "获取读卡器列表,错误代码为:" + runResult.ToString("X"); //Log.WriteLog(errMsg); throw new Exception(errMsg); } if (receiveLength > 2) { byte[] retMessage = new byte[receiveLength - 2]; for (int i = 0; i < receiveLength - 2; i++) { retMessage[i] = receiveMessageByte[i]; } SW[0] = receiveMessageByte[receiveLength - 2]; SW[1] = receiveMessageByte[receiveLength - 1]; return(retMessage); } if (receiveLength == 2) { SW[0] = receiveMessageByte[0]; SW[1] = receiveMessageByte[1]; return(null); } return(receiveMessageByte); }
/// <summary> /// 初始化设备 /// </summary> /// <returns>返回设备列表</returns> public List <string> InitializeDevice() { //获取设备上下文句柄 runResult = PCSC.SCardEstablishContext(0, 0, 0, ref hContext); if (runResult != SCARD_SUCCESS) { errMsg = "获取设备上下文失败,错误代码为:" + runResult.ToString("X"); //Log.WriteLog(errMsg); throw new Exception(errMsg); } if (hContext == 0) { errMsg = "未能获取设备上下文句柄"; // Log.WriteLog(errMsg); throw new Exception(errMsg); } int mszReaderSize = 0; //获取读卡器列表,第一次用于获取字符串长度 runResult = PCSC.SCardListReaders(hContext, null, null, ref mszReaderSize); if (runResult != SCARD_SUCCESS) { errMsg = "获取读卡器列表,错误代码为:" + runResult.ToString("X"); // Log.WriteLog(errMsg); throw new Exception(errMsg); } List <string> readerList = new List <string>(); byte[] reads = new byte[mszReaderSize]; //获取读卡器列表 runResult = PCSC.SCardListReaders(hContext, null, reads, ref mszReaderSize); if (runResult != SCARD_SUCCESS) { errMsg = "获取读卡器列表,错误代码为:" + runResult.ToString("X"); // Log.WriteLog(errMsg); throw new Exception(errMsg); } ASCIIEncoding encoding = new ASCIIEncoding(); string strBuffer = encoding.GetString(reads); char nullChar = '\0'; int len = mszReaderSize; int index = 0; while (strBuffer[0] != nullChar) { index = strBuffer.IndexOf(nullChar); string reader = strBuffer.Substring(0, index); len = len - (reader.Length + 1); strBuffer = strBuffer.Substring(index + 1, len); readerList.Add(reader); } return(readerList); }
/// <summary> /// 初始化卡 /// </summary> /// <param name="readerList">设备列表</param> /// <returns></returns> public void InitializeCard(List <string> readerList) { if (hContext == 0) { errMsg = "获取设备上下文句柄失败。初始化卡片前需初始化设备!"; // Log.WriteLog(errMsg); throw new Exception(errMsg); } bool isConnectCard = false; for (int i = 0; i < readerList.Count; i++) { string reader = readerList[i]; runResult = PCSC.SCardConnect(hContext, reader, 1, 2, ref hCard, ref ActiveProtocol); if (runResult != SCARD_SUCCESS) { continue; } else { activeReaderName = reader; isConnectCard = true; break; } } if (!isConnectCard) { errMsg = "未能搜索到卡片!"; // Log.WriteLog(errMsg); throw new Exception(errMsg); } int readerLength = 0; int cardStatus = 0; int cardProtocal = 0; int atrLength = 0; runResult = PCSC.SCardStatus(hCard, activeReaderName, ref readerLength , ref cardStatus, ref cardProtocal, null, ref atrLength); if (runResult != SCARD_SUCCESS) { errMsg = "获取读卡器列表,错误代码为:" + runResult.ToString("X"); //Log.WriteLog(errMsg); throw new Exception(errMsg); } if (cardStatus != 6) { errMsg = "卡片状态不合法!连接失败!"; //Log.WriteLog(errMsg); throw new Exception(errMsg); } byte[] ATR = new byte[atrLength]; runResult = PCSC.SCardStatus(hCard, activeReaderName, ref readerLength , ref cardStatus, ref cardProtocal, ATR, ref atrLength); if (runResult != SCARD_SUCCESS) { errMsg = "获取读卡器列表,错误代码为:" + runResult.ToString("X"); // Log.WriteLog(errMsg); throw new Exception(errMsg); } }