hpib.py 1.27 KB
Newer Older
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
1
import serial
2
3
import serial.threaded
import threading
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
4
5
6
import time
import re
import platform
7
import sys
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import datetime

try:
    import queue
except ImportError:
    import Queue as queue


class GPIBProtocol(serial.threaded.LineReader):
    TERMINATOR = b'\n'

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

    def init_hpib(self):
        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")

    def command(self, command, timeout=0.2, address=None):
        with self.lock:
            if address:
                self.write_line("++addr {:d}".format(address))
            self.write_line(command)
            if "?" in command:
                self.write_line("++read eoi")
                # The command was a query, wait for response
                line = self.responses.get(timeout=timeout)
                return line
            else:
                pass

    def handle_line(self, line):
        self.responses.put(line)