Home | History | Annotate | Download | only in serial
      1 # sermsdos.py
      2 #
      3 # History:
      4 #
      5 #   3rd September 2002                      Dave Haynes
      6 #   1. First defined
      7 #
      8 # Although this code should run under the latest versions of
      9 # Python, on DOS-based platforms such as Windows 95 and 98,
     10 # it has been specifically written to be compatible with
     11 # PyDOS, available at:
     12 # http://www.python.org/ftp/python/wpy/dos.html
     13 #
     14 # PyDOS is a stripped-down version of Python 1.5.2 for
     15 # DOS machines. Therefore, in making changes to this file,
     16 # please respect Python 1.5.2 syntax. In addition, please
     17 # limit the width of this file to 60 characters.
     18 #
     19 # Note also that the modules in PyDOS contain fewer members
     20 # than other versions, so we are restricted to using the
     21 # following:
     22 #
     23 # In module os:
     24 # -------------
     25 # environ, chdir, getcwd, getpid, umask, fdopen, close,
     26 # dup, dup2, fstat, lseek, open, read, write, O_RDONLY,
     27 # O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_EXCL, O_TRUNC,
     28 # access, F_OK, R_OK, W_OK, X_OK, chmod, listdir, mkdir,
     29 # remove, rename, renames, rmdir, stat, unlink, utime,
     30 # execl, execle, execlp, execlpe, execvp, execvpe, _exit,
     31 # system.
     32 #
     33 # In module os.path:
     34 # ------------------
     35 # curdir, pardir, sep, altsep, pathsep, defpath, linesep.
     36 #
     37 
     38 import os
     39 import sys
     40 import string
     41 import serial.serialutil
     42 
     43 BAUD_RATES = {
     44                 110: "11",
     45                 150: "15",
     46                 300: "30",
     47                 600: "60",
     48                 1200: "12",
     49                 2400: "24",
     50                 4800: "48",
     51                 9600: "96",
     52                 19200: "19"}
     53 
     54 (PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK,
     55 PARITY_SPACE) = (0, 1, 2, 3, 4)
     56 (STOPBITS_ONE, STOPBITS_ONEANDAHALF,
     57 STOPBITS_TWO) = (1, 1.5, 2)
     58 FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5, 6, 7, 8)
     59 (RETURN_ERROR, RETURN_BUSY, RETURN_RETRY, RETURN_READY,
     60 RETURN_NONE) = ('E', 'B', 'P', 'R', 'N')
     61 portNotOpenError = ValueError('port not open')
     62 
     63 def device(portnum):
     64     return 'COM%d' % (portnum+1)
     65 
     66 class Serial(serialutil.FileLike):
     67     """
     68        port: number of device; numbering starts at
     69             zero. if everything fails, the user can
     70             specify a device string, note that this
     71             isn't portable any more
     72        baudrate: baud rate
     73        bytesize: number of databits
     74        parity: enable parity checking
     75        stopbits: number of stopbits
     76        timeout: set a timeout (None for waiting forever)
     77        xonxoff: enable software flow control
     78        rtscts: enable RTS/CTS flow control
     79        retry: DOS retry mode
     80     """
     81     def __init__(self,
     82                  port,
     83                  baudrate = 9600,
     84                  bytesize = EIGHTBITS,
     85                  parity = PARITY_NONE,
     86                  stopbits = STOPBITS_ONE,
     87                  timeout = None,
     88                  xonxoff = 0,
     89                  rtscts = 0,
     90                  retry = RETURN_RETRY
     91                  ):
     92 
     93         if type(port) == type(''):
     94         # strings are taken directly
     95             self.portstr = port
     96         else:
     97         # numbers are transformed to a string
     98             self.portstr = device(port+1)
     99 
    100         self.baud = BAUD_RATES[baudrate]
    101         self.bytesize = str(bytesize)
    102 
    103         if parity == PARITY_NONE:
    104             self.parity = 'N'
    105         elif parity == PARITY_EVEN:
    106             self.parity = 'E'
    107         elif parity == PARITY_ODD:
    108             self.parity = 'O'
    109         elif parity == PARITY_MARK:
    110             self.parity = 'M'
    111         elif parity == PARITY_SPACE:
    112             self.parity = 'S'
    113 
    114         self.stop = str(stopbits)
    115         self.retry = retry
    116         self.filename = "sermsdos.tmp"
    117 
    118         self._config(self.portstr, self.baud, self.parity,
    119         self.bytesize, self.stop, self.retry, self.filename)
    120 
    121     def __del__(self):
    122         self.close()
    123 
    124     def close(self):
    125         pass
    126 
    127     def _config(self, port, baud, parity, data, stop, retry,
    128         filename):
    129         comString = string.join(("MODE ", port, ":"
    130         , " BAUD= ", baud, " PARITY= ", parity
    131         , " DATA= ", data, " STOP= ", stop, " RETRY= ",
    132         retry, " > ", filename ), '')
    133         os.system(comString)
    134 
    135     def setBaudrate(self, baudrate):
    136         self._config(self.portstr, BAUD_RATES[baudrate],
    137         self.parity, self.bytesize, self.stop, self.retry,
    138         self.filename)
    139 
    140     def inWaiting(self):
    141         """returns the number of bytes waiting to be read"""
    142         raise NotImplementedError
    143 
    144     def read(self, num = 1):
    145         """Read num bytes from serial port"""
    146         handle = os.open(self.portstr,
    147         os.O_RDONLY | os.O_BINARY)
    148         rv = os.read(handle, num)
    149         os.close(handle)
    150         return rv
    151 
    152     def write(self, s):
    153         """Write string to serial port"""
    154         handle = os.open(self.portstr,
    155         os.O_WRONLY | os.O_BINARY)
    156         rv = os.write(handle, s)
    157         os.close(handle)
    158         return rv
    159 
    160     def flushInput(self):
    161         raise NotImplementedError
    162 
    163     def flushOutput(self):
    164         raise NotImplementedError
    165 
    166     def sendBreak(self):
    167         raise NotImplementedError
    168 
    169     def setRTS(self,level=1):
    170         """Set terminal status line"""
    171         raise NotImplementedError
    172 
    173     def setDTR(self,level=1):
    174         """Set terminal status line"""
    175         raise NotImplementedError
    176 
    177     def getCTS(self):
    178         """Eead terminal status line"""
    179         raise NotImplementedError
    180 
    181     def getDSR(self):
    182         """Eead terminal status line"""
    183         raise NotImplementedError
    184 
    185     def getRI(self):
    186         """Eead terminal status line"""
    187         raise NotImplementedError
    188 
    189     def getCD(self):
    190         """Eead terminal status line"""
    191         raise NotImplementedError
    192 
    193     def __repr__(self):
    194         return string.join(( "<Serial>: ", self.portstr
    195         , self.baud, self.parity, self.bytesize, self.stop,
    196         self.retry , self.filename), ' ')
    197 
    198 if __name__ == '__main__':
    199     s = Serial(0)
    200     sys.stdio.write('%s %s\n' % (__name__, s))
    201