Changeset 3542

Show
Ignore:
Timestamp:
04/27/10 10:06:19 (14 years ago)
Author:
jerome
Message:

It mostly works now with a real Cartadis TCRS (Serial #68169). Time to
put some additional credits onto my card, otherwise I won't be able to
test for long :-)
TODO : improve robustness, and implement the few missing calls.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykocard/trunk/pykocard/cartadistcrs.py

    r3540 r3542  
    5151ERRADMINNOTALLOWED = -11 
    5252 
     53# Sensor 
     54SENSORNOCARD=0     # No card present 
     55SENSORUNKNOWN1=1   # Partially inside the TCRS 
     56SENSORCARDINSIDE=2 # Card is inside the TCRS 
     57SENSORUNKNOWN3=3   # Partially inside the TCRS 
    5358 
    5459class CartadisTCRS : 
     
    5964       Cartadis is a registered trademark from Copie Monnaie France (C.M.F.) 
    6065    """ 
    61     def __init__(self, device, timeout=5.0, debug=False) : 
     66    def __init__(self, device, timeout=1.0, debug=False) : 
    6267        """Initializes the connection to the TCRS.""" 
    6368        self.device = device 
     
    9095                                  timeout=timeout) 
    9196 
    92         # cleans up any data waiting to be read 
     97        # cleans up any data waiting to be read or written 
    9398        try : 
    9499            self.tcrs.flushInput() 
     100            self.tcrs.flushOutput() 
     101            self.tcrs.read(1) # Skips the first $ prompt 
    95102        except serial.serialutil.SerialException, msg : 
    96103            self.logError(msg) 
     
    100107            self.versionNumber = self.version() 
    101108            self.serialNumber = self.serial() 
    102             self.logDebug("%s terminal detected on device %s with serial number %s" \ 
     109            self.logDebug("%s TCRS detected on device %s with serial number %s" \ 
    103110                              % (self.versionNumber, 
    104111                                 self.device, 
    105112                                 self.serialNumber)) 
    106             self.supportedCommands = self.help() 
    107             self.logDebug("Supported commands : %s" % self.supportedCommands) 
    108113 
    109114    def __del__(self) : 
     
    147152            answer = self.tcrs.readline(eol=self.prompt) 
    148153            self.logDebug("TCRS answered %s" % repr(answer)) 
     154            if answer.startswith(command) : 
     155                answer = answer[len(command):] 
    149156            if answer.startswith(self.sol) and answer.endswith(self.prompt) : 
    150157                return answer[self.sollen:-self.promptlen] 
    151158            else : 
    152                 self.logError("Unknown answer %s" % repr(answer)) 
     159                if answer != self.sol : 
     160                    self.logError("Unknown answer %s" % repr(answer)) 
    153161                return None 
    154162        else : 
     
    169177    def read(self) : 
    170178        """Reads the card's content to the TCRS. Returns the type of card or an error value.""" 
    171         return int(self.sendCommand("read")) 
     179        return int(self.sendCommand("read") or -1) 
    172180 
    173181    def write(self) : 
     
    192200            return int(self.sendCommand("value")) 
    193201        else : 
    194             return self.sendCommand("value", value) 
    195  
    196     def account(self, account) : 
     202            return self.sendCommand("value", str(value)) 
     203 
     204    def account(self, account=None) : 
    197205        """Returns the last account number read, or sets the account number, but doesn't write it to the card yet.'""" 
    198206        if account is None : 
    199207            return int(self.sendCommand("account")) 
    200208        else : 
    201             return self.sendCommand("account", account) 
    202  
    203     def department(self, department) : 
     209            return self.sendCommand("account", str(account)) 
     210 
     211    def department(self, department=None) : 
    204212        """Returns the last department number read, or sets the department number, but doesn't write it to the card yet.'""" 
    205213        if department is None : 
    206214            return int(self.sendCommand("department")) 
    207215        else : 
    208             return self.sendCommand("department", department) 
    209  
    210     def group(self, group) : 
     216            return self.sendCommand("department", str(department)) 
     217 
     218    def group(self, group=None) : 
    211219        """Returns the last group number read, or sets the group number, but doesn't write it to the card yet.'""" 
    212220        if group is None : 
    213221            return int(self.sendCommand("group")) 
    214222        else : 
    215             return self.sendCommand("group", group) 
     223            return self.sendCommand("group", str(group)) 
    216224 
    217225    def addgrp(self, group=None) : 
    218226        """Adds the group to the list of allowed ones. If no group, the one on the admin card is used.""" 
    219         return int(self.sendCommand("addgrp", group)) 
     227        return int(self.sendCommand("addgrp", str(group))) 
    220228 
    221229    def listgrp(self) : 
    222230        """Returns the list of allowed group numbers.""" 
    223         return self.sendCommand("listgrp") 
     231        return [int(g) for g in self.sendCommand("listgrp").split()] 
    224232 
    225233    def delgrp(self, group) : 
    226234        """Deletes the group from the list of allowed groups.""" 
    227         return int(self.sendCommand("delgrp", group)) 
    228  
    229     def cardtype(self, cardtype) : 
     235        return int(self.sendCommand("delgrp", str(group))) 
     236 
     237    def cardtype(self, cardtype=None) : 
    230238        """Returns the type of card, or sets it (not clear in the doc if a write call is needed or not).""" 
    231         return int(self.sendCommand("cardtype", cardtype)) 
     239        # TODO : doesn't seem to return a meaningful answer 
     240        if cardtype is None : 
     241            answer = self.sendCommand("cardtype") 
     242        else : 
     243            answer = self.sendCommand("cardtype", str(cardtype)) 
     244        try : 
     245            return int(answer) 
     246        except ValueError : 
     247            self.logError("Unknown card type %s" % repr(answer)) 
     248            return None 
    232249 
    233250    def display(self, text) : 
     
    265282if __name__ == "__main__" : 
    266283    # Minimal testing 
    267     tcrs = CartadisTCRS("/dev/ttyS0", debug=True) 
    268     tcrs.help() 
    269     tcrs.close() 
     284    tcrs = CartadisTCRS("/dev/ttyS0", debug=False) 
     285    try : 
     286        sys.stdout.write("%s TCRS detected on device %s with serial number %s\n" \ 
     287                              % (tcrs.versionNumber, 
     288                                 tcrs.device, 
     289                                 tcrs.serialNumber)) 
     290 
     291 
     292        sys.stdout.write("This Cartadis TCRS supports the following commands :\n%s\n" % tcrs.help()) 
     293        sys.stdout.write("Allowed groups : %s\n" % tcrs.listgrp()) 
     294 
     295        import time 
     296        sys.stdout.write("Please insert your card into the TCRS...") 
     297        sys.stdout.flush() 
     298        while True : 
     299            cardpresent = tcrs.sensor() 
     300            tcrs.logDebug("Sensor Status : %i\n" % cardpresent) 
     301            if cardpresent == SENSORCARDINSIDE : 
     302                break 
     303            time.sleep(1.0) 
     304        sys.stdout.write("\n") 
     305 
     306        sys.stdout.write("Card read status : %s\n" % tcrs.read()) 
     307        sys.stdout.write("Group : %s\n" % tcrs.group()) 
     308        value = tcrs.value() 
     309        tcrs.display("Card has %s credits" % value) 
     310        sys.stdout.write("Card has %s credits\n" % value) 
     311        sys.stdout.write("Department : %s\n" % tcrs.department()) 
     312        sys.stdout.write("Account : %s\n" % tcrs.account()) 
     313        sys.stdout.write("Transaction # : %s\n" % tcrs.trnum()) 
     314        # 
     315        # This block commented out because I don't have many credits for testing ;-) 
     316        # It seems to work anyway. 
     317        # Now we decrement the number of credits 
     318        #tcrs.value(value-1) 
     319        # And we flush the card's content to the card 
     320        #sys.stdout.write("Card write status : %s\n" % tcrs.write()) 
     321        # Now we read it back 
     322        #tcrs.read() 
     323        #sys.stdout.write("Card now has %s credits\n" % tcrs.value()) 
     324    finally : 
     325        # We always do an eject, even if card not present 
     326        tcrs.eject() 
     327        tcrs.close()