这两年Python这门开发语言火遍了大江南北,处处可以看到关于学习Python的广告。在平时了解客户需求的过程中也接触到了很多开发者正在准备使用Python来开发智能卡读写的相关功能,这里我们就不去追究为何有如此多的用户利用Python这门开发语言来实现智能卡读写操作的原因了。总之,有客户需求我们就应该积极响应。

关于Python集成开发环境这些过程我们就不一一赘述了,我相信各位使用Python的开发者对这些基础操作都比较熟悉。我们主要是介绍一下在LotusCardDriver.dll 这个动态链接库里面已经是支持了非接触式IC卡,CPU卡、社保卡,居民健康卡,二代证等多种功能,如何利用Python这个开发语言来实现这些功能?平时接触到的用户大多数是使用C#、java这类开发语言实现对非接触式IC卡,CPU卡、社保卡的阅读,用Python的近两年来开始多起来了。

首先,说明一下LotusCardDriver.dll 这个动态链接库有32位和64位版本,这里介绍64位版本的开发过程。首先,我们定义一个结构体,我喜欢这么称呼他,你们也可以用其他称呼。

import os
class LotusCardParamStruct(Structure): _fields_ = [ ("nCardType", c_int), ("arrCardNo", c_ubyte * 8),("nCardSize",c_int) ,("arrBuffer", c_ubyte * 64),("nBufferSize", c_int),("arrKeys", c_ubyte * 64),("nKeysSize", c_int),("arrCosResultBuffer", c_ubyte * 256),("unCosReultBufferLength", c_int),("arrCosSendBuffer", c_ubyte * 256),("unCosSendBufferLength", c_int)]
print(os.getcwd()+"\LotusCardDriver.dll")

导入LotusCardDriver.dll 动态链接库,注意,要导入64位的动态链接库。

#Objdll = windll.LoadLibrary("LotusCardDriver.dll")
Objdll = windll.LoadLibrary(os.getcwd()+"\LotusCardDriver.dll")
sttLotusCardParam = LotusCardParamStruct()
#int _stdcall LotusCardOpenDevice(char * pszDeviceName, int nVID, int nPID, LotusCardExtendReadWriteCallBack  pLotusCardExtendReadWriteCallBack);
#LotusHandle WINAPI LotusCardOpenDevice(char * pszDeviceName, int nVID, int nPID, int nUsbDeviceIndex, unsigned int unRecvTimeOut, LotusCardExtendReadWriteCallBack  pLotusCardExtendReadWriteCallBack);

如果采用的是支持网络TCP/IP通信的智能卡读写器,还需要输入读卡器IP地址,这个地址可以通过工具进行自定义配置。

#strServerIp = '192.168.1.252'
strServerIp = ''

如果采用的USB免驱或者RS232通信接口的智能卡读写器,这一步骤可以省略。

接下来是对非接触式IC卡和二代证阅读的范例了

