Skip to content

Commit

Permalink
Change get_hardcopy to use read_raw and update test.
Browse files Browse the repository at this point in the history
Test still worked, but now we can use hypothesis to simulate binary
data.
  • Loading branch information
trappitsch committed Sep 11, 2020
1 parent 21ffafa commit 4f5d6d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
5 changes: 2 additions & 3 deletions instruments/tektronix/tektds5xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,12 @@ def get_hardcopy(self):
self.sendcmd('HARDC:PORT GPI;HARDC:LAY PORT;:HARDC:FORM BMP')
self.sendcmd('HARDC START')
time.sleep(1)
header = self.query("", size=54)
header = header.encode("utf-8")
header = self._file.read_raw(size=54)
# Get BMP Length in kilobytes from DIB header, because file header is
# bad
length = reduce(
operator.mul, struct.unpack('<iihh', header[18:30])) / 8
length = int(length) + 8 # Add 8 bytes for our monochrome colour table
data = header + self.query("", size=length).encode("utf-8")
data = header + self._file.read_raw(size=length)
self._file.flush_input() # Flush input buffer
return data
23 changes: 12 additions & 11 deletions instruments/tests/test_tektronix/test_tktds5xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,30 +821,31 @@ def test_display_clock_value_error():
assert err_msg == f"Expected bool but got {type(wrong_type)} instead"


def test_get_hardcopy(mocker):
"""Transfer image (?) data in binary from the instrument.
@given(data=st.binary(min_size=1, max_size=2147483647))
def test_get_hardcopy(mocker, data):
"""Transfer data in binary from the instrument.
Data is at least 1 byte long, then we need to add 8 for the
color table apparently.
color table.
Fake the header of the data such that in byte 18:30 are 4 factorial
packed as '<iihh' that multiplied together result in the length of
the total data.
Take some random data, then stick a header to it.
Unchecked entries in header are filled with zeros.
the total data. Limit maximum size of binary such that we don't have
to factor the length and such that it simply fits into 4 bytes
unsigned.
Take some random data, then stick a header to it. Unchecked entries
in header are filled with zeros.
Mocking out sleep to do nothing.
"""
# mock out time
mocker.patch.object(time, 'sleep', return_value=None)

# make data
data = bytes("1349703492734019837", "utf-8") # some data
length_data = (len(data) - 8) * 8 # subtract header and color table
# make a fake header, fill with zeros, but length of data
# must be at position 18:30 in factorial notation
# make a fake header
header = struct.pack('<ddhiihhddd', 0, 0, 0, length_data, 1, 1, 1, 0, 0,
0)
# compile data to compare
data_expected = header + data.decode().encode()
# stick header and data together
data_expected = header + data

with expected_protocol(
ik.tektronix.TekTDS5xx,
Expand Down

0 comments on commit 4f5d6d0

Please sign in to comment.