- Timestamp:
- 04/27/10 10:06:19 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykocard/trunk/pykocard/cartadistcrs.py
r3540 r3542 51 51 ERRADMINNOTALLOWED = -11 52 52 53 # Sensor 54 SENSORNOCARD=0 # No card present 55 SENSORUNKNOWN1=1 # Partially inside the TCRS 56 SENSORCARDINSIDE=2 # Card is inside the TCRS 57 SENSORUNKNOWN3=3 # Partially inside the TCRS 53 58 54 59 class CartadisTCRS : … … 59 64 Cartadis is a registered trademark from Copie Monnaie France (C.M.F.) 60 65 """ 61 def __init__(self, device, timeout= 5.0, debug=False) :66 def __init__(self, device, timeout=1.0, debug=False) : 62 67 """Initializes the connection to the TCRS.""" 63 68 self.device = device … … 90 95 timeout=timeout) 91 96 92 # cleans up any data waiting to be read 97 # cleans up any data waiting to be read or written 93 98 try : 94 99 self.tcrs.flushInput() 100 self.tcrs.flushOutput() 101 self.tcrs.read(1) # Skips the first $ prompt 95 102 except serial.serialutil.SerialException, msg : 96 103 self.logError(msg) … … 100 107 self.versionNumber = self.version() 101 108 self.serialNumber = self.serial() 102 self.logDebug("%s terminaldetected on device %s with serial number %s" \109 self.logDebug("%s TCRS detected on device %s with serial number %s" \ 103 110 % (self.versionNumber, 104 111 self.device, 105 112 self.serialNumber)) 106 self.supportedCommands = self.help()107 self.logDebug("Supported commands : %s" % self.supportedCommands)108 113 109 114 def __del__(self) : … … 147 152 answer = self.tcrs.readline(eol=self.prompt) 148 153 self.logDebug("TCRS answered %s" % repr(answer)) 154 if answer.startswith(command) : 155 answer = answer[len(command):] 149 156 if answer.startswith(self.sol) and answer.endswith(self.prompt) : 150 157 return answer[self.sollen:-self.promptlen] 151 158 else : 152 self.logError("Unknown answer %s" % repr(answer)) 159 if answer != self.sol : 160 self.logError("Unknown answer %s" % repr(answer)) 153 161 return None 154 162 else : … … 169 177 def read(self) : 170 178 """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) 172 180 173 181 def write(self) : … … 192 200 return int(self.sendCommand("value")) 193 201 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) : 197 205 """Returns the last account number read, or sets the account number, but doesn't write it to the card yet.'""" 198 206 if account is None : 199 207 return int(self.sendCommand("account")) 200 208 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) : 204 212 """Returns the last department number read, or sets the department number, but doesn't write it to the card yet.'""" 205 213 if department is None : 206 214 return int(self.sendCommand("department")) 207 215 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) : 211 219 """Returns the last group number read, or sets the group number, but doesn't write it to the card yet.'""" 212 220 if group is None : 213 221 return int(self.sendCommand("group")) 214 222 else : 215 return self.sendCommand("group", group)223 return self.sendCommand("group", str(group)) 216 224 217 225 def addgrp(self, group=None) : 218 226 """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))) 220 228 221 229 def listgrp(self) : 222 230 """Returns the list of allowed group numbers.""" 223 return self.sendCommand("listgrp")231 return [int(g) for g in self.sendCommand("listgrp").split()] 224 232 225 233 def delgrp(self, group) : 226 234 """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) : 230 238 """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 232 249 233 250 def display(self, text) : … … 265 282 if __name__ == "__main__" : 266 283 # 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()