Skip to content

Commit 0448033

Browse files
author
SamSchott
authored
Merge pull request #7 from OE-FET/master
A few bug fixes and consistency improvements
2 parents f391ab1 + 2c47550 commit 0448033

4 files changed

Lines changed: 42 additions & 33 deletions

File tree

CHANGES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
v0.2.3 21.07.2020 -- Fixed a possible issue when parsing alarms, set connection to None if not connected, improved thread safety when connecting to or disconnecting from device.
12
v0.2.2 04.12.2019 -- Remember PyVisa connection settings passed to constructor between disconnects. Improved thread safety.
23
v0.2.1, 30.04.2019 -- Moved controls for LOOP module to TEMP module.
3-
v0.2.0, 09.02.2019 -- Use pyvisa as communication protocol, added support for LOOP module, singleton behaviour when instanciating the device (all credit goes to Sam Schott for this release)
4+
v0.2.0, 09.02.2019 -- Use pyvisa as communication protocol, added support for LOOP module, singleton behaviour when instantiating the device (all credit goes to Sam Schott for this release)
45
v0.1.0, 18.03.2014 -- Initial release.

mercuryitc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = '0.2.2'
1+
__version__ = '0.2.3'
22

33
from mercuryitc.mercury_driver import MercuryITCFactory as MercuryITC

mercuryitc/mercury_driver.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,7 @@ class MercuryITC(MercuryCommon):
779779
DIMAS = ['ON', 'OFF']
780780
_lock = threading.RLock()
781781

782-
connected = True
783-
connection = False
782+
connection = None
784783

785784
address = 'SYS'
786785

@@ -800,32 +799,32 @@ def connect(self, **kwargs):
800799
kwargs = kwargs or self._connection_kwargs # use specified or remembered kwargs
801800
connection_error = OSError if PY2 else ConnectionError
802801

803-
try:
804-
self.connection = self.rm.open_resource(self.visa_address, **kwargs)
805-
self.connection.read_termination = '\n'
806-
self.connected = True
807-
self._init_modules()
808-
except connection_error:
809-
logger.info('Connection to the instrument failed. Please check ' +
810-
'that no other programm is connected.')
811-
self.connection = None
812-
self.connected = False
813-
except AttributeError:
814-
logger.info('Invalid VISA address %s.' % self.visa_address)
815-
self.connection = None
816-
self.connected = False
817-
except Exception:
818-
logger.info('Could not connect to Mercury at %s.' % self.visa_address)
819-
self.connection = None
820-
self.connected = False
802+
with self._lock:
803+
try:
804+
self.connection = self.rm.open_resource(self.visa_address, **kwargs)
805+
self.connection.read_termination = '\n'
806+
self._init_modules()
807+
except connection_error:
808+
logger.info('Connection to the instrument failed. Please check ' +
809+
'that no other program is connected.')
810+
self.connection = None
811+
except AttributeError:
812+
logger.info('Invalid VISA address %s.' % self.visa_address)
813+
self.connection = None
814+
except Exception:
815+
logger.info('Could not connect to Mercury at %s.' % self.visa_address)
816+
self.connection = None
821817

822818
def disconnect(self):
823-
self.connected = False
824-
try:
825-
self.connection.close()
826-
del self.connection
827-
except AttributeError:
828-
pass
819+
820+
with self._lock:
821+
if self.connection:
822+
self.connection.close()
823+
self.connection = None
824+
825+
@property
826+
def connected(self):
827+
return self.connection is not None
829828

830829
def _init_modules(self):
831830
self.modules = []
@@ -841,18 +840,27 @@ def _init_modules(self):
841840
self.modules.append(MercuryITC_HTR(address, self))
842841

843842
def write(self, q):
843+
844844
with self._lock:
845+
if not self.connection:
846+
raise ConnectionError('Not connected to device.')
845847
self.connection.write(q)
846848

847849
def read(self):
850+
848851
with self._lock:
852+
if not self.connection:
853+
raise ConnectionError('Not connected to device.')
849854
return self.connection.read()
850855

851856
def query(self, q):
857+
852858
with self._lock:
859+
if not self.connection:
860+
raise ConnectionError('Not connected to device.')
853861
r = self.connection.query('%s' % q)
854862
self.connection.clear()
855-
return r
863+
return r
856864

857865
@property
858866
def cat(self):
@@ -996,9 +1004,9 @@ def date(self, val):
9961004
@property
9971005
def alarms(self):
9981006
"""Gets the system alarms log"""
999-
string = self._read_property('ALRM', str)
1000-
alarm_list = string[:-1].split(';')
1001-
alarms_dict = dict(item.split('\t') for item in alarm_list)
1007+
value = self._read_property('ALRM', str)
1008+
string_list = value.split(';')
1009+
alarms_dict = dict(s.split('\t') for s in string_list if s)
10021010
return alarms_dict
10031011

10041012

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Sam Schott <ss2151@cam.ac.uk>
66

77
setup(name="mercuryitc",
8-
version='0.2.2',
8+
version='0.2.3',
99
description="Full Python driver for the Oxford Mercury iTC cryogenic environment controller.",
1010
author='Florian Forster, Sam Schott',
1111
maintainer='Florian Forster',

0 commit comments

Comments
 (0)