Objdll.LotusCardOpenDevice.restype = c_longlong
Objdll.LotusCardGetCardNo.argtypes = [c_longlong, c_int,c_void_p]
Objdll.LotusCardLoadKey.argtypes = [c_longlong, c_int,c_int, c_void_p]
Objdll.LotusCardAuthentication.argtypes = [c_longlong, c_int,c_int, c_void_p]
Objdll.LotusCardRead.argtypes = [c_longlong, c_int, c_void_p]
Objdll.LotusCardWrite.argtypes = [c_longlong, c_int, c_void_p]
Objdll.LotusCardCloseDevice.argtypes = [c_longlong]
Objdll.LotusCardSetCardType.argtypes = [c_longlong,c_byte]
Objdll.LotusCardGetTwoGenerationIDCardNo.argtypes = [c_longlong,c_char_p,c_int]
hLotusCard = Objdll.LotusCardOpenDevice(strServerIp.encode('gb2312'),0,0,0,2000,0)
print(hLotusCard)
RT_ALL = 0x52
RT_NOT_HALT = 0x26
AM_A = 0x60
AM_B = 0x61
szTwoGenerationID =  bytes(64)
nRequestType = RT_NOT_HALT
bResult = 0
#input("wait input")
if -1 != hLotusCard:'''bResult = Objdll.LotusCardSetCardType(hLotusCard,0x42)if 1==bResult:print("LotusCardSetCardType OK")else:print("LotusCardSetCardType Error")sys.exit()bResult = Objdll.LotusCardGetTwoGenerationIDCardNo(hLotusCard,szTwoGenerationID, 64)if 1==bResult:print("LotusCardGetTwoGenerationIDCardNo OK %s" %(szTwoGenerationID))print(str(szTwoGenerationID, encoding="utf-8"))else:print("LotusCardGetTwoGenerationIDCardNo Error")exit()exit()'''bResult = Objdll.LotusCardGetCardNo(hLotusCard,nRequestType, byref(sttLotusCardParam))if 1==bResult:#print(sttLotusCardParam.arrCardNo)print("%d" %(len(sttLotusCardParam.arrCardNo)))#print("CardNo(HEX) %.2x%.2x%.2x%.2x" %(sttLotusCardParam.arrCardNo[0],sttLotusCardParam.arrCardNo[1],sttLotusCardParam.arrCardNo[2],sttLotusCardParam.arrCardNo[3]))print("CardNo(HEX) %.2X%.2X%.2X%.2X" %(sttLotusCardParam.arrCardNo[0],sttLotusCardParam.arrCardNo[1],sttLotusCardParam.arrCardNo[2],sttLotusCardParam.arrCardNo[3]))print("CardNo(HEX) %.2X%.2X%.2X%.2X" %(sttLotusCardParam.arrCardNo[3],sttLotusCardParam.arrCardNo[2],sttLotusCardParam.arrCardNo[1],sttLotusCardParam.arrCardNo[0]))else:print("LotusCardGetCardNo Error")if 1==bResult:#sttLotusCardParam.arrKeys = "\xff\xff\xff\xff\xff\xff"sttLotusCardParam.arrKeys[0] = 0xffsttLotusCardParam.arrKeys[1] = 0xffsttLotusCardParam.arrKeys[2] = 0xffsttLotusCardParam.arrKeys[3] = 0xffsttLotusCardParam.arrKeys[4] = 0xffsttLotusCardParam.arrKeys[5] = 0xffbResult =Objdll.LotusCardLoadKey(hLotusCard, AM_A, 0, byref(sttLotusCardParam))if 1==bResult:print("LotusCardLoadKey OK")else:print("LotusCardLoadKey Error")if 1==bResult:bResult =Objdll.LotusCardAuthentication(hLotusCard, AM_A, 0, byref(sttLotusCardParam))if 1==bResult:print("LotusCardAuthentication OK")else:print("LotusCardAuthentication Error")'''                if 1==bResult:bResult =Objdll.LotusCardRead(hLotusCard, 2, byref(sttLotusCardParam))if 1==bResult:print("LotusCardRead OK")#print(sttLotusCardParam.arrBuffer)print("ReadData(HEX):%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x" %(sttLotusCardParam.arrBuffer[0],sttLotusCardParam.arrBuffer[1],sttLotusCardParam.arrBuffer[2],sttLotusCardParam.arrBuffer[3],sttLotusCardParam.arrBuffer[4],sttLotusCardParam.arrBuffer[5],sttLotusCardParam.arrBuffer[6],sttLotusCardParam.arrBuffer[7],sttLotusCardParam.arrBuffer[8],sttLotusCardParam.arrBuffer[9],sttLotusCardParam.arrBuffer[10],sttLotusCardParam.arrBuffer[11],sttLotusCardParam.arrBuffer[12],sttLotusCardParam.arrBuffer[13],sttLotusCardParam.arrBuffer[14],sttLotusCardParam.arrBuffer[15]))else:print("LotusCardRead Error")'''if 1==bResult:sttLotusCardParam.arrBuffer[0] = 0x00sttLotusCardParam.arrBuffer[1] = 0x01sttLotusCardParam.arrBuffer[2] = 0x02sttLotusCardParam.arrBuffer[3] = 0x03sttLotusCardParam.arrBuffer[4] = 0x04sttLotusCardParam.arrBuffer[5] = 0x05sttLotusCardParam.arrBuffer[6] = 0x06sttLotusCardParam.arrBuffer[7] = 0x07sttLotusCardParam.arrBuffer[8] = 0x08sttLotusCardParam.arrBuffer[9] = 0x09sttLotusCardParam.arrBuffer[10] = 0x0AsttLotusCardParam.arrBuffer[11] = 0x0BsttLotusCardParam.arrBuffer[12] = 0x0CsttLotusCardParam.arrBuffer[13] = 0x0DsttLotusCardParam.arrBuffer[14] = 0x0EsttLotusCardParam.arrBuffer[15] = 0x0FsttLotusCardParam.nBufferSize = 16bResult =Objdll.LotusCardWrite(hLotusCard, 2, byref(sttLotusCardParam))if 1==bResult:print("LotusCardWrite OK")else:print("LotusCardWrite Error")Objdll.LotusCardCloseDevice(hLotusCard)else:print("LotusCardOpenDevice ERROR")
print(bResult)

下载链接中有详细代码,可以供参考和学习。由于时间匆忙,有遗漏和不足之处还请谅解。

