Fri, 04 May 2018 01:00:59 +0200
port HPGL plotter emulator to PyQt5
66
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
1 | # This program is free software; you can redistribute it and/or modify it under |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
2 | # the terms of the GNU General Public License as published by the Free Software |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
3 | # Foundation; either version 2 of the License, or (at your option) any later |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
4 | # version. |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
5 | # |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
6 | # This program is distributed in the hope that it will be useful, but WITHOUT |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
7 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
8 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
9 | # |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
10 | # You should have received a copy of the GNU General Public License along with |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
11 | # this program; if not, write to the Free Software Foundation, Inc., |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
12 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
13 | """ Copyright (c) 2007-2018 David Douard (Paris, FRANCE). |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
14 | http://www.logilab.org/project/pygpibtoolkit -- mailto:david.douard@sdfa3.org |
66
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
64
diff
changeset
|
15 | |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
13
diff
changeset
|
16 | prologix |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
17 | ======== |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
18 | |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
19 | Module defining a communication object to talk to Prologix USB-GPIB controler. |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
20 | """ |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
21 | |
79 | 22 | import sys |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
23 | import serial |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
24 | import time |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
25 | |
40
1bbea188a7e5
refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
35
diff
changeset
|
26 | from pygpibtoolkit.pygpib import ConnectionError |
13
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 | GPIB_CONTROLLER = 1 |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
29 | GPIB_DEVICE = 0 |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
30 | |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
31 | |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
32 | class GPIB: |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
33 | _retries = 15 |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
34 | |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
35 | def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
36 | address=0, mode=1): |
13
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 | Create a new GPIB controller for the Prologix USB-GPIB device |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
39 | located on serial device 'device'. |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
40 | """ |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
41 | self._cnx = serial.Serial(port=device, baudrate=baudrate, |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
42 | timeout=timeout) |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
43 | self._timeout = timeout |
56
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
44 | |
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
45 | try: |
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
46 | res = self._cnx.readlines() |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
47 | if res: # empty evetual read buffer |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
48 | print("there where pending stuffs in buffer %r" % res) |
77
1e539617d6ac
fix small bugs in qgpib plotter
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
49 | self.set_mode(1) |
56
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
50 | self.set_address(address) |
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
51 | self._set_cmd('auto', 0) |
77
1e539617d6ac
fix small bugs in qgpib plotter
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
52 | self.set_mode(mode) |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
53 | except Exception as e: |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
54 | print("Humm, something went wrong: %s" % e) |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
55 | |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
56 | def set_address(self, address, check=True): |
13
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 the address of the GPIB device: |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
59 | |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
60 | - 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
|
61 | address of the device commands are sent to, |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
62 | |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
63 | - 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
|
64 | """ |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
65 | self._set_cmd('addr', address, check) |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
66 | self._address = address |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
67 | # self._set_cmd('auto', 0) |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
68 | |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
69 | def set_mode(self, mode): |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
70 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
71 | Set GPIB device mode to 'mode': |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
72 | |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
73 | - GPIB_CONTROLLER: set the device as the Controller In Charge |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
74 | on the GPIB bus |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
75 | |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
76 | - 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
|
77 | bus. |
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 | self._set_cmd('mode', mode) |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
80 | self._mode = mode |
56
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
81 | if self._mode: |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
82 | self._cnx.write(b'++ifc\r') |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
83 | |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
84 | def set_controller(self): |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
85 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
86 | 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
|
87 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
88 | self.set_mode(1) |
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 set_device(self): |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
91 | """ |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
92 | Set the GPIB device as a simple device on the GPIB bus. |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
93 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
94 | self.set_mode(0) |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
95 | |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
96 | def send_command(self, cmd, address=None): |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
97 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
98 | 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
|
99 | and read the answer. |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
100 | Eventually, set the addressed device first. |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
101 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
102 | assert self._mode == 1 |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
103 | if isinstance(cmd, str): |
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
104 | cmd = cmd.encode() |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
105 | if address is not None: |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
106 | self.set_address(address) |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
107 | self._cnx.write(cmd + b';\r') |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
108 | time.sleep(self._timeout) # required? |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
109 | return self.read_eoi() |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
110 | |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
111 | def read_eoi(self, address=None): |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
112 | """ |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
113 | Read the HPIB buffer from device, till EOI is performed, or timeout. |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
114 | """ |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
115 | if address is not None: |
79 | 116 | self.set_address(address, check=False) |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
117 | self._cnx.write(b'++read eoi\r') # idem |
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
118 | ret = '' |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
119 | i = 0 |
56
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
120 | while not ret.endswith('\r\n') and i<3: |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
121 | ret += (b''.join(self._cnx.readlines())).decode() |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
122 | time.sleep(self._timeout) # required? |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
123 | i += 1 |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
124 | return ret |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
125 | |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
126 | def check_srq(self): |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
127 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
128 | Check the SRQ line |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
129 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
130 | assert self._mode == 1, "must be the Controller In Charge" |
79 | 131 | ret = self._cnx.readline().strip() |
132 | if ret: | |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
133 | print("garbage: %s" % ret) |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
134 | self._cnx.write(b'++srq\r') |
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
135 | ret = self._cnx.readline().decode().strip() |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
136 | if ret in "01": |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
137 | return bool(int(ret)) |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
138 | return None |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
139 | |
79 | 140 | def trigger(self, address=None): |
141 | """ | |
142 | Trigger device at 'address' | |
143 | """ | |
144 | if address is not None: | |
145 | self.set_address(address, check=False) | |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
146 | self._cnx.write(b'++trg\r') # idem |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
147 | |
79 | 148 | def poll(self, addresses=None, verbose=False): |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
149 | """ |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
150 | Poll every address, and return a dictionnary |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
151 | {add: status, ...} |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
152 | """ |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
153 | assert self._mode == 1, "must be the Controller In Charge" |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
154 | only_one = False |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
155 | if addresses is None: |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
156 | addresses = list(range(31)) |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
157 | if not isinstance(addresses, (list, tuple)): |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
158 | addresses = [addresses] |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
159 | only_one = True |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
160 | if not addresses: |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
161 | return None |
79 | 162 | |
163 | if verbose: | |
164 | sys.stderr.write('polling ') | |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
165 | dico = {} |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
166 | for add in addresses: |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
167 | self._cnx.write(('++spoll %d\r' % add).encode()) |
79 | 168 | time.sleep(0.1) |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
169 | ret = self._cnx.readline().decode().strip() |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
170 | if ret: |
79 | 171 | if verbose: |
172 | sys.stderr.write('X') | |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
173 | dico[add] = int(ret) |
79 | 174 | else: |
175 | if verbose: | |
176 | sys.stderr.write('.') | |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
177 | |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
178 | # need to wait at least 150ms (not enough on prologix) |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
179 | time.sleep(0.30) |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
180 | |
79 | 181 | if verbose: |
182 | sys.stderr.write('\n') | |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
183 | self.set_address(self._address) |
64
624157f78b77
add a simple 'gpib_detect' command line tool and some minor fixes
David Douard <david.douard@logilab.fr>
parents:
56
diff
changeset
|
184 | if only_one and dico: |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
185 | return list(dico.values())[0] |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
186 | return dico |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
187 | |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
188 | def _read(self): |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
189 | for i in range(self._retries): |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
190 | rdata = self._cnx.readline() |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
191 | if rdata.strip(): |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
192 | break |
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
193 | time.sleep(self._timeout) |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
194 | return rdata.decode() |
13
78e3e839658b
some forgotten added files
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
195 | |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
196 | def _set_cmd(self, cmd, value, check=True): |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
197 | self._cnx.write(('++%s %d\r' % (cmd, value)).encode()) |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
198 | if check: |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
199 | self._cnx.write(('++%s\r' % (cmd)).encode()) |
53
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
200 | rval = self._read().strip() |
8e32c806fcdd
Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
40
diff
changeset
|
201 | if not rval.isdigit() or int(rval) != value: |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
202 | raise ConnectionError("Can't set GPIB %s to %s [ret=%s]" % ( |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
203 | cmd, value, repr(rval))) |
56
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
204 | |
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
205 | def reset(self): |
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
206 | """ |
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
207 | Perform a reset of the USB device |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
208 | |
56
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
209 | """ |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
210 | print("Resetting GPIB controller") |
87
59a0946aa3d1
port HPGL plotter emulator to PyQt5
David Douard <david.douard@logilab.fr>
parents:
86
diff
changeset
|
211 | self._cnx.write(b'++rst\r') |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
212 | print("Must wait for 5 seconds") |
56
9573558f9191
Try to be a little more robust when instanciating the GPIB class (serial connector)
David Douard <david.douard@logilab.fr>
parents:
53
diff
changeset
|
213 | time.sleep(5) |