camrasdevices.py 2.32 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
            CamrasHpibDevice.command_thread = serial.threaded.ReaderThread(serial_port, hpib.GPIBProtocol)
            CamrasHpibDevice.command_thread.start()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
20
            CamrasHpibDevice.command_thread._connection_made.wait()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
21
            CamrasHpibDevice.command_thread.protocol.init_hpib()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
22

23
24
    @property
    def frequency(self):
25
26
        """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
27
        freq_str = CamrasHpibDevice.command_thread.protocol.command("freq?", address=self.hpib_address)
28
29
        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
30
        return int(float(freq_str)) * u.Hz
31
32
33

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

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

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

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

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

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