camrasdevices.py 2.25 KB
Newer Older
1
2
import astropy.units as u
from astropy.units import Quantity
3

4
5
import hpib
import serial.threaded
6

7
8
9

class CamrasHpibDevice(object):
    """Wrapper around HPIB commands"""
10

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
11
12
    command_thread = None

13
    def __init__(self, address):
14
        self.hpib_address = address
15

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
16
        if not CamrasHpibDevice.command_thread:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
17
            serial_port = serial.Serial("/dev/ttyUSB0")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
18
19
20
            CamrasHpibDevice.command_thread = serial.threaded.ReaderThread(serial_port, hpib.GPIBProtocol)
            CamrasHpibDevice.command_thread.start()
            CamrasHpibDevice.command_thread.protocol.init_hpib()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
21

22
23
    @property
    def frequency(self):
24
25
        """Returns the frequency of the device as an astropy Quantity.
        Throws a RuntimeError if no response"""
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
26
        freq_str = CamrasHpibDevice.command_thread.protocol.command("freq?", address=self.hpib_address)
27
28
        if len(freq_str)==0:
            raise RuntimeError("Camras device at address {} is not responding".format(self._hpib_address))
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
29
        return int(float(freq_str)) * u.Hz
30
31
32

    @frequency.setter
    def frequency(self, freq):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
33
34
35
36
37
        """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
        """
38
39
        if isinstance(freq, Quantity):
            freq = freq.to(u.Hz).value
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
40
        CamrasHpibDevice.command_thread.protocol.command("freq {:d} Hz".format(int(freq)), address=self.hpib_address)
41
        new_freq = self.frequency
42
        if new_freq.to(u.Hz).value != int(freq):
43
44
45
46
47
48
            raise RuntimeError("Setting frequency failed: tried to set to {}, it is now {}".format(
                               freq, new_freq))

class Receiver(CamrasHpibDevice):
    """Wrapper around HPIB commands for the Rohde & Schwartz receiver"""

49
    def __init__(self, **kwargs):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
50
        super(Receiver, self).__init__(1)
51

52
class LocalOscillator(CamrasHpibDevice):
53
54
    """Wrapper around HPIB commands for the local oscillator"""

55
    def __init__(self, **kwargs):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
56
        super(LocalOscillator, self).__init__(28)
57

58
59
class ClockGenerator(CamrasHpibDevice):
    """Wrapper around HPIB commands for the clock generator (should be at 140MHz)"""
60

61
    def __init__(self, **kwargs):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
62
        super(ClockGenerator, self).__init__(14)