Created
September 13, 2013 02:47
-
-
Save cooncesean/6546311 to your computer and use it in GitHub Desktop.
pyserial write() method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Serial(object): | |
.... | |
def write(self, data): | |
"""Output the given string over the serial port.""" | |
if not self._isOpen: raise portNotOpenError | |
t = len(data) | |
d = data | |
if self._writeTimeout is not None and self._writeTimeout > 0: | |
timeout = time.time() + self._writeTimeout | |
else: | |
timeout = None | |
while t > 0: | |
try: | |
n = os.write(self.fd, d) | |
if timeout: | |
# when timeout is set, use select to wait for being ready | |
# with the time left as timeout | |
timeleft = timeout - time.time() | |
if timeleft < 0: | |
raise writeTimeoutError | |
_, ready, _ = select.select([], [self.fd], [], timeleft) | |
if not ready: | |
raise writeTimeoutError | |
d = d[n:] | |
t = t - n | |
except OSError, v: | |
if v.errno != errno.EAGAIN: | |
raise SerialException('write failed: %s' % (v,)) | |
return len(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment