********************************************* Program instruments with Tcl Test & Measurement World August 2003 http://www.tmworld.com ********************************************* Tcl Script From Ron Simonson, Analog Devices, ********************************************* To get started on using TCL to do GPIB programming you first need to get TCL. I am using ActiveState TCL on the MS machines. The binaries can be obtained from the web at: http://www.ActiveState.com/Products/ActiveTcl/ Next, you will need the GPIB package written by Ian Dees at Anritsu. You can get the binaries from: http://gpib-tcl.sourceforge.net You can also get some GPIB support for TCL from the Linux Lab http://www.llp.fu-berlin.de/ The ActiveState TCL comes in a self executable extraction program that will install TCL and make any changes to the machine setup that is required to run it. Ian's GPIB package comes in a zip file. Just unzip the file, look for the readme and follow the directions Ian gives to get things installed. Basically all you have to do is to create a directory called gpib in your TCL lib directory, and then copy the gpib.dll and the package.tcl files into the gpib directory you just made. Here are a couple of files to get you started. The first is simply a batch file for MS Windows that makes it just a bit easier to launch tclsh83 with the necessary command line: The ActiveState TCL comes in a self executable extraction program that will install TCL and make any changes to the machine setup that is required to run it. Ian's GPIB package comes in a zip file. Just unzip the file, look for the readme and follow the directions Ian gives to get things installed. Basically all you have to do is to create a directory called gpib in your TCL lib directory, and then copy the gpib.dll and the package.tcl files into the gpib directory you just made. Here are a couple of files to get you started. The first is simply a batch file for MS Windows that makes it just a bit easier to launch tclsh83 with the necessary command line. I name this file gpibcat.bat: @echo off tclsh83 gpibcat.tcl %1 The next file, called gpibcat.tcl, is the actual TCL script. This script reads an input file that contains an address followed by a command string that will be sent to the device at the address in the first field. The address is given as primary_address.secondary_address. If no secondary address is given, the program defaults to a secondary address of 0. #!/bin/sh # vim: set syntax=tcl : # The next line restarts using tclsh \ exec tclsh "$0" "$@" # # Script: gpibcat # Date: April 29th, 2003 # Author: Cary D. Renzema # # Version: 1.0 # Initial release. (CDR) # # Last modified: May 2nd, 2003 # # Description: # This program reads GPIB addresses and commands from the specified file # and sends the commands to the provided GPIB addresses. Blank lines and # comments (lines beginning with # are ignored). The GPIB address is # separated from the command by white space. The command may contain # embedded whitespace, though leading and trailing white space is removed. # A secondary address is provided by adding . to the # primary address. # # (c) Copyright 2003 Cary D. Renzema # Unpublished -- rights reserved under the copyright laws of the # United States. # # Print usage if a file is not specified if {$argc != 1} { puts stderr "Usage: $argv0 \n" puts stderr "This program reads a file of GPIB addresses and commands." puts stderr "Sending the individual commands to their given address.\n" puts stderr "File Syntax:" puts stderr " Blank lines and lines beginning with # (comments) are ignored." puts stderr " " puts stderr " An optional secondary address can be added as ." puts stderr " after the primary address." exit } # Open the input file if possible if {[catch {set fpIn [open [lindex $argv 0] r]}]} then { puts stderr "Error: The file \"[lindex $argv 0]\" is missing/unreadable!" exit } # Load the gpib package if possible if {[catch {package require gpib}]} then { puts stderr "Error: Unable to find the gpib package!" exit } # Make it an error for gpib commands to fail gpib interface exceptions on # Process the file while {![eof $fpIn]} { # Get the input line and remove leading/trailing whitespace set line [string trim [gets $fpIn]] # Skip blank lines and comments (lines beginning with #) if {[string equal "" $line] || [string equal -length 1 "#" $line]} then { continue } # Get the GPIB address and command (number(.number)? space command) if {![regexp {^((\d+)(?:\.(\d+))?)\s+(.+)$} $line not_used address \ primary secondary command]} then { puts stderr "Warning: Unable to parse the following line, skipping!" puts stderr " -----> $line" continue } # Set the secondary address to 0 if it is not given if {[string equal "" $secondary]} then { set secondary "0" } # Try to open the specified GPIB address if it is not already open if {![info exists gpibDev($address)]} then { if {[catch {set gpibDev($address) \ [gpib open -address $primary -secondary $secondary]}]} then { puts stderr "Warning: Unable to open address number $address, skipping!" continue } } # Select the GPIB device and try to send the command to it gpib select -device $gpibDev($address) if {[catch {gpib write -device $gpibDev($address) -message $command}]} then { puts stderr "Warning: Problems writing command to address number $address!" puts stderr " -----> $command" } } gpib close -device all close $fpIn The final file is an example input file. Lines beginning with # are comment lines and will be ignored by the script gpibcat.tcl. Blank lines are also ignored. # Set up the Keithley 230 Programmable voltage source # Current limit = 20mA # Output voltage = 5.0Vdc # Output On 12 I1X 12 V5.0E0X 12 F1X # Setup the Keithley 619 Electrometer # Channel A set to measure current # Channel B set to measure current 16.97 F1R0M0T4D0Q0S9P0C0Z0N0U0X 16.98 F1R0M0T4D0Q0S9P0C0Z0N0U0X #Set up the Keithley 220 Progammable current source # Compliance voltage = 5.0V # Output current = 1nA # Output On 14 V5X 14 I1E-9X 14 F1X # Setup the Tektronix TDS 3032 Oscilloscope # Channel 1 scale factor = 2.00 V/div 1 *RST 1 CH1:SCALE 2.0E0 1 HORIZONTAL:MAIN:SCALE 20E-6 1 TRIGGER:A:LEVEL 3.0E0 1 TRIGGER:EDGE:COUPLING DC 1 TRIGGER:EDGE:SLOPE RISE 1 TRIGGER:EDGE:SOURCE CH1