diff --git a/.gitignore b/.gitignore index 47f4499c9..c02194b2c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,10 @@ Thumbs.db ## Build directories ## doc/_build +## Venv +.venv/ + + ## setup.py generated files ## MANIFEST @@ -20,6 +24,7 @@ MANIFEST *.so # Packages +.egg/ *.egg *.egg-info dist diff --git a/instruments/abstract_instruments/comm/abstract_comm.py b/instruments/abstract_instruments/comm/abstract_comm.py index e15547760..b29b5e97d 100644 --- a/instruments/abstract_instruments/comm/abstract_comm.py +++ b/instruments/abstract_instruments/comm/abstract_comm.py @@ -11,7 +11,9 @@ from __future__ import unicode_literals import abc +import codecs import logging +import struct from future.utils import with_metaclass @@ -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): """ diff --git a/instruments/abstract_instruments/comm/visa_communicator.py b/instruments/abstract_instruments/comm/visa_communicator.py index 7f407298c..0b16ef2a5 100644 --- a/instruments/abstract_instruments/comm/visa_communicator.py +++ b/instruments/abstract_instruments/comm/visa_communicator.py @@ -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() @@ -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) diff --git a/instruments/abstract_instruments/instrument.py b/instruments/abstract_instruments/instrument.py index 7163f40f0..2b294e96c 100644 --- a/instruments/abstract_instruments/instrument.py +++ b/instruments/abstract_instruments/instrument.py @@ -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. @@ -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 # diff --git a/instruments/tests/test_base_instrument.py b/instruments/tests/test_base_instrument.py index 2c121e1c6..f369f3d95 100644 --- a/instruments/tests/test_base_instrument.py +++ b/instruments/tests/test_base_instrument.py @@ -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(): diff --git a/instruments/thorlabs/pm100usb.py b/instruments/thorlabs/pm100usb.py index 8b06dceff..e9ad6e99b 100644 --- a/instruments/thorlabs/pm100usb.py +++ b/instruments/thorlabs/pm100usb.py @@ -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. diff --git a/requirements.txt b/requirements.txt index 1d83117d2..5c1eb06f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ numpy pyserial +pyvisa>=1.9 quantities>=0.12.1 future>=0.15 enum34 diff --git a/setup.py b/setup.py index 85af41b41..4275a38d3 100644 --- a/setup.py +++ b/setup.py @@ -37,6 +37,7 @@ INSTALL_REQUIRES = [ "numpy", "pyserial>=3.3", + "pyvisa>=1.9", "quantities", "enum34", "future", @@ -45,9 +46,7 @@ "pyusb", "ruamel.yaml" ] -EXTRAS_REQUIRE = { - 'VISA': ["pyvisa"] -} + # HELPER FUNCTONS ############################################################ @@ -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) @@ -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 +)