hpib.py 2.5 KB
Newer Older
1
2
3
4
5
6
"""Implementation of the GPIB protocol for use with serial.threading

This class implements the GPIB (General Purpose Interface Bus) protocol following the example at
https://github.com/pyserial/pyserial/blob/master/examples/at_protocol.py
"""

7
8
9
10
11
12
13
14
15
16
import serial.threaded
import threading

try:
    import queue
except ImportError:
    import Queue as queue


class GPIBProtocol(serial.threaded.LineReader):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
17
18
    """Implementation of the GPIB protocol"""

19
20
21
22
23
24
25
26
    TERMINATOR = b'\n'

    def __init__(self):
        super(GPIBProtocol, self).__init__()
        self.responses = queue.Queue()
        self.lock = threading.Lock()

    def init_hpib(self):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
27
        """Execute some setup commands for the HPIB system"""
28
29
30
31
32
33
34
35
36
        with self.lock:
            self.write_line("++savecfg 0")
            self.write_line("++auto 0")
            self.write_line("++eoi 1")
            self.write_line("++eos 2")
            self.write_line("++eot_enable 0")
            self.write_line("++eot_char 0")
            self.write_line("++read_tmo_ms 500")

37
    def command(self, command, timeout=0.2, address=None, return_to_local=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
38
39
40
41
42
43
        """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
44
            return_to_local (bool): send a 'return to local' command after the command
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
45
46
47

        Returns:
             If the command contains '?', the value returned by
48
49
50
51
             the HPIB device is returned.

        Raises:
             RuntimeError: if no response was received to a query.
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
52
        """
53
        with self.lock:
54
55
            assert(responses.empty())

56
57
58
            if address:
                self.write_line("++addr {:d}".format(address))
            self.write_line(command)
59
60
61
            if address and return_to_local:
                self.write_line("loc {:d}".format(address))

62
63
64
            if "?" in command:
                self.write_line("++read eoi")
                # The command was a query, wait for response
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
65
66
                try:
                    line = self.responses.get(timeout=timeout)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
67
                except queue.Empty:
68
69
                    raise RuntimeError("HPIB device at address did not respond " +
                                       "to query \"{}\"".format(command))
70
71
72
73
74
                return line
            else:
                pass

    def handle_line(self, line):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
75
        """Handle input from serial port, put it in the queue"""
76
        self.responses.put(line)