import java.awt.*; import java.io.*; import java.net.*; import java.applet.*; // This is a SCPI Demo to demonstrate how one can communicate with the // E4406A Transmitter Tester with a JAVA capable browser. This is the // Main class for the SCPI Demo. This applet will need Socks.class to // support the I/O commands and a ScpiDemo.html for a browser to load // the applet. // To use this applet, either compile this applet with a Java compiler // or use the existing compiled classes. copy ScpiDemo.class, // Socks.class and ScpiDemo.html to a floppy. Insert the floppy into // your instrument. Load up a browser on your computer and do the // following: // 1. Load this URL in your browser: // ftp:///int/ScpiDemo.html // 2. There should be two text windows show up in the browser: // The top one is the SCPI response text area for any response // coming back from the instrument. The bottom one is for you // to enter a SCPI command. Type in a SCPI command and hit enter. // If the command expects a response, it will show up in the top // window. public class ScpiDemo extends java.applet.Applet implements Runnable { Thread responseThread; Socks sck; URL appletBase; TextField scpiCommand = new TextField(); TextArea scpiResponse = new TextArea(10, 60); Panel southPanel = new Panel(); Panel p; // Initialize the applets public void init() { SetupSockets(); SetupPanels(); // Set up font type for both panels Font font = new Font("TimesRoman", Font.BOLD,14); scpiResponse.setFont(font); scpiCommand.setFont(font); scpiResponse.appendText("SCPI Demo Program: Response messages\n"); scpiResponse.appendText("-------------------------------------\n"); } // This routine is called whenever the applet is actived public void start() { // Open the sockets if not already opened sck.OpenSockets(); // Start a response thread StartResponseThread(true); } // This routine is called whenever the applet is out of scope // i.e. minimize browser public void stop() { // Close all local sockets sck.CloseSockets(); // Kill the response thread StartResponseThread(false); } // Action for sending out scpi commands // This routine is called whenever a command is received from the // SCPI command panel. public boolean action(Event evt, Object what) { // If this is the correct target if (evt.target == scpiCommand) { // Get the scpi command String str = scpiCommand.getText(); // Send it out to the Scpi socket sck.ScpiWriteLine(str); // Query for any error sck.ScpiWriteLine("syst:err?"); return true; } return false; } // Start/Stop a Response thread to display the response strings private void StartResponseThread(boolean start) { if (start) { // Start a response thread responseThread = new Thread(this); responseThread.start(); } else { // Kill the response thread responseThread = null; } } // Response thread running public void run() { String str = ""; // Initialize str to null // Clear the error queue before starting the thread // in case if there's any error messages from the previous actions while ( str.indexOf("No error") == -1 ) { sck.ScpiWriteLine("syst:err?"); str = sck.ScpiReadLine(); } // Start receiving response or error messages while(true) { str = sck.ScpiReadLine(); // If response messages is "No error", do no display it if ( str.indexOf("No error") == -1 ) { // Display the error message in the Response panel scpiResponse.appendText(str+"\n"); // Query for any error messages sck.ScpiWriteLine("syst:err?"); } } } // Set up and open the SCPI sockets private void SetupSockets() { // Get server url appletBase = (URL)getCodeBase(); // Open the sockets sck = new Socks(appletBase); } // Set up the SCPI command and response panels private void SetupPanels() { // Set up SCPI command panel southPanel.setLayout(new GridLayout(1, 1)); p = new Panel(); p.setLayout(new BorderLayout()); p.add("West", new Label("SCPI command:")); p.add("Center", scpiCommand); southPanel.add(p); // Set up the Response panel setLayout(new BorderLayout(2,2)); add("Center", scpiResponse); add("South", southPanel); } } // Socks class is responsible for open/close/read/write operations // from the predefined socket ports. For this example program, // the only port used is 5025 for the SCPI port. class Socks extends java.applet.Applet { // Socket Info // To add a new socket, add a constant here, // change MAX_NUM_OF_SOCKETS // then, edit the constructor for the new socket. public final int SCPI=0; private final int MAX_NUM_OF_SOCKETS=1; // Port number // 5025 is the dedicated port number for the e4406a's SCPI port private final int SCPI_PORT = 5025; // Socket info private URL appletBase; private Socket[] sock = new Socket[MAX_NUM_OF_SOCKETS]; private DataInputStream[] sockIn=new DataInputStream[MAX_NUM_OF_SOCKETS]; private PrintStream[] sockOut = new PrintStream[MAX_NUM_OF_SOCKETS]; private int[] port = new int[MAX_NUM_OF_SOCKETS]; private boolean[] sockOpen = new boolean[MAX_NUM_OF_SOCKETS]; // Constructor Socks(URL appletB) { appletBase = appletB; // Set up for port array. port[SCPI] = SCPI_PORT; // Initialize the sock array for ( int i = 0; i < MAX_NUM_OF_SOCKETS; i++ ) { sock[i] = null; sockIn[i] = null; sockOut[i] = null; sockOpen[i] = false; } } //***** Sockects open/close routines // Open the socket(s) if not already opened public void OpenSockets() { try { // Open each socket if possible for ( int i = 0; i < MAX_NUM_OF_SOCKETS; i++ ) { if ( !sockOpen[i] ) { sock[i] = new Socket(appletBase.getHost(),port[i]); sockIn[i]=new DataInputStream(sock[i].getInputStream()); sockOut[i]=new PrintStream(sock[i].getOutputStream()); if ( (sock[i] != null) && (sockIn[i] != null) && (sockOut[i] != null) ) { sockOpen[i] = true; } } } } catch (IOException e) { System.out.println("Sock, Open Error "+e.getMessage()); } } // Close the socket(s) if opened public void CloseSocket(int s) { try { if ( sockOpen[s] == true ) { // write blank line to exit servers elegantly sockOut[s].println(); sockOut[s].flush(); sockIn[s].close(); sockOut[s].close(); sock[s].close(); sockOpen[s] = false; } } catch (IOException e) { System.out.println("Sock, Close Error "+e.getMessage()); } } // Close all sockets public void CloseSockets() { for ( int i=0; i < MAX_NUM_OF_SOCKETS; i++ ) { CloseSocket(i); } } // Return the status of the socket, open or close. public boolean SockOpen(int s) { return sockOpen[s]; } //************* Socket I/O routines. //*** I/O routines for SCPI socket // Write an ASCII string with carriage return to SCPI socket public void ScpiWriteLine(String command) { if ( SockOpen(SCPI) ) { sockOut[SCPI].println(command); sockOut[SCPI].flush(); } } // Read an ASCII string, terminated with carriage return // from SCPI socket public String ScpiReadLine() { try { if ( SockOpen(SCPI) ) { return sockIn[SCPI].readLine(); } } catch (IOException e) { System.out.println("Scpi Read Line Error "+e.getMessage()); } return null; } // Read a byte from SCPI socket public byte ScpiReadByte() { try { if ( SockOpen(SCPI) ) { return sockIn[SCPI].readByte(); } } catch (IOException e) { System.out.println("Scpi Read Byte Error "+e.getMessage()); } return 0; } }