#!/usr/bin/perl
# Dumps data from the Tektronix TDS-1002 digital oscilloscope over the RS-232
# interface and makes a nice (gnu-) plot from it.
#
# Copyright (C) 2005 Thor Andreassen <ta@imada.sdu.dk> 
#                    Anders Dubgaard <anders@dubgaard.net>
#
# $Id: tds1002.pl 198 2005-10-01 14:20:12Z anders $
#
# Usage: tds1002.pl <filename>
# - dumps data from oscilloscope to filename and (if $do_gnuplot is set) runs
#   gnuplot with output into filename.eps
#
# BUGS / Missing features
# - we currently assume that stty can set up the serial device properly;
#   19200,8N1, no flag.
#
# For details, see the TDS 200-, 1000 and 2000-Series Programmer Manual,
# which is NOT available from Tektronix's homepage, so search for it on
# http://www.google.com (the part number is 071-1075-01).
#
# More Tektronix-capturing information on the net:
#   1. http://secure.netroedge.com/~phil/tekscope/
#   2. http://www.warped.org/tek/
#   3. http://www.febo.com/geekworks/data-capture/
#
# GNUPlot:
#   http://www.gnuplot.info/faq/faq.html
#   http://www.duke.edu/~hpgavin/gnuplot.html
#   http://aquaterm.sourceforge.net/index.shtml?page=s1 ;-)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

use warnings;
use strict;
use File::Basename;

my $SERIAL_PORT = "/dev/ttyS0";
my $line;
my $do_gnuplot = "ya";

my $data_output;
if(defined $ARGV[0]){
  $data_output = $ARGV[0];
} else {
  $data_output = "/dev/stdout";
  $do_gnuplot = ""; # don't do gnuplotting as we send the coords to stdout
}

my $baudrate = 19200;
# set baud rate, assumes a Linux-like system
`stty -F $SERIAL_PORT $baudrate`;


open IN_DATA, "$SERIAL_PORT" or die "$!";
open OUT_DATA, ">$SERIAL_PORT" or die "$!";

print OUT_DATA "SELECT:CH2 ON\n";
print OUT_DATA "DATA:encdg ASCII\n";
print OUT_DATA "DATA:SOURCE CH1\n";
print OUT_DATA "CURVE?\n";
my $channel1 = <IN_DATA>;

print OUT_DATA "SELECT:CH2 ON\n";
print OUT_DATA "DATA:encdg ASCII\n";
print OUT_DATA "DATA:SOURCE CH2\n";
print OUT_DATA "CURVE?\n";
my $channel2 = <IN_DATA>;

close OUT_DATA;
close IN_DATA;

#$/ = $INPUT_RECORD_SEPARATOR = '\n\ as default
my @ch1 = split ',',$channel1;
my @ch2 = split ',',$channel2;

chop($ch1[@ch1-1]);
chop($ch2[@ch2-1]);

my $curve = "";
for (my $i=0; $i<2500; $i++) {
  $curve .= $i . "\t" . $ch1[$i] . "\t" . $ch2[$i] . "\n";
}
open DATAOUT, ">$data_output" or die "$!";
print DATAOUT $curve;
close DATAOUT;

if($do_gnuplot) {
  my $plotstr = 'set terminal postscript color solid; ' .
    'set output \"' . $data_output . '.eps\"; ' .
    'plot \"' . $data_output . '\" using 1:2 title \"CH1\" with lines lw 1, \"' .
    $data_output . '\" using 1:3 title \"CH2\" with l lt 4 lw 3';
  system("echo \"$plotstr\" | gnuplot");
}

# More TDS1002 commands:
# channel: CH1
# aq: PEAK
# ave: 16
# stat: 0
# ID?             fx. ID TEK/TDS 1002,CF:91.1CT TDS2MEM:FV:v6.08
# DATa INIT
# DATa:ENCdg ASCIi
# Data:start 1
# Data:stop 2500
# data:source CH1
# acq:mode PEAK
# acq:numavg 16
# CH1:pos?        fx. -1.32E0
# CH1:scale?      fx. 1.0E0
# hor:main:scale? fx. 5.0E-5
# hor:main:pos?   fx. 0.0E0
# WFMPre:WFId?    fx. "Ch1, DC coupling, 1.0E0 V/div,
#                      5.0E-5 s/div, 2500 points, Pk Detect mode"
# Data?           fx.ASCII;REFA;CH1;1;2500;1
#
# GnuPlot example:
# plot [xmin:xmax] [ymin:ymax] "file.dat" using 1:2 title 'CH1' with lines,\
#      "file.dat" using 1:3 title 'CH2' with lines 4
#
