- implement write timeouts + tests
- added XON/XOFF constants
diff --git a/pyserial/serial/serialutil.py b/pyserial/serial/serialutil.py
index c503568..0d4883b 100644
--- a/pyserial/serial/serialutil.py
+++ b/pyserial/serial/serialutil.py
@@ -15,6 +15,9 @@
PARITY_ODD: 'Odd',
}
+XON = chr(16)
+XOFF = chr(18)
+
#Python < 2.2.3 compatibility
try:
True
@@ -27,6 +30,11 @@
portNotOpenError = SerialException('Port not open')
+class SerialTimeoutException(SerialException):
+ """Write timeouts give an exception"""
+
+writeTimeoutError = SerialTimeoutException("Write timeout")
+
class FileLike(object):
"""An abstract file like class.
@@ -113,6 +121,7 @@
timeout=None, #set a timeout value, None to wait forever
xonxoff=0, #enable software flow control
rtscts=0, #enable RTS/CTS flow control
+ writeTimeout=None, #set a timeout for writes
):
"""Initialize comm port object. If a port is given, then the port will be
opened immediately. Otherwise a Serial port object in closed state
@@ -125,16 +134,18 @@
self._parity = None #correct value is assigned below trough properties
self._stopbits = None #correct value is assigned below trough properties
self._timeout = None #correct value is assigned below trough properties
+ self._writeTimeout = None #correct value is assigned below trough properties
self._xonxoff = None #correct value is assigned below trough properties
self._rtscts = None #correct value is assigned below trough properties
- #assign values using get/set methods using the properties featrure
+ #assign values using get/set methods using the properties feature
self.port = port
self.baudrate = baudrate
self.bytesize = bytesize
self.parity = parity
self.stopbits = stopbits
self.timeout = timeout
+ self.writeTimeout = writeTimeout
self.xonxoff = xonxoff
self.rtscts = rtscts
@@ -263,7 +274,26 @@
"""Get the current timeout setting."""
return self._timeout
- timeout = property(getTimeout, setTimeout, "Timeout setting")
+ timeout = property(getTimeout, setTimeout, "Timeout setting for read()")
+
+
+ def setWriteTimeout(self, timeout):
+ """Change timeout setting."""
+ if timeout is not None:
+ if timeout < 0: raise ValueError("Not a valid timeout: %r" % timeout)
+ try:
+ timeout + 1 #test if it's a number, will throw a TypeError if not...
+ except TypeError:
+ raise ValueError("Not a valid timeout: %r" % timeout)
+
+ self._writeTimeout = timeout
+ if self._isOpen: self._reconfigurePort()
+
+ def getWriteTimeout(self):
+ """Get the current timeout setting."""
+ return self._writeTimeout
+
+ writeTimeout = property(getWriteTimeout, setWriteTimeout, "Timeout setting for write()")
def setXonXoff(self, xonxoff):