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

Improve documentation

parent 185d241a
...@@ -4,8 +4,8 @@ from astropy.units import Quantity ...@@ -4,8 +4,8 @@ from astropy.units import Quantity
import hpib import hpib
import serial.threaded import serial.threaded
serial_port = serial.Serial("/dev/ttyUSB0") command_thread = None
command_thread = serial.threaded.ReaderThread(serial_port, hpib.GPIBProtocol)
class CamrasHpibDevice(object): class CamrasHpibDevice(object):
"""Wrapper around HPIB commands""" """Wrapper around HPIB commands"""
...@@ -15,6 +15,10 @@ class CamrasHpibDevice(object): ...@@ -15,6 +15,10 @@ class CamrasHpibDevice(object):
command_thread.start() command_thread.start()
self.hpib_address = address self.hpib_address = address
if not command_thread:
serial_port = serial.Serial("/dev/ttyUSB0")
command_thread = serial.threaded.ReaderThread(serial_port, hpib.GPIBProtocol)
@property @property
def frequency(self): def frequency(self):
"""Returns the frequency of the device as an astropy Quantity. """Returns the frequency of the device as an astropy Quantity.
...@@ -26,7 +30,11 @@ class CamrasHpibDevice(object): ...@@ -26,7 +30,11 @@ class CamrasHpibDevice(object):
@frequency.setter @frequency.setter
def frequency(self, freq): def frequency(self, freq):
"""Set the device to the specified frequency. Throws a RuntimeError if failed""" """Set the device to the specified frequency. Throws a RuntimeError if failed
Args:
freq (Union[float, Quantity]): new frequency. If no unit present, assume Hz
"""
if isinstance(freq, Quantity): if isinstance(freq, Quantity):
freq = freq.to(u.Hz).value freq = freq.to(u.Hz).value
command_thread.protocol.command("freq {:d} Hz".format(int(freq)), address=self.hpib_address) command_thread.protocol.command("freq {:d} Hz".format(int(freq)), address=self.hpib_address)
...@@ -39,16 +47,16 @@ class Receiver(CamrasHpibDevice): ...@@ -39,16 +47,16 @@ class Receiver(CamrasHpibDevice):
"""Wrapper around HPIB commands for the Rohde & Schwartz receiver""" """Wrapper around HPIB commands for the Rohde & Schwartz receiver"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
super(Receiver, self).__init__(1, **kwargs) super(Receiver, self).__init__(1)
class LocalOscillator(CamrasHpibDevice): class LocalOscillator(CamrasHpibDevice):
"""Wrapper around HPIB commands for the local oscillator""" """Wrapper around HPIB commands for the local oscillator"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
super(LocalOscillator, self).__init__(28, **kwargs) super(LocalOscillator, self).__init__(28)
class ClockGenerator(CamrasHpibDevice): class ClockGenerator(CamrasHpibDevice):
"""Wrapper around HPIB commands for the clock generator (should be at 140MHz)""" """Wrapper around HPIB commands for the clock generator (should be at 140MHz)"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
super(SignalGenerator, self).__init__(14, **kwargs) super(ClockGenerator, self).__init__(14)
import serial
import serial.threaded import serial.threaded
import threading import threading
import time
import re
import platform
import sys
import datetime
try: try:
import queue import queue
...@@ -14,6 +8,8 @@ except ImportError: ...@@ -14,6 +8,8 @@ except ImportError:
class GPIBProtocol(serial.threaded.LineReader): class GPIBProtocol(serial.threaded.LineReader):
"""Implementation of the GPIB protocol"""
TERMINATOR = b'\n' TERMINATOR = b'\n'
def __init__(self): def __init__(self):
...@@ -22,6 +18,7 @@ class GPIBProtocol(serial.threaded.LineReader): ...@@ -22,6 +18,7 @@ class GPIBProtocol(serial.threaded.LineReader):
self.lock = threading.Lock() self.lock = threading.Lock()
def init_hpib(self): def init_hpib(self):
"""Execute some setup commands for the HPIB system"""
with self.lock: with self.lock:
self.write_line("++savecfg 0") self.write_line("++savecfg 0")
self.write_line("++auto 0") self.write_line("++auto 0")
...@@ -32,6 +29,17 @@ class GPIBProtocol(serial.threaded.LineReader): ...@@ -32,6 +29,17 @@ class GPIBProtocol(serial.threaded.LineReader):
self.write_line("++read_tmo_ms 500") self.write_line("++read_tmo_ms 500")
def command(self, command, timeout=0.2, address=None): def command(self, command, timeout=0.2, address=None):
"""Execute a HPIB command
Args:
command (str): Command to execute (can be a query)
timeout (float): How long to wait for a reply
address (int): HPIB address to send the command to
Returns:
If the command contains '?', the value returned by
the HPIB device is returned, else None.
"""
with self.lock: with self.lock:
if address: if address:
self.write_line("++addr {:d}".format(address)) self.write_line("++addr {:d}".format(address))
...@@ -41,12 +49,12 @@ class GPIBProtocol(serial.threaded.LineReader): ...@@ -41,12 +49,12 @@ class GPIBProtocol(serial.threaded.LineReader):
# The command was a query, wait for response # The command was a query, wait for response
try: try:
line = self.responses.get(timeout=timeout) line = self.responses.get(timeout=timeout)
except Empty: except queue.Empty:
print("No frequency returned")
return None return None
return line return line
else: else:
pass pass
def handle_line(self, line): def handle_line(self, line):
"""Handle input from serial port, put it in the queue"""
self.responses.put(line) self.responses.put(line)
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