Changeset 3537 for pykocard

Show
Ignore:
Timestamp:
04/21/10 02:17:15 (14 years ago)
Author:
jerome
Message:

Now opens the serial device and can write to it.

Files:
1 modified

Legend:

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

    r3536 r3537  
    2424import sys 
    2525 
     26import serial # On Debian/Ubuntu : apt-get install python-serial 
     27 
    2628class CartadisTCRS : 
    2729    """A class to manage Cartadis TCRS vending card readers. 
     
    3133       Cartadis is a registered trademark from Copie Monnaie France (C.M.F.) 
    3234    """ 
    33     def __init__(self, device, timeout, debug) : 
     35    def __init__(self, device, timeout=5.0, debug=False) : 
    3436        """Initializes the connection to the reader.""" 
    3537        self.device = device 
    3638        self.timeout = timeout 
    3739        self.debug = debug 
    38         self.serialport = None 
    3940 
     41        self.lastcommand = None 
    4042        self.tcrsprompt = chr(13) + chr(10) + '$' # the prompt 
    4143        self.eoc = chr(13) # end of command 
     
    5759        self.trnum = None 
    5860 
     61        # opens the connection to the reader 
     62        self.tcrs = serial.Serial(device, 
     63                                  baudrate=9600, 
     64                                  bytesize=serial.EIGHTBITS, 
     65                                  parity=serial.PARITY_NONE, 
     66                                  stopbits=serial.STOPBITS_ONE, 
     67                                  xonxoff=False, 
     68                                  rtscts=True, 
     69                                  timeout=timeout) 
     70 
     71        # cleans up any data waiting to be read 
     72        self.tcrs.flushInput() 
     73 
    5974    def __del__(self) : 
    6075        """Ensures the serial link is closed on deletion.""" 
     
    6378    def close(self) : 
    6479        """Closes the serial link if it is open.""" 
    65         if self.serialport is not None : 
    66             self.serialport.close() 
    67             self.serialport = None 
     80        if self.tcrs is not None : 
     81            self.tcrs.close() 
     82            self.tcrs = None 
    6883 
    6984    def logDebug(self, message) : 
     
    7388            sys.stderr.flush() 
    7489 
     90    def sendCommand(self, cmd, param=None) : 
     91        """Sends a command to the reader.""" 
     92        if param is not None : 
     93            command = "%s %s%s" % (cmd, param, self.eoc) 
     94        else : 
     95            command = "%s%s" % (cmd, self.eoc) 
     96        self.logDebug("Sending %s to reader" % repr(command)) 
     97        self.tcrs.write(command) 
     98        self.tcrs.flush() 
     99        self.lastcommand = command 
    75100 
     101