From 12922cba92b3c752f1e45f34cde111c5d453f5c1 Mon Sep 17 00:00:00 2001 From: Andrew Jayich Date: Thu, 16 Jul 2015 16:53:43 -0700 Subject: [PATCH 1/7] For USB devices the physical address is sufficient for the only two USB devices we have tested, a Rigol DG1022 and an Agilent DSO1024A. If other devices require adding '::INSTR' perhaps parsing addr for an ending '::INSTR' would be a solution. --- gpib_server.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/gpib_server.py b/gpib_server.py index 23fbe377..f82cb11f 100644 --- a/gpib_server.py +++ b/gpib_server.py @@ -107,15 +107,10 @@ def refreshDevices(self): deletions = set(self.devices.keys()) - set(addresses) for addr in additions: try: - if addr.startswith('GPIB'): - instName = addr - elif addr.startswith('TCPIP'): - instName = addr - elif addr.startswith('USB'): - instName = addr + '::INSTR' - else: + device_prefixes = ('GPIB', 'USB', 'TCPIP') + if not (addr.startswith(device_prefixes)): continue - instr = rm.get_instrument(instName) + instr = rm.get_instrument(addr) instr.write_termination = '' instr.clear() if addr.endswith('SOCKET'): From 77b14b236bdd9d82745f4875b9789c6cfceb4827 Mon Sep 17 00:00:00 2001 From: Andrew Jayich Date: Thu, 16 Jul 2015 16:55:34 -0700 Subject: [PATCH 2/7] We found that for two of our devices, a Rigol DG1022 and an Agilent DSO1024A, that we could not get proper communication without an asynchronous wait. If there is a better solution for this problem, we would be happy to implement. --- gpib_server.py | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/gpib_server.py b/gpib_server.py index f82cb11f..04d2fa84 100644 --- a/gpib_server.py +++ b/gpib_server.py @@ -38,6 +38,7 @@ from labrad.server import LabradServer, setting from twisted.internet.defer import inlineCallbacks +from twisted.internet import defer from twisted.internet.reactor import callLater from labrad.errors import DeviceNotSelectedError import labrad.units as units @@ -140,6 +141,23 @@ def getDevice(self, c): instr = self.devices[c['addr']] return instr + def asynchronous_wait(self, seconds, result=None): + """ + Pause the reactor for seconds to wait for GPIB devices that require + time between read and write calls. + + Parameters + ---------- + seconds: float, sleep time in seconds + + Returns + ------- + Deferred + """ + d = defer.Deferred() + callLater(seconds, d.callback, result) + return d + @setting(0, addr='s', returns='s') def address(self, c, addr=None): """Get or set the GPIB address for this context. @@ -171,10 +189,26 @@ def read(self, c, n_bytes=None): Otherwise, reads until the device stops sending. """ instr = self.getDevice(c) - if n_bytes is None: - ans = instr.read_raw() + if instr.resource_name.startswith('USB'): + try: + if n_bytes is None: + ans = instr.read() + else: + ans = instr.read(n_bytes) + except: + self.asynchronous_wait(0.1) + if n_bytes is None: + ans = instr.read() + else: + ans = instr.read(n_bytes) + ans = str(ans) + if ",," in ans: + ans = ans.replace(",,", ",") # rigol specific stupidity else: - ans = instr.read_raw(n_bytes) + if n_bytes is None: + ans = instr.read_raw() + else: + ans = instr.read_raw(n_bytes) return str(ans).strip() @setting(5, data='s', returns='s') From 094710dc45d5c8a8bea7cc5addb0c9ac33f32694 Mon Sep 17 00:00:00 2001 From: Andrew Jayich Date: Thu, 16 Jul 2015 20:57:38 -0700 Subject: [PATCH 3/7] labrad.util.wakeupCall already existed and looks more robust than aysnchornous_wait. Works with a pair of 'USB' GPIB devices. --- gpib_server.py | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/gpib_server.py b/gpib_server.py index 04d2fa84..221fa48d 100644 --- a/gpib_server.py +++ b/gpib_server.py @@ -37,6 +37,7 @@ # connections work, and should be improved. from labrad.server import LabradServer, setting +from labrad import util from twisted.internet.defer import inlineCallbacks from twisted.internet import defer from twisted.internet.reactor import callLater @@ -141,23 +142,6 @@ def getDevice(self, c): instr = self.devices[c['addr']] return instr - def asynchronous_wait(self, seconds, result=None): - """ - Pause the reactor for seconds to wait for GPIB devices that require - time between read and write calls. - - Parameters - ---------- - seconds: float, sleep time in seconds - - Returns - ------- - Deferred - """ - d = defer.Deferred() - callLater(seconds, d.callback, result) - return d - @setting(0, addr='s', returns='s') def address(self, c, addr=None): """Get or set the GPIB address for this context. @@ -196,7 +180,8 @@ def read(self, c, n_bytes=None): else: ans = instr.read(n_bytes) except: - self.asynchronous_wait(0.1) + util.wakeupCall(0.1) + # self.asynchronous_wait(0.1) if n_bytes is None: ans = instr.read() else: From 4d87cae3da8e7262b83dc058f9944d2be14219c2 Mon Sep 17 00:00:00 2001 From: Andrew Jayich Date: Thu, 16 Jul 2015 21:02:47 -0700 Subject: [PATCH 4/7] Deleted commented out old code. --- gpib_server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gpib_server.py b/gpib_server.py index 221fa48d..64803c25 100644 --- a/gpib_server.py +++ b/gpib_server.py @@ -181,7 +181,6 @@ def read(self, c, n_bytes=None): ans = instr.read(n_bytes) except: util.wakeupCall(0.1) - # self.asynchronous_wait(0.1) if n_bytes is None: ans = instr.read() else: From 66b48359c2ac336a5ea292f17bec261f374338cc Mon Sep 17 00:00:00 2001 From: Andrew Jayich Date: Thu, 16 Jul 2015 21:15:24 -0700 Subject: [PATCH 5/7] Comment to clarify an obscure logic statements that handles the address syntax returned by a specific instrument. --- gpib_server.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gpib_server.py b/gpib_server.py index 64803c25..ecb0a219 100644 --- a/gpib_server.py +++ b/gpib_server.py @@ -186,6 +186,8 @@ def read(self, c, n_bytes=None): else: ans = instr.read(n_bytes) ans = str(ans) + # Handles a special use case for a device (Rigol DG1022) that + # returns an address with ,, instead of ,. if ",," in ans: ans = ans.replace(",,", ",") # rigol specific stupidity else: From f58ed8e46092cfc9a768cd289c855817eef73550 Mon Sep 17 00:00:00 2001 From: Andrew Jayich Date: Thu, 16 Jul 2015 21:32:09 -0700 Subject: [PATCH 6/7] Included Exception. Need to increase the wait time to give the device a chance to respond. --- gpib_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gpib_server.py b/gpib_server.py index ecb0a219..a3a889c2 100644 --- a/gpib_server.py +++ b/gpib_server.py @@ -179,8 +179,8 @@ def read(self, c, n_bytes=None): ans = instr.read() else: ans = instr.read(n_bytes) - except: - util.wakeupCall(0.1) + except Exception: + util.wakeupCall(0.7) if n_bytes is None: ans = instr.read() else: From b397b5b64cbff206f3fb2fa173782b1dc38b4f38 Mon Sep 17 00:00:00 2001 From: Andrew Jayich Date: Fri, 17 Jul 2015 00:03:54 -0700 Subject: [PATCH 7/7] Including the "e" seems to be important. Eliminated an unnecessary str conversion. --- gpib_server.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gpib_server.py b/gpib_server.py index a3a889c2..4327ba39 100644 --- a/gpib_server.py +++ b/gpib_server.py @@ -179,13 +179,12 @@ def read(self, c, n_bytes=None): ans = instr.read() else: ans = instr.read(n_bytes) - except Exception: - util.wakeupCall(0.7) + except Exception, e: + util.wakeupCall(.3) if n_bytes is None: ans = instr.read() else: ans = instr.read(n_bytes) - ans = str(ans) # Handles a special use case for a device (Rigol DG1022) that # returns an address with ,, instead of ,. if ",," in ans: