Rigol DS1074Z times out with Linux USBTMC drivers and Python scripts - Page 1
Forum section: electronics / testgear

author: Franz Zinn
author_rank: Contributor
timestamp: August 09, 2014, 11:20:50 pm
content: I am playing around trying to program a Rigol DS1074Z oscilloscope interfacing it to a Linux (Kernel 3.10) PC using the USBTMC driver (/dev/usbtmc0). However I am unable to get a response back from the scope. If anyone has managed to get this working I would be grateful if they could advice.Here is my codeThis usbtmc device classCode:# for future compatibilityfrom __future__ import divisionfrom __future__ import print_function# for file accessimport os# for timed waitsimport timeclass Device:"""Simple usbmtc device"""def __init__(self, device="/dev/usbtmc0"):self.FILE = os.open(device, os.O_RDWR)def __del__(self):os.close(self.FILE)def write(self, cmd):os.write(self.FILE, cmd)def read(self):return os.read(self.FILE, 4096)def query(self, cmd, timeout=0.01):self.write(cmd)time.sleep(timeout)return self.read()def reset():self.write("*RST")The main loopCode:# for future compatibilityfrom __future__ import divisionfrom __future__ import print_function# for command history and editingimport readline# for command line processingimport sys# import device driversfrom usbtmc import Device# check command line argumentsif len(sys.argv) != 2 :print('Usage: %s <device>'%sys.argv, file=sys.stderr)exit(1)dev = Device(sys.argv)# read and execute commandstry:while True:cmd = raw_input('scpi> ')if cmd.find('?') >= 0:answer = dev.query(cmd).strip()print(answer)else:dev.write(cmd)except EOFError:passThe errorCode:./scpishell /dev/Rigol-DS1074Z-Sscpi> *IDN?Traceback (most recent call last):File "./scpishell", line 27, in <module>answer = dev.query(cmd).strip()File "/home/bt/work/rigol/src/usbtmc.py", line 30, in queryreturn self.read()File "/home/bt/work/rigol/src/usbtmc.py", line 25, in readreturn os.read(self.FILE, 4096)OSError: [Errno 110] Connection timed outNote :/dev/Rigol-DS1074Z-S is symlinked to /dev/usbtmc0The same problems occurs with C code, in which case the error is "Unable to read data, error -110"I do not see any instructions in the manual or the scope interface to set it to something like "PC control mode". So I presume this is not required

author: Franz Zinn
author_rank: Contributor
timestamp: August 10, 2014, 01:38:19 am
content: I managed to make this work by using Python's built-in open() call instead of os.open(), which allowed me to disable buffering using the buffering=0 option. So it seems I/O buffering by Linux was the issue (and os.open() could also be used with appropriate flags). Though I am not sure why this problem does not occur with older Rigol scopes such as the DS1052.The new code isCode:# for future compatibilityfrom __future__ import divisionfrom __future__ import print_function# for file accessimport os# for timed waitsimport timeclass Device:"""Simple usbmtc device"""def __init__(self, device="/dev/usbtmc0"):self.FILE = open(device, 'w+', buffering=0)def __del__(self):self.FILE.close()def write(self, cmd):self.FILE.write(cmd)def read(self):return self.FILE.read()def query(self, cmd, timeout=0.01):self.write(cmd)time.sleep(timeout)return self.read()def reset():self.write("*RST")

author: FrankBuss
author_rank: Unknown
timestamp: December 16, 2016, 07:46:39 pm
content: In case someone is searching for the same problem: With my SPD3303D your first implementation of the device class worked (btw, thanks for the code!), but your second implementation not (gave timeout errors). Looks like the first code works reliable with a timeout of 10 ms or more. Below 5 ms it doesn't work at all, no data. Might be safe to set the timeout to 100 ms, just in case. I guess it has to do with the USB 10 ms packet scheduling?