增加pboc 规范中电子现金相关接口
增加 decard 模块 with 支持
diff --git a/tcutils/pyDecard.py b/tcutils/pyDecard.py
index 55774e6..5821a4f 100644
--- a/tcutils/pyDecard.py
+++ b/tcutils/pyDecard.py
@@ -6,6 +6,7 @@
'''
from ctypes import *
import codecs
+import traceback
def _b(v):
@@ -48,10 +49,14 @@
py_decard.error_method = kwdargs["error_method"]
####################################################################
- def __init__(self):
+ def __init__(self, port=None, baud=None):
py_decard._do_load_dll()
self.card_mode = 'contactless'
self.ic_port = 0
+ if port:
+ if not self.open_port(port, baud):
+ raise RuntimeError(u"不能打开读卡器port=%d,baud=%d" % (port, baud))
+ self.beep()
def set_card_mode(self, mode='cotactless', icport=-1):
self.card_mode = mode
@@ -160,7 +165,7 @@
if ret != 0:
command_header = codecs.encode(command, 'hex')
return self.error('CPUCard apdu command : %s ,ret=%d' % (command_header, ret))
- return response.raw[:ord(resplen.value)]
+ return response.raw[:ord(resplen.value)]
def cpucard_apdu(self, command):
current_resp = self._do_apdu(command)
@@ -187,7 +192,7 @@
if ret != 0:
command_header = codecs.encode(command, 'hex')
return self.error('CPUCard apdu command : %s ,ret=%d' % (command_header, ret))
- return response.raw[:ord(resplen.value)]
+ return response.raw[:ord(resplen.value)]
def sam_apdu(self, command):
current_resp = self._do_sam_apdu(command)
@@ -236,3 +241,18 @@
if r != 0:
return False
return True
+
+ def __enter__(self):
+ if not py_decard.device_handle:
+ raise RuntimeError(u"Device not open")
+ py_decard.setup(error_method=self.ERROR_RAISE_EXP)
+ return self
+
+ def __exit__(self, exc_type, exc_value, tb):
+ try:
+ self.close_port()
+ except:
+ pass
+ if exc_type:
+ traceback.print_exception(exc_type, exc_value, tb)
+ return True