Author Topic: Rigol DS1074Z times out with Linux USBTMC drivers and Python scripts  (Read 2058 times)

0 Members and 1 Guest are viewing this topic.

Offline Franz Zinn

  • Contributor
  • Posts: 19
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 :
  • /dev/Rigol-DS1074Z-S  is symlinked to /dev/usbtmc0
  • The 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
 

Offline Franz Zinn

  • Contributor
  • Posts: 19
Re: Rigol DS1074Z times out with Linux USBTMC drivers and Python scripts
« Reply #1 on: August 10, 2014, 11: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")
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 1709
  • Country: de
    • Frank Buss
Re: Rigol DS1074Z times out with Linux USBTMC drivers and Python scripts
« Reply #2 on: December 17, 2016, 06:46:39 AM »
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?
quadro copter flying, electronics, retro computing and other geeky things: http://www.youtube.com/user/frankbuss/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

http://opalkelly.com/