From 1e6b5960285500ba044a18b059ea70ada121a7f2 Mon Sep 17 00:00:00 2001 From: cliechti Date: Fri, 11 Oct 2013 02:27:30 +0000 Subject: [Bug pyserial:117] no error on lost conn w/socket:// => return empty string to signal EOF also fix reception of multiple bytes (extend vs append) git-svn-id: http://svn.code.sf.net/p/pyserial/code/trunk/pyserial@468 f19166aa-fa4f-0410-85c2-fa1106f25c8a --- CHANGES.txt | 1 + serial/urlhandler/protocol_socket.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 4945c15..5af51af 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -450,6 +450,7 @@ Bugfixes: - [Bug pyserial:145] Error in socket_connection.py - [Bug pyserial:135] reading from socket with timeout=None causes TypeError - [Bug pyserial:130] setup.py should not append py3k to package name +- [Bug pyserial:117] no error on lost conn w/socket:// Bugfixes (posix): diff --git a/serial/urlhandler/protocol_socket.py b/serial/urlhandler/protocol_socket.py index 0c96511..58eddd1 100644 --- a/serial/urlhandler/protocol_socket.py +++ b/serial/urlhandler/protocol_socket.py @@ -30,6 +30,7 @@ LOGGER_LEVELS = { 'error': logging.ERROR, } +POLL_TIMEOUT = 2 class SocketSerial(SerialBase): """Serial port implementation for plain sockets.""" @@ -46,13 +47,14 @@ class SocketSerial(SerialBase): if self._isOpen: raise SerialException("Port is already open.") try: + # XXX in future replace with create_connection (py >=2.6) self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._socket.connect(self.fromURL(self.portstr)) except Exception, msg: self._socket = None raise SerialException("Could not open port %s: %s" % (self.portstr, msg)) - self._socket.settimeout(2) # used for write timeout support :/ + self._socket.settimeout(POLL_TIMEOUT) # used for write timeout support :/ # not that there anything to configure... self._reconfigurePort() @@ -143,11 +145,16 @@ class SocketSerial(SerialBase): try: # an implementation with internal buffer would be better # performing... + t = time.time() block = self._socket.recv(size - len(data)) + duration = time.time() - t if block: - data.append(block) + data.extend(block) + else: + # no data -> EOF (connection probably closed) + break except socket.timeout: - # just need to get out of recv form time to time to check if + # just need to get out of recv from time to time to check if # still alive continue except socket.error, e: @@ -163,7 +170,8 @@ class SocketSerial(SerialBase): try: self._socket.sendall(data) except socket.error, e: - raise SerialException("socket connection failed: %s" % e) # XXX what exception if socket connection fails + # XXX what exception if socket connection fails + raise SerialException("socket connection failed: %s" % e) return len(data) def flushInput(self): -- cgit v1.2.1