|
1 """ |
|
2 Prologix |
|
3 ======== |
|
4 |
|
5 Module defining a communication object to talk to Prologix USB-GPIB controler. |
|
6 |
|
7 """ |
|
8 import serial |
|
9 import time |
|
10 |
|
11 import gpib |
|
12 from gpib import ConnectionError |
|
13 |
|
14 GPIB_CONTROLLER = 1 |
|
15 GPIB_DEVICE = 0 |
|
16 |
|
17 class GPIB(object): |
|
18 _retries = 15 |
|
19 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, |
|
20 address=0, mode=1): |
|
21 """ |
|
22 Create a new GPIB controller for the Prologix USB-GPIB device |
|
23 located on serial device 'device'. |
|
24 """ |
|
25 self._cnx = serial.Serial(port=device, baudrate=baudrate, timeout=timeout) |
|
26 self._timeout = timeout |
|
27 |
|
28 self.set_mode(mode) |
|
29 self.set_address(address) |
|
30 |
|
31 def set_address(self, address): |
|
32 """ |
|
33 Set the address of the GPIB device: |
|
34 |
|
35 - if the device is the Controller In Charge, this is the |
|
36 address of the device commands are sent to, |
|
37 |
|
38 - if the device is in GPIB_DEVICE mode, this is its address. |
|
39 """ |
|
40 self._set_cmd('addr', address) |
|
41 self._adress = address |
|
42 |
|
43 def set_mode(self, mode): |
|
44 """ |
|
45 Set GPIB device mode to 'mode': |
|
46 |
|
47 - GPIB_CONTROLLER: set the device as the Controller In Charge |
|
48 on the GPIB bus |
|
49 |
|
50 - GPIB_DEVICE: set the device as a standard GPIB device on the |
|
51 bus. |
|
52 """ |
|
53 self._set_cmd('mode', mode) |
|
54 self._mode = mode |
|
55 |
|
56 def set_controller(self): |
|
57 """ |
|
58 Set GPIB device the Controller In Charge on the GPIB bus. |
|
59 """ |
|
60 self.set_mode(1) |
|
61 |
|
62 def set_device(self): |
|
63 """ |
|
64 Set the GPIB device as a simple device on the GPIB bus. |
|
65 """ |
|
66 self.set_mode(0) |
|
67 |
|
68 def send_command(self, cmd): |
|
69 """ |
|
70 Send the specified GPIB command on the bus (must be the CIC), |
|
71 and read the answer. |
|
72 """ |
|
73 assert self._mode == 1 |
|
74 self._cnx.write(cmd+'\r') |
|
75 time.sleep(self._timeout) # required? |
|
76 ret = self._cnx.readlines() |
|
77 return ''.join(ret) |
|
78 |
|
79 def check_srq(self): |
|
80 """ |
|
81 Check the SRQ line |
|
82 """ |
|
83 assert self._mode == 1, "must be the Controller In Charge" |
|
84 self._cnx.write('++srq\r') |
|
85 ret = self._cnx.readline().strip() |
|
86 if ret: |
|
87 return bool(int(ret)) |
|
88 return None |
|
89 |
|
90 def poll(self): |
|
91 """ |
|
92 Poll every address, and return a dictionnary |
|
93 {add: status, ...} |
|
94 """ |
|
95 assert self._mode == 1, "must be the Controller In Charge" |
|
96 dico = {} |
|
97 for add in range(31): |
|
98 self._cnx.write('++spoll %d\r'%i) |
|
99 ret = self._cnx.readline().strip() |
|
100 if ret: |
|
101 dico[i] = int(ret) |
|
102 return dico |
|
103 |
|
104 def _read(self): |
|
105 for i in range(self._retries): |
|
106 rdata = self._cnx.readline() |
|
107 if rdata.strip() != "": |
|
108 break |
|
109 time.sleep(self._timeout) |
|
110 return rdata |
|
111 |
|
112 def _set_cmd(self, cmd, value): |
|
113 self._cnx.write('++%s %d\r'%(cmd, value)) |
|
114 self._cnx.write('++%s\r'%(cmd)) |
|
115 rval = self._read().strip() |
|
116 if not rval.isdigit() or int(rval) != value: |
|
117 raise ConnectionError("Can't set GPIB %s to %s [ret=%s]"%(cmd, value, repr(rval))) |