/* Agilent Spectrum Analyzer Computer Control Demo */ /* Steve Ellingson, 06 Jun 2002 */ /* This software demonstrates how to interface to an Agilent E4407B spectrum analyzer */ /* via RS-232 using C. Specifically, LabWindows/CVI is used, but only for convenience */ /* in formatting and plotting captured data. Suggested reference is "ESA Series */ /* Spectrum Analyzers Programmer's Guide" -- but I strongly recommend ignoring the */ /* VISA API strategy used in their examples and using just straight SCPI commands as */ /* I have done here. */ /* To run this demo, first set the analyzer's serial port to run at 115200 (fastest) */ /* and jumper the "AMPTD REF OUT" jack to the "INPUT" jack (this provides a 50 MHz */ /* reference signal at -20 dBm, which gives us something to look at). When this */ /* program is run, it does the following: */ /* 1. Analyzer is reset to a known condition (factory presets) */ /* 2. 50 MHz reference signal is turned on */ /* 3. Frequency range is set to 10-90 MHz */ /* 4. A single frequency sweep is triggered and captured */ /* 5. The captured trace data is converted to a double-precision array and plotted */ /* 6. Program idles until "QUIT" button is clicked */ /* NOTE: Allow a few seconds for the trace to be plotted after starting the program */ /*--- Header files -----------------------------------------------------*/ #include #include #include /* to allow use of NI's nifty "Scan()" function */ /*--- Macro Defines ----------------------------------------------------*/ #define TRUE 1 #define FALSE 0 /*--- Globals --------------------------------------------------------*/ /*--- LabWindows/CVI-specific GUI stuff --------------------------------*/ #include "sa_demo.h" /* contains only macro defines for GUI */ #define NO_WAIT 0 #define NULL_PLOT_HANDLE -1 static int hpMain; /* handle to panel "MAIN" */ /*======================================================================*/ /*=== main() ===========================================================*/ /*======================================================================*/ void main(void) { /*--- LabWindows/CVI-specific GUI stuff -------------------------------*/ int iControlID; int hHandle; int bQuit = FALSE; /*--- Setting up the LabWindows/CVI GUI ---------------------------------*/ hpMain = LoadPanel (0, "sa_demo.uir", MAIN); DisplayPanel (hpMain); /*--- SA control section ------------------------------------------------*/ { char acTxBuffer[256]; int iStrLen; int bDone; #define BYTES_PER_SWEEP 1611 char acSweep[BYTES_PER_SWEEP]; #define POINTS_PER_SWEEP 401 double x[POINTS_PER_SWEEP]; double y[POINTS_PER_SWEEP]; int i; /* Open COM1 to spectrum analyzer. */ /* Assumes spectrum analyzer baud rate set to 115200 bps. Note port config is 8N1 */ OpenComConfig (1, "COM1", 115200, 0, 8, 1, BYTES_PER_SWEEP+1, 512); /* Reset spectrum analyzer to factory preset */ iStrLen = sprintf(acTxBuffer,"%s\n","*RST"); ComWrt (1, acTxBuffer, iStrLen); /* Turn on -20 dBm @ 50 MHz cal source; must be jumpered to input on front panel */ iStrLen = sprintf(acTxBuffer,"%s\n","CAL:SOUR:STAT ON"); ComWrt (1, acTxBuffer, iStrLen); /* Set sweep start frequency */ iStrLen = sprintf(acTxBuffer,"%s\n","SENS:FREQ:STAR 10 MHZ"); ComWrt (1, acTxBuffer, iStrLen); /* Set sweep stop frequency */ iStrLen = sprintf(acTxBuffer,"%s\n","SENS:FREQ:STOP 90 MHZ"); ComWrt (1, acTxBuffer, iStrLen); /* Set to single sweep mode (vs. default, which is continuous sweep) */ iStrLen = sprintf(acTxBuffer,"%s\n","INIT:CONT 0"); ComWrt (1, acTxBuffer, iStrLen); /* Set data format to 32-bit real */ iStrLen = sprintf(acTxBuffer,"%s\n","FORM:DATA REAL,32"); ComWrt (1, acTxBuffer, iStrLen); /* Set data format binary byte order to SWAP */ iStrLen = sprintf(acTxBuffer,"%s\n","FORM:BORD SWAP"); ComWrt (1, acTxBuffer, iStrLen); /* Flush input queue */ FlushInQ (1); /* Trigger a sweep */ iStrLen = sprintf(acTxBuffer,"%s\n","INIT:IMM"); ComWrt (1, acTxBuffer, iStrLen); /* Wait for completion */ /* "*OPC?" prevents new commands from being accepted and */ /* returns 1 when measurement is complete */ iStrLen = sprintf(acTxBuffer,"%s\n","*OPC?"); ComWrt(1,acTxBuffer,iStrLen); /* Read one byte at a time, looking for "1" indicating measurement complete */ bDone = FALSE; while (!bDone) { if(GetInQLen(1)>0) { if (ComRdByte(1)==49) { /* "1" is ASCII 49 */ bDone=TRUE; ComRdByte(1); /* get the LF character, so it doesn't arrive after we flush the buffer later */ } } /* if(GetInQLen(1)>0) */ } /* while(!bDone) */ /* Flush input queue in preparation for receiving trace data */ FlushInQ (1); /* Request trace data */ iStrLen = sprintf(acTxBuffer,"%s\n","TRAC:DATA? TRACE1"); ComWrt(1,acTxBuffer,iStrLen); /* Wait for BYTES_PER_SWEEP bytes to show up in RX queue */ while (GetInQLen(1)%401f",y); /* "b4" tells Scan() to assume 4-byte (as opposed to default 8-byte) reals */ /* "z" means ignore fact that source array is type char, and typecast as necessary */ /* i6 means skip first 6 bytes of capture, which in this case is header information */ /* wait for output queue to empty */ while (GetOutQLen(1)>0) {} /* close COM1 */ CloseCom (1); /* Show Captured Data */ for (i=0;i