Skip to content

Commit

Permalink
Merge pull request #113 from Galvant/fix_read_timeout
Browse files Browse the repository at this point in the history
Fix read timeout
  • Loading branch information
scasagrande committed May 5, 2016
2 parents ab379d7 + 4b31ff3 commit 178be4f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion instruments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# In keeping with PEP-396, we define a version number of the form
# {major}.{minor}[.{postrelease}]{prerelease-tag}

__version__ = "0.0.3"
__version__ = "0.0.4"

__title__ = "instrumentkit"
__description__ = "Test and measurement communication library"
Expand Down
3 changes: 3 additions & 0 deletions instruments/abstract_instruments/comm/serial_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def read_raw(self, size=-1):
c = b''
while c != self._terminator.encode("utf-8"):
c = self._conn.read(1)
if c == b"":
raise IOError("Serial connection timed out before reading "
"a termination character.")
if c != self._terminator.encode("utf-8"):
result += c
return result
Expand Down
3 changes: 3 additions & 0 deletions instruments/abstract_instruments/comm/socket_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def read_raw(self, size=-1):
c = b''
while c != self._terminator.encode("utf-8"):
c = self._conn.recv(1)
if c == b"":
raise IOError("Socket connection timed out before reading "
"a termination character.")
if c != self._terminator.encode("utf-8"):
result += c
return result
Expand Down
5 changes: 4 additions & 1 deletion instruments/abstract_instruments/comm/visa_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ def read_raw(self, size=-1):
"""
if size >= 0:
while len(self._buf) < size:
self._buf += self._conn.read()
data = self._conn.read()
if data == "":
break
self._buf += data
msg = self._buf[:size]
# Remove the front of the buffer.
del self._buf[:size]
Expand Down
9 changes: 9 additions & 0 deletions instruments/tests/test_comm/test_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def test_serialcomm_read_raw():
comm._conn.read.assert_called_with(10)


@raises(IOError)
def test_serialcomm_read_raw_timeout():
comm = SerialCommunicator(serial.Serial())
comm._conn = mock.MagicMock()
comm._conn.read = mock.MagicMock(side_effect=[b"a", b"b", b""])

_ = comm.read_raw(-1)


def test_serialcomm_write_raw():
comm = SerialCommunicator(serial.Serial())
comm._conn = mock.MagicMock()
Expand Down
9 changes: 9 additions & 0 deletions instruments/tests/test_comm/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def test_socketcomm_read_raw():
comm._conn.recv.assert_called_with(10)


@raises(IOError)
def test_serialcomm_read_raw_timeout():
comm = SocketCommunicator(socket.socket())
comm._conn = mock.MagicMock()
comm._conn.recv = mock.MagicMock(side_effect=[b"a", b"b", b""])

_ = comm.read_raw(-1)


def test_socketcomm_write_raw():
comm = SocketCommunicator(socket.socket())
comm._conn = mock.MagicMock()
Expand Down

0 comments on commit 178be4f

Please sign in to comment.