"""
Driver for the Stanford Research PRS10 Rubidium Clock
"""
import serial

class Clock:
    """
    Rubidium clock that can be used with an 1 pulse-per-second (1PPS) signal to
    keep it accurate. Controlled through serial (RS-232) interface
    """
    __ser = None # The private interface to the device

    def __init__(self, COM=0):
        """ Connecting to the device """
        comport = '/dev/ttyUSB%d' %(COM)  # This is Linux specific like this
        # Settings from the manual
        self.__ser = serial.Serial(comport,
                                   baudrate=9600,
                                   bytesize=8,
                                   stopbits=2,
                                   parity=serial.PARITY_NONE,
                                   xonxoff=1,
                                   timeout=1,
                                   )

    def write(self, message):
        """ Write to the device """
        self.__ser.write("%s\r" %message) # Adding the line-feed that is the command delimiter

    def read(self):
        """ Read one line from the device """
        return self.__ser.readline()

    def ask(self, query):
        """ Write a command and listen for answer """
        self.write(query)
        return self.read()

    def setupWithGPS(self):
        """ Reset settings suitable for 1PPS stabilization from GPS """
        clock.write("LM1") # Enable digital pre-filter for the incoming 1PPS signal from GPS
        clock.write("PT14") # Set longest PLL parameter for 1PPS input with bad short-term stability

if __name__ == "__main__":
    clock = Clock()
    print "Unit ID: %s" %(clock.ask("ID?"))

@@ -68,16 +68,26 @@
 t serial
+%0Aimport os
 %0A%0Aclass 
@@ -396,76 +396,223 @@
     
-comport = '/dev/ttyUSB%25d' %25(COM)  # This is Linux specific like this
+# Get serial port name on Linux or other (i.e. Windows) system%0A        if os.name == %22posix%22:%0A            portbase = '/dev/ttyUSB'%0A        else:%0A            portbase = 'COM'%0A        comport = '%25s%25d' %25(portbase, COM)
 %0A   
@@ -643,16 +643,33 @@
  manual%0A
+        try:%0A    
         
@@ -704,16 +704,20 @@
 omport,%0A
+    
         
@@ -793,16 +793,20 @@
         
+    
 bytesize
@@ -809,16 +809,20 @@
 size=8,%0A
+    
         
@@ -860,16 +860,20 @@
 bits=2,%0A
+    
         
@@ -961,16 +961,20 @@
         
+    
 xonxoff=
@@ -972,24 +972,28 @@
  xonxoff=1,%0A
+    
             
@@ -1061,16 +1061,134 @@
         
+   
+ )%0A        except (serial.serialutil.SerialException):%0A            raise IOError(%22Couldn't open port %25s%22 %25(comport)
 )%0A%0A    d
@@ -1912,23 +1912,228 @@
     
-clock = Clock()
+from sys import exit%0A    com = int(raw_input(%22Serial port number (the X in COMX or ttyUSBX): %22))%0A    try:%0A        clock = Clock(com)%0A    except IOError:%0A        print %22Port no good...%22%0A        exit(1)%0A%0A    # If all good:
 %0A   

add some exception handling