summaryrefslogtreecommitdiff
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
commit5d66a95bf70c87906fa4dcba8635f46a07680d85 (patch)
treea6137d06456a8ff3a2260bb218ae07d132a7b438
parent5746be32b9926bb4455ee4152c85084fd0942611 (diff)
downloadpyserial-git-5d66a95bf70c87906fa4dcba8635f46a07680d85.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)
-rw-r--r--pyserial/CHANGES.txt1
-rw-r--r--pyserial/serial/urlhandler/protocol_socket.py16
2 files changed, 13 insertions, 4 deletions
diff --git a/pyserial/CHANGES.txt b/pyserial/CHANGES.txt
index 4945c15..5af51af 100644
--- a/pyserial/CHANGES.txt
+++ b/pyserial/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/pyserial/serial/urlhandler/protocol_socket.py b/pyserial/serial/urlhandler/protocol_socket.py
index 0c96511..58eddd1 100644
--- a/pyserial/serial/urlhandler/protocol_socket.py
+++ b/pyserial/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):