利用Python对非接触式IC卡的读写操作相关推荐

  1. 磁条卡,接触式IC卡,非接触式IC卡的优缺点

    磁条卡的特点: 磁条卡由于其结构简单,存储容量小,安全保密性差,读写设备复杂且维护费用高,作为七.八十年代技术水平的产品已风光不再,即将面临淘汰. 智能IC卡与磁条卡相比有哪些优势? 接触式IC卡与磁 ...

  2. 非接触式IC卡与条码卡磁卡接触式IC卡相比较

    一.最新技术-非接触IC卡 非接触IC卡,又名感应卡,诞生于90年代初,由于存在着条码卡.磁卡和接触式IC卡不可比拟的优点,使之一经问世,便立刻引起了广泛的关注,并以惊人的速度得到推广应用. 非接触式 ...

  3. 非接触式IC卡、条码卡、磁道卡、接触式IC卡、芯片卡

    转自 http://blog.csdn.net/lvxiangan/article/details/40953831 非接触式IC卡:门禁卡.工卡 条码卡:较少接触到 磁道卡:带黑色磁条的卡,如可刷的 ...

  4. iso14443 非接触式IC卡协议

    转自百度百科 http://baike.baidu.com/link?url=Y1koEW6mX0AgDV5kt37i7M5RaV0g_WwxU163dO-VKUbR5w4qEHv0OMOnbw1BT ...

  5. 全球与中国非接触式IC卡芯片市场现状及未来发展趋势2022

    根据QYR(恒州博智)的统计及预测,2021年全球非接触式IC卡芯片市场销售额达到了 亿美元,预计2028年将达到 亿美元,年复合增长率(CAGR)为 %(2022-2028).地区层面来看,中国市场 ...

  6. S50非接触式IC卡 学习笔记

    S50非接触式IC卡性能简介(M1) 一. 主要指标 l 容量为8K位EEPROM(1K 字节) l 分为16个扇区,每个扇区为4块,每块16个字节,以块为存取单位 l 每个扇区有独立的一组密码及访问 ...

  7. 非接触式IC卡性能简介(M1)

    卡片的电气部分只由一个天线和ASIC组成. 天线:卡片的天线是只有几组绕线的线圈,很适于封装到IS0卡片中. ASIC:卡片的ASIC由一个高速(106KB波特率)的RF接口,一个控制单元和一个 8K ...

  8. 非接触式IC卡存储结构

    非接触式IC卡Contactless Smart Card(CSS),也称作感应卡.射频卡,由IC芯片.感应天线组成,并完全密封在一个标准PVC卡片中, 无外露部分,其结构示意图如下. 非接触IC卡工 ...

  9. Mifare One 非接触式IC卡介绍

    贴一篇关于Mifare1的介绍 Mifare l卡特性简介 M1卡为8K位的非接触式IC卡. 电气特性 l         容量为8K位EEPrOM l         分为16个扇区,每个扇区为4块 ...

  10. ID卡,非接触式IC卡,接触式IC卡,他们分别有那些芯片封装起来的

    1.ID卡 EM4100.EM4102.TK4100.TK28.TEMIC.ATMILE.TK9013.Temic E5551.T5557. T5567. 2.非接触式IC卡 FM11RF08. Ut ...

最新文章

  1. Arduino处理STM32中的多个串口通讯问题
  2. lighttpd防御 Slow HTTP Denial of Service Attack 解决办法
  3. sklearn中各算法类的fit,fit_transform和transform函数
  4. QML绘制不同类型的图表
  5. (*长期更新)软考网络工程师学习笔记——Section 8 传输层
  6. 一维和二维卷积和池化
  7. nditer —— numpy.ndarray 多维数组的迭代
  8. 【美赛】美赛字体格式和排版要求
  9. 板翅式换热器翅片表面传热与阻力特性性能分析
  10. C# 节约里程法实现
  11. android版 暴风影音,Android版暴风影音 掌上的3D影院
  12. linux单引号作用,单引号 双引号如何输入(Linux运维:单引号与双引号的使用)...
  13. 计算机其它离的360云盘,win7系统将360云盘文件快速转移到百度云盘的方法
  14. 他是“自由软件”之父,天才程序员,史上最伟大的黑客!最后却黯然离场
  15. 利用FPGA制作数字电压表
  16. 符合 V.25TER 的 AT 命令
  17. 区块链毕业设计必读论文【2020-5】
  18. H264码流中SPS的获取
  19. 初学51单片机制作智能小车心得
  20. Android进阶之光 读书笔记

热门文章

  1. 计算机水平cet2是什么等级,英语cet2等级考试试题
  2. PVE7 GPU卡直通
  3. 关于破解邮箱的一点心得
  4. property_get和property_set使用方法
  5. 【网站】比较知名的大型公司官网清单可以收藏关注一下,欢迎您来补充
  6. mysql中rownumber用法_MySQL中row_number的实现
  7. 腾讯云对象存储操作流程
  8. 美年旅游_自由行_自由行分页PageHelper
  9. 方舟手游服务器设置文件翻译,方舟生存进化单机模式设置中英文对照翻译一览...
  10. Mac 谷歌浏览器chrome恶意插件劫持Any search(TotalSearchToolbox)处理