Fri, 14 Dec 2007 00:21:47 +0100
several updates to the gpib module
2
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
1 | """ |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
2 | gpib: create serial connection to GPIB-USB device (ProLogix is the |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
3 | only supported device for now). |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
4 | """ |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
5 | import serial |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
6 | from serial.serialutil import SerialException |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
7 | import time |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
8 | |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
9 | class ConnectionError(Exception): |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
10 | pass |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
11 | |
9
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
12 | GPIB_CONTROLLER = 1 |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
13 | GPIB_DEVICE = 0 |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
14 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
15 | # IEEE 488.2 Status Byte constants |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
16 | MAV = 0x10 # Message AVailable: bit 4 of the Status Byte |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
17 | ESB = 0x20 # Event Status Bit: bit 5 of the Status Byte |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
18 | MSS = 0x40 # Master Summary Status bit: bit 6 of the Status Byte (NOT |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
19 | # sent in response to a serial poll) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
20 | RQS = 0x40 # Request Service: bit 6 of the Status Byte (when sent in |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
21 | # response to a serial poll) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
22 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
23 | # SESR constants (Standard Event Status Register) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
24 | PON = 0x80 # Power On: Power has been turned On since last register |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
25 | # read access |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
26 | URQ = 0x40 # User Request: the user has activated some device control |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
27 | # (whatever the Remote Local state is) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
28 | CME = 0x20 # Command Error |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
29 | EXE = 0x10 # Execution Error |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
30 | DDE = 0x08 # Device Dependant Error |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
31 | QYE = 0x04 # QuerY Error (attempt to read data while Output Queue is |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
32 | # empty, or data in the OQ was lost) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
33 | RQC = 0x02 # Request Control: tell the CiC that the device wants to |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
34 | # become the CiC |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
35 | OPC = 0x01 # Operation Complete: device has completed any pending |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
36 | # operation (ready to accept new commands). This bit is |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
37 | # generated in response to a OPC command. |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
38 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
39 | ##################### |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
40 | # HP3562A constants |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
41 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
42 | # GPIB buffer size is 3x80 characters lines |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
43 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
44 | # HP3562A Status Byte |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
45 | RQS = 0x40 # Request Service: when sent in response to a serial poll |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
46 | ERR = 0x20 # ERRor: GPIB error |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
47 | RDY = 0x10 #ReaDY: ready to accept GPIB commands |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
48 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
49 | |
2
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
50 | class GPIB(object): |
7 | 51 | _retries = 15 |
2
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
52 | def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
53 | address=0, mode=1): |
9
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
54 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
55 | Create a new GPIB controller for the Prologix USB-GPIB device |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
56 | located on serial device 'device'. |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
57 | """ |
2
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
58 | self._cnx = serial.Serial(port=device, baudrate=baudrate, timeout=timeout) |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
59 | self._timeout = timeout |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
60 | |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
61 | self.set_mode(mode) |
7 | 62 | self.set_address(address) |
9
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
63 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
64 | def set_address(self, address): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
65 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
66 | Set the address of the GPIB device: |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
67 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
68 | - if the device is the Controller In Charge, this is the |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
69 | address of the device commands are sent to, |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
70 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
71 | - if the device is in GPIB_DEVICE mode, this is its address. |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
72 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
73 | self._set_cmd('addr', address) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
74 | self._adress = address |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
75 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
76 | def set_mode(self, mode): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
77 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
78 | Set GPIB device mode to 'mode': |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
79 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
80 | - GPIB_CONTROLLER: set the device as the Controller In Charge |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
81 | on the GPIB bus |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
82 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
83 | - GPIB_DEVICE: set the device as a standard GPIB device on the |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
84 | bus. |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
85 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
86 | self._set_cmd('mode', mode) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
87 | self._mode = mode |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
88 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
89 | def set_controler(self): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
90 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
91 | Set GPIB device the Controller In Charge on the GPIB bus. |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
92 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
93 | self.set_mode(1) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
94 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
95 | def set_device(self): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
96 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
97 | Set the GPIB device as a simple device on the GPIB bus. |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
98 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
99 | self.set_mode(0) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
100 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
101 | def send_command(self, cmd): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
102 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
103 | Send the specified GPIB command on the bus (must be the CIC), |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
104 | and read the answer. |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
105 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
106 | assert self._mode == 1 |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
107 | self._cnx.write(cmd+'\r') |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
108 | time.sleep(self._timeout) # required? |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
109 | ret = self._cnx.readlines() |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
110 | return ''.join(ret) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
111 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
112 | def check_srq(self): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
113 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
114 | Check the SRQ line |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
115 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
116 | assert self._mode == 1, "must be the Controller In Charge" |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
117 | self._cnx.write('++srq\r') |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
118 | ret = self._cnx.readline().strip() |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
119 | if ret: |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
120 | return bool(int(ret)) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
121 | return None |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
122 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
123 | def poll(self): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
124 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
125 | Poll every address, and return a dictionnary |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
126 | {add: status, ...} |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
127 | """ |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
128 | assert self._mode == 1, "must be the Controller In Charge" |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
129 | dico = {} |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
130 | for add in range(31): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
131 | self._cnx.write('++spoll %d\r'%i) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
132 | ret = self._cnx.readline().strip() |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
133 | if ret: |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
134 | dico[i] = int(ret) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
135 | return dico |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
136 | |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
137 | def _read(self): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
138 | for i in range(self._retries): |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
139 | rdata = self._cnx.readline() |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
140 | if rdata.strip() != "": |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
141 | break |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
142 | time.sleep(self._timeout) |
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
143 | return rdata |
2
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
144 | |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
145 | def _set_cmd(self, cmd, value): |
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
146 | self._cnx.write('++%s %d\r'%(cmd, value)) |
7 | 147 | self._cnx.write('++%s\r'%(cmd)) |
148 | rval = self._read().strip() | |
2
cd9efa64f6da
added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
149 | if not rval.isdigit() or int(rval) != value: |
7 | 150 | raise ConnectionError("Can't set GPIB %s to %s [ret=%s]"%(cmd, value, repr(rval))) |
9
3b50c46fca56
several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents:
7
diff
changeset
|
151 |