camrasdevices.py 1.99 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
10
11
serial_port = serial.Serial("/dev/ttyUSB0")
command_thread = serial.threaded.ReaderThread(serial_port, hpib.GPIBProtocol)

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

13
    def __init__(self, address):
14
15
16
        if not command_thread.is_alive():
            command_thread.start()
        self.hpib_address = address
17
18
19

    @property
    def frequency(self):
20
21
        """Returns the frequency of the device as an astropy Quantity.
        Throws a RuntimeError if no response"""
22
        freq_str = command_thread.protocol.command("freq?", address=self.hpib_address)
23
24
        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
25
        return int(float(freq_str)) * u.Hz
26
27
28

    @frequency.setter
    def frequency(self, freq):
29
        """Set the device to the specified frequency. Throws a RuntimeError if failed"""
30
31
        if isinstance(freq, Quantity):
            freq = freq.to(u.Hz).value
32
        command_thread.protocol.command("freq {:d} Hz".format(int(freq)), address=self.hpib_address)
33
        new_freq = self.frequency
34
        if new_freq.to(u.Hz).value != int(freq):
35
36
37
38
39
40
            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"""

41
42
    def __init__(self, **kwargs):
        super(Receiver, self).__init__(1, **kwargs)
43

44
class LocalOscillator(CamrasHpibDevice):
45
46
    """Wrapper around HPIB commands for the local oscillator"""

47
48
    def __init__(self, **kwargs):
        super(LocalOscillator, self).__init__(28, **kwargs)
49

50
51
class ClockGenerator(CamrasHpibDevice):
    """Wrapper around HPIB commands for the clock generator (should be at 140MHz)"""
52

53
54
    def __init__(self, **kwargs):
        super(SignalGenerator, self).__init__(14, **kwargs)