Commit bf072b26 authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Factor out command function

parent 2cb072d0
......@@ -34,6 +34,12 @@ This checking can be disabled:
>>> non_checking_lo.frequency
<Quantity 1.1 GHz>
Other HPIB commands can be sent using the `command` function:
>>> lo.command("loc 28") # Set device to local mode
Also generic HPIB commands (without specific address) can be sent:
>>> lo.command("++eoi 1", prepend_address=False)
The CamrasHpibDevice class spawns one thread (shared with all instances) to perform the HPIB
commands. This is to make sure that instances living in separate threads do not interfere with
each other. The thread is a daemon, so it will be killed on program exit.
......@@ -67,6 +73,23 @@ class CamrasHpibDevice(object):
CamrasHpibDevice.command_thread._connection_made.wait()
CamrasHpibDevice.command_thread.protocol.init_hpib()
def command(self, command_string, prepend_address=True):
"""Send a command to the HPIB command thread
Args:
command_string (str): HPIB command
prepend_address (bool): first send the address of this device
Returns:
str: output of the command (in case it was a query)
"""
if prepend_address:
address = self.address
else:
address = None
return CamrasHpibDevice.command_thread.protocol.command(command_string, address)
@property
def frequency(self):
"""Frequency of the device as an astropy Quantity.
......@@ -77,8 +100,8 @@ class CamrasHpibDevice(object):
Raises:
RuntimeError: If the device does not respond
"""
freq_str = CamrasHpibDevice.command_thread.protocol.command("freq?",
address=self.hpib_address)
freq_str = self.command("freq?")
if not freq_str or len(freq_str) == 0:
raise RuntimeError("Camras device at address {} is not responding".format(
self.hpib_address))
......@@ -97,8 +120,8 @@ class CamrasHpibDevice(object):
"""
if isinstance(freq, Quantity):
freq = freq.to(u.Hz).value
CamrasHpibDevice.command_thread.protocol.command("freq {:d} Hz".format(int(freq)),
address=self.hpib_address)
self.command("freq {:d} Hz".format(int(freq)))
new_freq = self.frequency
if self._check_frequency and new_freq.to(u.Hz).value != int(freq):
raise RuntimeError("Setting frequency failed: tried to set to {}, it is now {}".format(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment