pySIMserial.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:SIMreader 作者: stoic1979 项目源码 文件源码
def openSession(self, portname):
        self.serialport = serial.Serial(port=portname,
                                 parity=serial.PARITY_EVEN,
                                 bytesize=serial.EIGHTBITS,
                                 stopbits=serial.STOPBITS_TWO,
                                 timeout=1,
                                 xonxoff=0,
                                 rtscts=0,
                                 baudrate=9600)
        if (not self.serialport):
            return 1

        # reset it!
        #self.serialport.setRTS(0)
        self.serialport.setRTS(1)
        self.serialport.setDTR(1)
        time.sleep(0.01)  # 10ms?
        self.serialport.flushInput()
        #self.serialport.setRTS(1)
        self.serialport.setRTS(0)
        self.serialport.setDTR(0)

        ts = self.serialport.read()
        if ts == None:
            return 2             # no card?
        if ord(ts) != 0x3B:
            return 3             # bad ATR byte 
        # ok got 0x3B
        print "TS: 0x%x Direct convention" % ord(ts)

        t0 = chr(0x3B)
        while ord(t0) == 0x3b:
            t0 = self.serialport.read()

        if t0 == None:
            return 2
        print "T0: 0x%x" % ord(t0)

        # read interface bytes
        if (ord(t0) & 0x10):
            print "TAi = %x" % ord(self.serialport.read())
        if (ord(t0) & 0x20):
            print "TBi = %x" % ord(self.serialport.read())
        if (ord(t0) & 0x40):
            print "TCi = %x" % ord(self.serialport.read())
        if (ord(t0) & 0x80):
            tdi = self.serialport.read()
            print "TDi = %x" % ord(tdi)

        for i in range(0, ord(t0) & 0xF):
            x = self.serialport.read()
            print "Historical: %x" % ord(x)

        while 1:
            x = self.serialport.read()
            if (x == ""):
                break
            print "read: %x" % ord(x)

        return 0
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号