sermsdos.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. import os
  38. import sys
  39. import string
  40. import serial.serialutil
  41. BAUD_RATES = {
  42. 110: "11",
  43. 150: "15",
  44. 300: "30",
  45. 600: "60",
  46. 1200: "12",
  47. 2400: "24",
  48. 4800: "48",
  49. 9600: "96",
  50. 19200: "19"}
  51. (PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK,
  52. PARITY_SPACE) = (0, 1, 2, 3, 4)
  53. (STOPBITS_ONE, STOPBITS_ONEANDAHALF,
  54. STOPBITS_TWO) = (1, 1.5, 2)
  55. FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5, 6, 7, 8)
  56. (RETURN_ERROR, RETURN_BUSY, RETURN_RETRY, RETURN_READY,
  57. RETURN_NONE) = ('E', 'B', 'P', 'R', 'N')
  58. portNotOpenError = ValueError('port not open')
  59. def device(portnum):
  60. return 'COM%d' % (portnum+1)
  61. class Serial(serialutil.FileLike):
  62. """
  63. port: number of device; numbering starts at
  64. zero. if everything fails, the user can
  65. specify a device string, note that this
  66. isn't portable any more
  67. baudrate: baud rate
  68. bytesize: number of databits
  69. parity: enable parity checking
  70. stopbits: number of stopbits
  71. timeout: set a timeout (None for waiting forever)
  72. xonxoff: enable software flow control
  73. rtscts: enable RTS/CTS flow control
  74. retry: DOS retry mode
  75. """
  76. def __init__(self,
  77. port,
  78. baudrate = 9600,
  79. bytesize = EIGHTBITS,
  80. parity = PARITY_NONE,
  81. stopbits = STOPBITS_ONE,
  82. timeout = None,
  83. xonxoff = 0,
  84. rtscts = 0,
  85. retry = RETURN_RETRY
  86. ):
  87. if type(port) == type(''):
  88. # strings are taken directly
  89. self.portstr = port
  90. else:
  91. # numbers are transformed to a string
  92. self.portstr = device(port+1)
  93. self.baud = BAUD_RATES[baudrate]
  94. self.bytesize = str(bytesize)
  95. if parity == PARITY_NONE:
  96. self.parity = 'N'
  97. elif parity == PARITY_EVEN:
  98. self.parity = 'E'
  99. elif parity == PARITY_ODD:
  100. self.parity = 'O'
  101. elif parity == PARITY_MARK:
  102. self.parity = 'M'
  103. elif parity == PARITY_SPACE:
  104. self.parity = 'S'
  105. self.stop = str(stopbits)
  106. self.retry = retry
  107. self.filename = "sermsdos.tmp"
  108. self._config(self.portstr, self.baud, self.parity,
  109. self.bytesize, self.stop, self.retry, self.filename)
  110. def __del__(self):
  111. self.close()
  112. def close(self):
  113. pass
  114. def _config(self, port, baud, parity, data, stop, retry,
  115. filename):
  116. comString = string.join(("MODE ", port, ":"
  117. , " BAUD= ", baud, " PARITY= ", parity
  118. , " DATA= ", data, " STOP= ", stop, " RETRY= ",
  119. retry, " > ", filename ), '')
  120. os.system(comString)
  121. def setBaudrate(self, baudrate):
  122. self._config(self.portstr, BAUD_RATES[baudrate],
  123. self.parity, self.bytesize, self.stop, self.retry,
  124. self.filename)
  125. def inWaiting(self):
  126. """returns the number of bytes waiting to be read"""
  127. raise NotImplementedError
  128. def read(self, num = 1):
  129. """Read num bytes from serial port"""
  130. handle = os.open(self.portstr,
  131. os.O_RDONLY | os.O_BINARY)
  132. rv = os.read(handle, num)
  133. os.close(handle)
  134. return rv
  135. def write(self, s):
  136. """Write string to serial port"""
  137. handle = os.open(self.portstr,
  138. os.O_WRONLY | os.O_BINARY)
  139. rv = os.write(handle, s)
  140. os.close(handle)
  141. return rv
  142. def flushInput(self):
  143. raise NotImplementedError
  144. def flushOutput(self):
  145. raise NotImplementedError
  146. def sendBreak(self):
  147. raise NotImplementedError
  148. def setRTS(self,level=1):
  149. """Set terminal status line"""
  150. raise NotImplementedError
  151. def setDTR(self,level=1):
  152. """Set terminal status line"""
  153. raise NotImplementedError
  154. def getCTS(self):
  155. """Eead terminal status line"""
  156. raise NotImplementedError
  157. def getDSR(self):
  158. """Eead terminal status line"""
  159. raise NotImplementedError
  160. def getRI(self):
  161. """Eead terminal status line"""
  162. raise NotImplementedError
  163. def getCD(self):
  164. """Eead terminal status line"""
  165. raise NotImplementedError
  166. def __repr__(self):
  167. return string.join(( "<Serial>: ", self.portstr
  168. , self.baud, self.parity, self.bytesize, self.stop,
  169. self.retry , self.filename), ' ')
  170. if __name__ == '__main__':
  171. s = Serial(0)
  172. sys.stdio.write('%s %s\n' % (__name__, s))