Connected up my Tektronix TDS 2012C to my Linux based computer and learned a bit I thought may be useful to others.

Topics include

Permissions for the usbtmc device
Hello world
5 Second Delay Reading usbtmc Device
Get a Screenshot
Getting and Plotting Data with gnuplot
Misc Commands I Sent to my Tektronix Scope



I have been experimenting with this scope on and off and have lost track of many references but here are a few:
Wikipedia about SCPI
Awesome c program

Permissions for the usbtmc0 device

The device when connected, in my case, created /dev/usbtmc1. Not sure why its 1 and not 0. One issue is a ‘normal user’ doesn’t have permissions to do anything with it. Two options here. One is to just ‘sudo chmod 666 /dev/usbtmc1’. The next is more permanent and recommended.

Create a file ‘/dev/udev/rules.d/86-tmc.rules’ with the following in it.

KERNEL=="usbtmc*", MODE="0666"  

Then run: ‘udevadm control –reload-rules && udevadm trigger’

Hello World

First a simple test. If you ask the device to ID does it?

adamd@pangolin:~/dev/TekTronix$ echo "*IDN?" > /dev/usbtmc1
adamd@pangolin:~/dev/TekTronix$ cat /dev/usbtmc1
TEKTRONIX,TDS 2012C,C015224,CF:91.1CT FV:v24.26
cat: /dev/usbtmc1: Connection timed out

One point of note here is the ‘Connection timed out’ after about 5 seconds.

5 Second Delay Reading usbtmc Device

I spent more time on this then I will admit. From some reading of others posts the kernel driver usbtmc doesn’t set the EOF bit so it just times out. This sucks! It means all my simple techniques can still work but will have a 5 second delay on read.

I tried many things and the only one that worked for me was this. The code came from https://github.com/dpenkler/linux-usbtmc/blob/master/ttmc.c and I just reduced and slightly modified to work for what I wanted to do. It works without the 5 second hang time. It does NOT work for commands that may return more then 1024 bytes. For those commands I still use echo and cat at the command line.

//This was cut from here: https://github.com/dpenkler/linux-usbtmc/blob/master/ttmc.c
//Trying to make the min necessary to ask an instrument questions from the cli
//It will read up to 1024 bytes, no more. For pulling large data sets like when using
//CURVE? I continue to use echo and cat from the command line and deal with the delay.

#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <linux/usb/tmc.h>

int fd;

/* Send string to scope */
void sscope(char *msg) {
        write(fd,msg,strlen(msg));
}

/* Read string from scope */
int rscope(char *buf, int max_len) {
        int len = read(fd,buf,max_len);
        if (len < 0) {
          perror("failed to read from scope");
          ioctl(fd,USBTMC_IOCTL_CLEAR);
          return 0;
        }
        buf[len] = 0; /* zero terminate */
        return len;
}

int main (int argc, char *argv[]) {
	#define MAX_BL 20480
	unsigned char buf[MAX_BL];
    	// Check that we were provided with a single command line argument
	if (argc != 2)
    	{
        	printf("Need a singlecommand to send, perhaps start with '*IDN?'\n");
        	return 0;
    	}

	/* Open file */
	if (0 > (fd = open("/dev/usbtmc1",O_RDWR))) {
		perror("failed to open device");
		exit(1);
	}

	/* Send device clear */
	ioctl(fd,USBTMC_IOCTL_CLEAR);
	/* set timeout lower */
	unsigned int timeout = 1000;
	ioctl(fd,USBTMC_IOCTL_SET_TIMEOUT,&timeout);

	/* Send identity query */
	sscope(argv[1]);

	/* If the command ended with a =? we expect a response */
    	if (argv[1][strlen(argv[1]) - 1] == '?') {
		rscope(buf,MAX_BL);
		printf("%s",buf);
	}
}

I compile it with ‘gcc -o send_this thecode.c’
With it I can do things like this:

#!/bin/bash
./send_this 'MEASU:IMMED:TYPE CUR'
while true 
do 
        ./send_this 'MEASU:IMMED:VALUE?'
done

Get a Screenshot

This has been around a while so I don’t have my reference(s).

#!/bin/bash

dev=/dev/usbtmc1
#script gets TDS2012C screen capture-
if [ "$1" == "" ]
then
	echo "Missing file name as command line parameter"
	exit
fi
out_file="$1"  #output file comes from firs arg of command line
size="0"
totaltries="0"
printf ":HARDCopy:PORT USB\n" > $dev
printf ":HARDCopy:FORMat JPEG\n" > $dev
printf ":HARDCopy STAR START\n" > $dev
printf ":HARDCOPY START\n" > $dev
#I keep trying until my picture is available
#giving up after a bit.
while (( size < 20 && totaltries < 6 ))
	do
	cat $dev  > $out_file 2> /dev/null
	size=`stat --printf="%s" $out_file`
	totaltries=$((totaltries+1))
	echo "Have tried $totaltries times, timeout at 6"
done

Use like this: ‘./thescript.sh bla.jpg’

Getting and Plotting Data with GNUPlot

I use this to get the raw data points. There is a lot of informatin missing such as formating the data, what data we’re taling about, etc. That information is in my TDS 2012C programmers manual. There are lots of options and you should be looking in your specific devices manuals.

# DATA INIT defaults the parameters for what CURV you are talking about.
echo "DATA INIT" > /dev/usbtmc1
# I want it ASCII encoded
echo "DAT:ENC ASCII" > /dev/usbtmc1 
# Request the data points
echo "CURV?" > /dev/usbtmc1 
# Read data points back dumping the timeout error mentioned above.
cat /dev/usbtmc1 2> /dev/null > thedata

Now I have a file with 2500 values all in one big line seperated by commas. It looks like this: 32,32,32,-21,-30,-31,-31,… For plotting it I use gnuplot. I use the following configuration file curv.gnuplot

#curv.gnuplot
set datafile separator comma
row = 0
set yrange [-100:100]
set terminal x11 size 800,600
plot filename matrix using 1:0 every :::row::row with lines

I get the plot by running this: gnuplot -p -e “filename=’result’” curv.gnuplot
It looks like this:




Misc Commands I Sent to my Tektronix Scope

Just some random commands I experimented with. I didn’t notice any case sensitivity when sending them. These commands were found in the Tektronix Programmer manual (077-0444-03) for my TDS 2012C osilliscope
*IDN?
ch1:scale?
DATA:SOURCE?
DATA:SOURCE REFA
MEASU:IMMED:TYPE CUR
MEASU:IMMED:VALUE?
HARDCopy:PORT USB
HARDCopy:FORMat JPEG
HARDCopy STAR START
HARDCOPY START
DATA INIT
DAT:ENC ASCII
CURV?