EEVblog Electronics Community Forum

Products => Test Equipment => Topic started by: Franz Zinn on August 09, 2014, 11:20:50 pm

Title: Rigol DS1074Z times out with Linux USBTMC drivers and Python scripts
Post by: Franz Zinn on August 09, 2014, 11:20:50 pm
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 code

This usbtmc device class

Code: [Select]
# for future compatibility
from __future__ import division
from __future__ import print_function

# for file access
import os
# for timed waits
import time

class 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 loop

Code: [Select]
# for future compatibility
from __future__ import division
from __future__ import print_function

# for command history and editing
import readline
# for command line processing
import sys

# import device drivers
from usbtmc import Device

# check command line arguments
if len(sys.argv) != 2 :
    print('Usage: %s <device>'%sys.argv[0], file=sys.stderr)
    exit(1)

dev = Device(sys.argv[1])

# read and execute commands
try:
    while True:
        cmd = raw_input('scpi> ')
        if cmd.find('?') >= 0:
            answer = dev.query(cmd).strip()
            print(answer)
        else:
            dev.write(cmd)
except EOFError:
    pass

The error

Code: [Select]
./scpishell /dev/Rigol-DS1074Z-S
scpi> *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 query
    return self.read()
  File "/home/bt/work/rigol/src/usbtmc.py", line 25, in read
    return os.read(self.FILE, 4096)
OSError: [Errno 110] Connection timed out


Note :
Title: Re: Rigol DS1074Z times out with Linux USBTMC drivers and Python scripts
Post by: Franz Zinn on August 10, 2014, 01:38:19 am
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 is

Code: [Select]
# for future compatibility
from __future__ import division
from __future__ import print_function

# for file access
import os
# for timed waits
import time

class 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")
Title: Re: Rigol DS1074Z times out with Linux USBTMC drivers and Python scripts
Post by: FrankBuss on December 16, 2016, 07:46:39 pm
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?