Skip to content

Commit

Permalink
Merge pull request #109 from Galvant/binblockread_retries
Browse files Browse the repository at this point in the history
Binblockread retries
  • Loading branch information
scasagrande committed Apr 27, 2016
2 parents af292a1 + 41988b1 commit a169ddd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 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.1"
__version__ = "0.0.2"

__title__ = "instrumentkit"
__description__ = "Test and measurement communication library"
Expand Down
15 changes: 14 additions & 1 deletion instruments/abstract_instruments/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,20 @@ def binblockread(self, data_width, fmt=None):

# Read in the data bytes, and pass them to numpy using the specified
# data type (format).
return np.frombuffer(self._file.read_raw(num_of_bytes), dtype=fmt)
# This is looped in case a communication timeout occurs midway
# through transfer and multiple reads are required
tries = 3
data = self._file.read_raw(num_of_bytes)
while len(data) < num_of_bytes:
old_len = len(data)
data += self._file.read_raw(num_of_bytes - old_len)
if old_len == len(data):
tries -= 1
if tries == 0:
raise IOError("Did not read in the required number of bytes"
"during binblock read. Got {}, expected "
"{}".format(len(data), num_of_bytes))
return np.frombuffer(data, dtype=fmt)

# CLASS METHODS #

Expand Down
38 changes: 36 additions & 2 deletions instruments/tests/test_base_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

from builtins import bytes

# pylint: disable=unused-import
from nose.tools import raises
import quantities as pq
import mock

import numpy as np

Expand All @@ -21,6 +20,8 @@

# TESTS ######################################################################

# pylint: disable=no-member,protected-access


def test_instrument_binblockread():
with expected_protocol(
Expand All @@ -32,3 +33,36 @@ def test_instrument_binblockread():
sep="\n"
) as inst:
np.testing.assert_array_equal(inst.binblockread(2), [0, 1, 2, 3, 4])


def test_instrument_binblockread_two_reads():
inst = ik.Instrument.open_test()
data = bytes.fromhex("00000001000200030004")
inst._file.read_raw = mock.MagicMock(
side_effect=[b"#", b"2", b"10", data[:6], data[6:]]
)

np.testing.assert_array_equal(inst.binblockread(2), [0, 1, 2, 3, 4])

calls_expected = [1, 1, 2, 10, 4]
calls_actual = [call[0][0] for call in inst._file.read_raw.call_args_list]
np.testing.assert_array_equal(calls_expected, calls_actual)


@raises(IOError)
def test_instrument_binblockread_too_many_reads():
inst = ik.Instrument.open_test()
data = bytes.fromhex("00000001000200030004")
inst._file.read_raw = mock.MagicMock(
side_effect=[b"#", b"2", b"10", data[:6], b"", b"", b""]
)

_ = inst.binblockread(2)


@raises(IOError)
def test_instrument_binblockread_bad_block_start():
inst = ik.Instrument.open_test()
inst._file.read_raw = mock.MagicMock(return_value=b"@")

_ = inst.binblockread(2)

0 comments on commit a169ddd

Please sign in to comment.