summaryrefslogtreecommitdiff
path: root/serial
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2013-10-11 02:27:30 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2013-10-11 02:27:30 +0000
commit1e6b5960285500ba044a18b059ea70ada121a7f2 (patch)
treed6b3bfca0961b37b013b0b72ee061108e0f83675 /serial
parent6de9e95339c3b4c2c5f05803cb935995cccaf243 (diff)
downloadpyserial-1e6b5960285500ba044a18b059ea70ada121a7f2.tar.gz
[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
Diffstat (limited to 'serial')
-rw-r--r--serial/urlhandler/protocol_socket.py16
1 files changed, 12 insertions, 4 deletions
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):