prologix.py

Mon, 04 Feb 2008 19:02:42 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 04 Feb 2008 19:02:42 +0100
changeset 36
cb4124e3e75b
parent 35
3b7a38af5c42
permissions
-rw-r--r--

some improvements; can now give a bunch of files to open on cmd line; removed print statements

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

mercurial