Skip to content

Commit

Permalink
Fix visa readbytes (#192)
Browse files Browse the repository at this point in the history
* Replace deprecated ask method for query in visa session.

* Update gitignore and add test requirements

* Replace visa read with with read_bytes

* Add encode parameter to instrument.read()

- Introduce unpacking of IEEE-754/64 double real numbers.

* Add newest pyvisa package to requirements.txt

- Removed extras_require from setup.py

* Update read mock call with utf-8

* Replace exception NotImplemented with NotImplementedError

* Change read method signature on pm100usb

* Revert change to _extras_require_

* Add pyvisa 1.9.x install requirement

* Fix pyvisa requirement

* Replace NotImplementedError with ValueError on abstract_comm

* Loosen pyvisa versioning requirements

* Use codecs module to determine valid builtin encode types
  • Loading branch information
scasagrande committed Feb 7, 2019
1 parent b5fa44b commit 3d9175d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Thumbs.db
## Build directories ##
doc/_build

## Venv
.venv/


## setup.py generated files ##
MANIFEST

Expand All @@ -20,6 +24,7 @@ MANIFEST
*.so

# Packages
.egg/
*.egg
*.egg-info
dist
Expand Down
11 changes: 10 additions & 1 deletion instruments/abstract_instruments/comm/abstract_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from __future__ import unicode_literals

import abc
import codecs
import logging
import struct

from future.utils import with_metaclass

Expand Down Expand Up @@ -202,7 +204,14 @@ def read(self, size=-1, encoding="utf-8"):
:return: The read string from the connection
:rtype: `str`
"""
return self.read_raw(size).decode(encoding)
try:
codecs.lookup(encoding)
return self.read_raw(size).decode(encoding)
except LookupError:
if encoding == 'IEEE-754/64':
return struct.unpack('>d', self.read_raw(size))[0]
else:
raise ValueError("Encoding {} is not currently supported.".format(encoding))

def sendcmd(self, msg):
"""
Expand Down
9 changes: 3 additions & 6 deletions instruments/abstract_instruments/comm/visa_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,11 @@ def read_raw(self, size=-1):
:rtype: `bytes`
"""
if size >= 0:
while len(self._buf) < size:
data = self._conn.read()
if data == "":
break
self._buf += data
self._buf += self._conn.read_bytes(size)
msg = self._buf[:size]
# Remove the front of the buffer.
del self._buf[:size]

elif size == -1:
# Read the whole contents, appending the buffer we've already read.
msg = self._buf + self._conn.read()
Expand Down Expand Up @@ -193,4 +190,4 @@ def _query(self, msg, size=-1):
:rtype: `str`
"""
msg += self._terminator
return self._conn.ask(msg)
return self._conn.query(msg)
4 changes: 2 additions & 2 deletions instruments/abstract_instruments/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def query(self, cmd, size=-1):
)
return value

def read(self, size=-1):
def read(self, size=-1, encoding="utf-8"):
"""
Read the last line.
Expand All @@ -161,7 +161,7 @@ def read(self, size=-1):
connected instrument.
:rtype: `str`
"""
return self._file.read(size)
return self._file.read(size, encoding)

# PROPERTIES #

Expand Down
4 changes: 2 additions & 2 deletions instruments/tests/test_base_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,13 @@ def test_instrument_read():
inst._file.read.return_value = "foobar"

assert inst.read() == "foobar"
inst._file.read.assert_called_with(-1)
inst._file.read.assert_called_with(-1, 'utf-8')

inst._file = mock.MagicMock()
inst._file.read.return_value = "foobar"

assert inst.read(6) == "foobar"
inst._file.read.assert_called_with(6)
inst._file.read.assert_called_with(6, 'utf-8')


def test_instrument_write():
Expand Down
2 changes: 1 addition & 1 deletion instruments/thorlabs/pm100usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def averaging_count(self, newval):

})

def read(self, size=-1):
def read(self, size=-1, encoding='utf-8'):
"""
Reads a measurement from this instrument, according to its current
configuration mode.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
numpy
pyserial
pyvisa>=1.9
quantities>=0.12.1
future>=0.15
enum34
Expand Down
36 changes: 20 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
INSTALL_REQUIRES = [
"numpy",
"pyserial>=3.3",
"pyvisa>=1.9",
"quantities",
"enum34",
"future",
Expand All @@ -45,9 +46,7 @@
"pyusb",
"ruamel.yaml"
]
EXTRAS_REQUIRE = {
'VISA': ["pyvisa"]
}


# HELPER FUNCTONS ############################################################

Expand All @@ -62,6 +61,7 @@ def read(*parts):
with codecs.open(os.path.join(HERE, *parts), "rb", "utf-8") as f:
return f.read()


META_FILE = read(META_PATH)


Expand All @@ -77,18 +77,22 @@ def find_meta(meta):
return meta_match.group(1)
raise RuntimeError("Unable to find __{meta}__ string.".format(meta=meta))


# MAIN #######################################################################

if __name__ == "__main__":
setup(
name=find_meta("title"),
version=find_meta("version"),
url=find_meta("uri"),
author=find_meta("author"),
author_email=find_meta("email"),
packages=PACKAGES,
install_requires=INSTALL_REQUIRES,
extras_require=EXTRAS_REQUIRE,
description=find_meta("description"),
classifiers=CLASSIFIERS
)

setup(
name=find_meta("title"),
version=find_meta("version"),
url=find_meta("uri"),
author=find_meta("author"),
author_email=find_meta("email"),
packages=PACKAGES,
install_requires=INSTALL_REQUIRES,
tests_require=[
'pytest >= 2.9.1',
'hypothesis'
],
description=find_meta("description"),
classifiers=CLASSIFIERS
)

0 comments on commit 3d9175d

Please sign in to comment.