Appendix A

Till_Link

    /*
     * @(#)SerialDemo.java	1.9 98/06/05 SMI - ********name of original program*******
     *
     * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
     *
     * Sun grants you ("Licensee") a non-exclusive, royalty free, license
     * to use, modify and redistribute this software in source and binary
     * code form, provided that i) this copyright notice and license appear
     * on all copies of the software; and ii) Licensee does not utilize the
     * software in a manner which is disparaging to Sun.
     *
     * This software is provided "AS IS," without a warranty of any kind.
     * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
     * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
     * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
     * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
     * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
     * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
     * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
     * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
     * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
     *
     * This software is not designed or intended for use in on-line control
     * of aircraft, air traffic, aircraft navigation or aircraft
     * communications; or in the design, construction, operation or
     * maintenance of any nuclear facility. Licensee represents and
     * warrants that it will not use or redistribute the Software for such
     * purposes.
     */

    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // Till_Link 28/11/99 Stephen Wilkinson
    //
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    import javax.comm.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import java.util.Properties;
    import java.util.Enumeration;

    // main program

    public class Till_Link extends Frame {
        final int HEIGHT = 450;
        final int WIDTH = 450;
            
        private Panel messagePanel;
        private TextArea messageAreaOut;
        private TextArea messageAreaIn;
        
        private ConfigurationPanel configurationPanel;
        private SerialParameters parameters;
        private SerialConnection connection;
        
        private Properties props = null;
        
        //main method
        
        public static void main(String[] args) {
            Till_Link tillLink = new Till_Link(args);
            tillLink.setVisible(true);
            tillLink.repaint();
        } // end of main
        
        public Till_Link(String[] args) {
            super("Till Link");
            
            parameters = new SerialParameters();
            
            // set up GUI
            
            messagePanel = new Panel();
            messagePanel.setLayout(new GridLayout(2, 1));
            
            messageAreaOut = new TextArea();
            messageAreaOut.setEditable(false); 
            messagePanel.add(messageAreaOut);
            
            messageAreaIn = new TextArea();
            messageAreaIn.setEditable(false);
            messagePanel.add(messageAreaIn);
            
            add(messagePanel, "Center");
            
            configurationPanel = new ConfigurationPanel(this);
            
            Panel southPanel = new Panel();
            
            GridBagLayout gridBag = new GridBagLayout();
            GridBagConstraints cons = new GridBagConstraints();
            
            southPanel.setLayout(gridBag);
            
            cons.gridwidth = GridBagConstraints.REMAINDER;
            gridBag.setConstraints(configurationPanel, cons);
            cons.weightx = 1.0;
            southPanel.add(configurationPanel);
            
            add(southPanel, "South");
            
            connection = new SerialConnection(this, parameters, messageAreaOut, messageAreaIn);
            setConfigurationPanel();
            
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            
            setLocation(screenSize.width/2 - WIDTH/2, screenSize.height/2 - HEIGHT/2);
            
            setSize(WIDTH, HEIGHT);
        }
        
        
        // sets GUI for configurationPanel
            
        public void setConfigurationPanel() {
            configurationPanel.setConfigurationPanel();
        } // end of setConfigurationPanel
        
        private void setNewCursor(Cursor c) {
            setCursor(c);
            messageAreaIn.setCursor(c);
            messageAreaOut.setCursor(c);
        } // end of setNewCursor
        
        private void shutdown() {
            connection.closeConnection();
            System.exit(1);
        } // end of shutdown
        
        private void loadParams() {
            parameters.setPortName(props.getProperty("portName"));
            parameters.setBaudRate(props.getProperty("baudRate"));
            parameters.setFlowControlIn(props.getProperty("flowControlIn"));
            parameters.setFlowControlOut(props.getProperty("flowControlOut"));
            parameters.setParity(props.getProperty("parity"));
            parameters.setDatabits(props.getProperty("databits"));
            parameters.setStopbits(props.getProperty("stopbits"));
        } // end of loadParams
        
        class ConfigurationPanel extends Panel {
            private Frame parent;
            
            private Label portNameLabel;
            private TextField portTextField;
            
            private Label baudLabel;
            private TextField baudTextField;
            
            private Label flowControlInLabel;
            private TextField flowControlInTextField;
            
            private Label flowControlOutLabel;
            private TextField flowControlOutTextField;
            
            private Label databitsLabel;
            private TextField databitsTextField;
            
            private Label stopbitsLabel;
            private TextField stopbitsTextField;
            
            private Label parityLabel;
            private TextField parityTextField;
            
            public ConfigurationPanel(Frame parent) {
                this.parent = parent;
                
                setLayout(new GridLayout(4, 4));
                
                portNameLabel = new Label("Port Name:", Label.LEFT);
                add(portNameLabel);
                
                portTextField = new TextField();
                portTextField.setEditable(false);
                add(portTextField);
                
                baudLabel = new Label("Baud Rate:", Label.LEFT);
                add(baudLabel);
                
                baudTextField = new TextField();
                baudTextField.setEditable(false);
                add(baudTextField);
                
                flowControlInLabel = new Label("Flow Control In:", Label.LEFT);
                add(flowControlInLabel);
                
                flowControlInTextField = new TextField();
                flowControlInTextField.setEditable(false);
                add(flowControlInTextField);
                
                flowControlOutLabel = new Label("Flow Control Out:", Label.LEFT);
                add(flowControlOutLabel);
                
                flowControlOutTextField = new TextField();
                flowControlOutTextField.setEditable(false);
                add(flowControlOutTextField);
                
                databitsLabel = new Label("Data Bits:", Label.LEFT);
                add(databitsLabel);
                
                databitsTextField = new TextField();
                databitsTextField.setEditable(false);
                add(databitsTextField);
                
                stopbitsLabel = new Label("Stop Bits:", Label.LEFT);
                add(stopbitsLabel);
                
                stopbitsTextField = new TextField();
                stopbitsTextField.setEditable(false);
                add(stopbitsTextField);
                
                parityLabel = new Label("Parity:", Label.LEFT);
                add(parityLabel);
                
                parityTextField = new TextField();
                parityTextField.setEditable(false);
                add(parityTextField);
            } // end of ConfigurationPanel
            
            /*
            getConfiguration gets the required parameters for the link to work
            from a .properties file
            */
                  
            public void getConfiguration(){
                    
                    // sets up a new file object linked to the properties fiel
                    File f = new File("C:/Plusdata/Till_Link.properties");
                try {
                  FileInputStream fis = new FileInputStream(f);
                  props = new Properties();
                  props.load(fis);
                  fis.close();
                } catch (FileNotFoundException e1) {
                  System.err.println(e1);
                } catch (IOException e2) {
                      System.err.println(e2);
                }
                // calls the loadParams method
                loadParams();
                   
            } // end of getConfiguration
            
            public void openComPort(){
                // opens  comm port 
                Cursor previousCursor = getCursor();
                setNewCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                try {
                    connection.openConnection();   
                }catch (SerialConnectionException e2) {
                    
                    setNewCursor(previousCursor);
                    shutdown();
                    return;
                }
                setNewCursor(previousCursor);   
            } // end of openComPort
            
            public void setConfigurationPanel() {
                getConfiguration();
                portTextField.setText(parameters.getPortName());
                baudTextField.setText(parameters.getBaudRateString());
                flowControlInTextField.setText(parameters.getFlowControlInString());
                flowControlOutTextField.setText(parameters.getFlowControlOutString());
                databitsTextField.setText(parameters.getDatabitsString());
                stopbitsTextField.setText(parameters.getStopbitsString());
                parityTextField.setText(parameters.getParityString());
                openComPort();
            } // end of setConfigurationPanel
            
            class CloseHandler extends WindowAdapter {
                Till_Link sd;
                
                public CloseHandler(Till_Link sd) {
                    this.sd = sd;   
                } // end of CloseHandler
                
                public void windowClosing(WindowEvent e) {
                    sd.shutdown();   
                } // end of windowClosing
            } // end of CloseHandler class
        } // end of ConfigurationPanel class
    } // end of Till_Link.java
    


Back to Contents Back to Home Page

SerialConnection

     /* @(#)SerialConnection.java	1.6 98/07/17 SMI
     *
     * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
     *
     * Sun grants you ("Licensee") a non-exclusive, royalty free, license
     * to use, modify and redistribute this software in source and binary
     * code form, provided that i) this copyright notice and license appear
     * on all copies of the software; and ii) Licensee does not utilize the
     * software in a manner which is disparaging to Sun.
     *
     * This software is provided "AS IS," without a warranty of any kind.
     * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
     * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
     * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
     * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
     * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
     * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
     * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
     * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
     * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
     *
     * This software is not designed or intended for use in on-line control
     * of aircraft, air traffic, aircraft navigation or aircraft
     * communications; or in the design, construction, operation or
     * maintenance of any nuclear facility. Licensee represents and
     * warrants that it will not use or redistribute the Software for such
     * purposes.
     */
     
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     // altered by Stephen Wilkinson
     // 28/01/2000
     //
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
    import javax.comm.*;
    import java.net.*;
    import java.io.*;
    import java.awt.TextArea;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.util.TooManyListenersException;
    import java.lang.*;

    /**
    A class that handles the details of a serial connection. Reads from one 
    TextArea and writes to a second TextArea. 
    Holds the state of the connection.
    */
    public class SerialConnection implements SerialPortEventListener, 
               CommPortOwnershipListener 
    {
        private Till_Link parent;

        private TextArea messageAreaOut;
        private TextArea messageAreaIn;
        private SerialParameters parameters;
        private OutputStream os;
        private InputStream is;
        private KeyHandler keyHandler;
        private OutputStream output;
        private InputStream input;
        private CommPortIdentifier portId;
        private SerialPort sPort;
        
       
        
        //final static int BufferSize = 2048;
        private boolean open;

        /**
        Creates a SerialConnection object and initilizes variables passed in
        as params.

        @param parent A Till_Link object.
        @param parameters A SerialParameters object.
        @param messageAreaOut The TextArea that messages that are to be sent out
        of the serial port are entered into.
        @param messageAreaIn The TextArea that messages comming into the serial
        port are displayed on.
        */
        public SerialConnection(Till_Link parent,
              SerialParameters parameters,
              TextArea messageAreaOut,
              TextArea messageAreaIn) 
      {
          this.parent = parent;
          this.parameters = parameters;
          this.messageAreaOut = messageAreaOut;
          this.messageAreaIn = messageAreaIn;
          open = false;
        }

       /**
       Attempts to open a serial connection and streams using the parameters
       in the SerialParameters object. If it is unsuccesfull at any step it
       returns the port to a closed state, throws a 
       SerialConnectionException, and returns.

       Gives a timeout of 30 seconds on the portOpen to allow other applications
       to reliquish the port if have it open and no longer need it.
       */
       public void openConnection() throws SerialConnectionException 
       {

          // Obtain a CommPortIdentifier object for the port you want to open.
          try 
          {
              portId = 
            CommPortIdentifier.getPortIdentifier(parameters.getPortName());
          } catch (NoSuchPortException e) 
          {
              throw new SerialConnectionException(e.getMessage());
          }

          // Open the port represented by the CommPortIdentifier object. Give
          // the open call a relatively long timeout of 30 seconds to allow
          // a different application to reliquish the port if the user 
          // wants to.
          try 
          {
              sPort = (SerialPort)portId.open("Till_Link", 30000);
          } catch (PortInUseException e) 
          {
              throw new SerialConnectionException(e.getMessage());
          }

          // Set the parameters of the connection. If they won't set, close the
          // port before throwing an exception.
          try 
          {
              setConnectionParameters();
          } catch (SerialConnectionException e) 
          {	
              sPort.close();
              throw e;
          }

          // Open the input and output streams for the connection. If they won't
          // open, close the port before throwing an exception.
          try 
          {
              os = sPort.getOutputStream();
              is = sPort.getInputStream();
          } catch (IOException e) 
          {
              sPort.close();
              throw new SerialConnectionException("Error opening i/o streams");
          }
            
            
           dataTransfer(); // calls the method for doing sending data from network connection
                            // to Comm Port & vice versa
            
            /*
            
          // Create a new KeyHandler to respond to key strokes in the 
          // messageAreaOut. Add the KeyHandler as a keyListener to the 
          // messageAreaOut.
          keyHandler = new KeyHandler(os);
          messageAreaOut.addKeyListener(keyHandler);

            */

          // Add this object as an event listener for the serial port.
          try 
          {
              sPort.addEventListener(this);
          } catch (TooManyListenersException e) 
          {
              sPort.close();
              throw new SerialConnectionException("too many listeners added");
          }

          // Set notifyOnDataAvailable to true to allow event driven input.
          sPort.notifyOnDataAvailable(true);

          // Set notifyOnBreakInterrup to allow event driven break handling.
          sPort.notifyOnBreakInterrupt(true);

          // Set receive timeout to allow breaking out of polling loop during
          // input handling.
          try 
          {
              sPort.enableReceiveTimeout(30);
          } catch (UnsupportedCommOperationException e) 
          {
            }

          // Add ownership listener to allow ownership event handling.
          portId.addPortOwnershipListener(this);

          open = true;
       }

            /**
        Sets the connection parameters to the setting in the parameters object.
        If set fails return the parameters object to origional settings and
        throw exception.
        */
        public void setConnectionParameters() throws SerialConnectionException 
        {

          // Save state of parameters before trying a set.
          int oldBaudRate = sPort.getBaudRate();
          int oldDatabits = sPort.getDataBits();
          int oldStopbits = sPort.getStopBits();
          int oldParity   = sPort.getParity();
          int oldFlowControl = sPort.getFlowControlMode();

          // Set connection parameters, if set fails return parameters object
          // to original state.
          try 
          {
              sPort.setSerialPortParams(parameters.getBaudRate(),
                  parameters.getDatabits(),
                  parameters.getStopbits(),
                  parameters.getParity());
          } catch (UnsupportedCommOperationException e) 
          {
              parameters.setBaudRate(oldBaudRate);
              parameters.setDatabits(oldDatabits);
              parameters.setStopbits(oldStopbits);
              parameters.setParity(oldParity);
              throw new SerialConnectionException("Unsupported parameter");
          }

          // Set flow control.
          try 
          {
              sPort.setFlowControlMode(parameters.getFlowControlIn() 
                     | parameters.getFlowControlOut());
          } catch (UnsupportedCommOperationException e) 
          {
              throw new SerialConnectionException("Unsupported flow control");
          }
        }

        /**
        Close the port and clean up associated elements.
        */
        public void closeConnection() 
        {
          // If port is alread closed just return.
          if (!open) 
          {
              return;
          }

          // Remove the key listener.
          messageAreaOut.removeKeyListener(keyHandler);

          // Check to make sure sPort has reference to avoid a NPE.
          if (sPort != null) 
          {
              try 
              {
                // close the i/o streams.
                os.close();
                is.close();
              } catch (IOException e) 
              {
                System.err.println(e);
              }

              // Close the port.
              sPort.close();
             
              // Remove the ownership listener.
              portId.removePortOwnershipListener(this);
          }

          open = false;
        }

        /**
        Send a one second break signal.
        */
        public void sendBreak() 
        {
          sPort.sendBreak(1000);
        }

        /**
        Reports the open status of the port.
        @return true if port is open, false if port is closed.
        */
        public boolean isOpen() 
        {
          return open;
        }

        /**
        Handles SerialPortEvents. The two types of SerialPortEvents that this
        program is registered to listen for are DATA_AVAILABLE and BI. During 
        DATA_AVAILABLE the port buffer is read until it is drained, when no more
        data is availble and 30ms has passed the method returns. When a BI
        event occurs the words BREAK RECEIVED are written to the messageAreaIn.
        */

        public void serialEvent(SerialPortEvent e) 
        {
          // Create a StringBuffer and int to receive input data.
          StringBuffer inputBuffer = new StringBuffer();
          int newData = 0;

          // Determine type of event.
          switch (e.getEventType()) 
          {

              // Read data until -1 is returned. If \r is received substitute
              // \n for correct newline handling.
              case SerialPortEvent.DATA_AVAILABLE:
                while (newData != -1) 
                {
                  try 
                  {
                      newData = is.read();
                      if (newData == -1) 
                      {
                        break;
                      }
                      if ('\r' == (char)newData) 
                      {
                          inputBuffer.append('\n');
                      } else 
                      {
                        inputBuffer.append((char)newData);
                      }
                  } catch (IOException ex) 
                  {
                      System.err.println(ex);
                      return;
                    }
                  }

            // Append received data to messageAreaIn.
            messageAreaIn.append(new String(inputBuffer));
            break;

              // If break event append BREAK RECEIVED message.
              case SerialPortEvent.BI:
            messageAreaIn.append("\n--- BREAK RECEIVED ---\n");
          }

        }   

        /**
        Handles ownership events. If a PORT_OWNERSHIP_REQUESTED event is
        received a dialog box is created asking the user if they are 
        willing to give up the port. No action is taken on other types
        of ownership events.
        */
        public void ownershipChange(int type) 
        {
          if (type == CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED) 
          {
              PortRequestedDialog prd = new PortRequestedDialog(parent);
          }
        }
        
        /**
        Handles all the data communications. Creates a network socket and sends
        data to the Comm Port and then when a control character is received, 
        sends data from Comm Port to the network socket.
        @param server ServerSocket object.
        @param connection Socket object.
        @param request String object.
        @param sendingData true when sending data to Comm Port, false when receiving data.
        @param stopLink false when link is running. when set to true link exits.
        */
        public void dataTransfer(){
            // new server socket connection
            ServerSocket server;
            
            // new Socket
            Socket connection;
            
            // string for holding data from stream
            String request;
            
            // flags for changing the direction of data flow re: serial port and TMS-15 client program
            boolean sendingData = true;
            boolean stopLink = false;
                      
                      
            try {
                // sets up server socket on port 5000
                server = new ServerSocket(5000, 100);
                // creates a new connection when server socket is hit
                connection = server.accept();
                // creates a new InputStream linked to the socket
                InputStream input = new BufferedInputStream(connection.getInputStream());
                // sets up the output stream connected to the server socket
                output = connection.getOutputStream();
                // sets up to string buffers, one for input, the other for output
                StringBuffer inputDataBuffer = new StringBuffer();
              StringBuffer outputDataBuffer = new StringBuffer();
              // sets data to 0
              int newInputData = 0;
                int newOutputData = 0;
                // sets a character enabling its value to be obtained
                Character ch = new Character('a');
                
              // creates a loop that doesn't finish
              while(stopLink != true) {
                  // if data should be obtained from the server socket and sent to the serial port..
                  if(sendingData == true){
                      while(newInputData != -1){
                          try {
                            newInputData = input.read();
                          if (newInputData == -1) {
                              sendingData = false;
                              messageAreaOut.appendText(" " + Integer.toString(newInputData));
                              break;
                          }else if(newInputData == 4){
                              // end of data coming from client, get data from serial port
                              sendingData = false;
                              messageAreaOut.appendText(" " + Integer.toString(newInputData));
                              os.write(newInputData);
                              break;
                          }else{
                              // writes data to the serial port
                              inputDataBuffer.append((char)newInputData);
                              os.write(newInputData);
                              messageAreaOut.appendText(" " + Integer.toString(newInputData));
                          }
                        }catch (IOException ex) {
                            System.err.println(ex);
                            return;
                        }
                      }
                  }
                  // if data should be obtained from the serial port and sent to the server socket..
                  if(sendingData == false){
                      while(newOutputData != -1) {
                          try {
                          newOutputData = is.read();
                          ch = new Character((char)newOutputData);
                          // on end of message the link gets data from server socket and sends to the serial port
                          if (newOutputData == 4) {
                              sendingData = true;
                              outputDataBuffer.append((char)newOutputData);
                            output.write(newOutputData);
                            messageAreaIn.appendText(" " + Integer.toString(newOutputData));
                              break;
                          }else {
                            outputDataBuffer.append((char)newOutputData);
                            output.write(newOutputData);
                            messageAreaIn.appendText(" "+ Integer.toString(newOutputData));
                          }
                        }catch (IOException ex) {
                          System.err.println(ex);
                          return;
                        }
                    }
                }
              }
          
              System.exit(0);
             
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
        

        /**
        A class to handle KeyEvents generated by the messageAreaOut.
        When a KeyEvent occurs the char that is 
        generated by the event is read, converted to an int and 
        writen to the OutputStream for the port.
        */
        class KeyHandler extends KeyAdapter 
        {
          OutputStream os;

          /**
          Creates the KeyHandler.
          @param os The OutputStream for the port.
          */
          public KeyHandler(OutputStream os) 
          {
              super();
              this.os = os;
          }

          /**
          Handles the KeyEvent.
          Gets the char generated by the KeyEvent,
          converts it to an int, writes it to the 
          OutputStream for the port.
          */
            public void keyTyped(KeyEvent evt) 
            {
                char newCharacter = evt.getKeyChar();
              try 
              {
                os.write((int)newCharacter);
              } catch (IOException e) 
              {
                System.err.println("OutputStream write error: " + e);
              }
            }
        }
    }
    


Back to Contents Back to Home Page

SerialConnectionException

     /* @(#)SerialConnectionException.java	1.3 98/06/04 SMI
     *
     * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
     *
     * Sun grants you ("Licensee") a non-exclusive, royalty free, license
     * to use, modify and redistribute this software in source and binary
     * code form, provided that i) this copyright notice and license appear
     * on all copies of the software; and ii) Licensee does not utilize the
     * software in a manner which is disparaging to Sun.
     *
     * This software is provided "AS IS," without a warranty of any kind.
     * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
     * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
     * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
     * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
     * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
     * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
     * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
     * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
     * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
     *
     * This software is not designed or intended for use in on-line control
     * of aircraft, air traffic, aircraft navigation or aircraft
     * communications; or in the design, construction, operation or
     * maintenance of any nuclear facility. Licensee represents and
     * warrants that it will not use or redistribute the Software for such purposes.
     */
     
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     // no changes made to this class
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      
    public class SerialConnectionException extends Exception 
    {

        /**
         * Constructs a SerialConnectionException
         * with the specified detail message.
         *
         * @param   s   the detail message.
         */
        public SerialConnectionException(String str) 
        {
            super(str);
        }

        /**
         * Constructs a SerialConnectionException
         * with no detail message.
         */
        public SerialConnectionException() 
        {
            super();
        }
    }
    


Back to Contents Back to Home Page

SerialParameters

    /* @(#)SerialParameters.java	1.5 98/07/17 SMI
     *
     * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
     *
     * Sun grants you ("Licensee") a non-exclusive, royalty free, license
     * to use, modify and redistribute this software in source and binary
     * code form, provided that i) this copyright notice and license appear
     * on all copies of the software; and ii) Licensee does not utilize the
     * software in a manner which is disparaging to Sun.
     *
     * This software is provided "AS IS," without a warranty of any kind.
     * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
     * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
     * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
     * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
     * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
     * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
     * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
     * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
     * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
     *
     * This software is not designed or intended for use in on-line control
     * of aircraft, air traffic, aircraft navigation or aircraft
     * communications; or in the design, construction, operation or
     * maintenance of any nuclear facility. Licensee represents and
     * warrants that it will not use or redistribute the Software for such
     * purposes.
     */
     
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     // altered by Stephen Wilkinson
     // 28/01/2000
     //
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
    import javax.comm.*;

    /**
    A class that stores parameters for serial ports. 
    */
    public class SerialParameters 
    {

        private String portName;
        private int baudRate;
        private int flowControlIn;
        private int flowControlOut;
        private int databits;
        private int stopbits;
        private int parity;
       
        /**
        Default constructer. Sets parameters to no port, 2400 baud, no flow 
        control, 8 data bits, 1 stop bit, no parity.
        */
        public SerialParameters () 
        {
          this("", // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
               2400, // <- this was altered to set the default to 2400, even 
              SerialPort.FLOWCONTROL_NONE, // though the parameters are read 
              SerialPort.FLOWCONTROL_NONE, //in from a file
              SerialPort.DATABITS_8, // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
              SerialPort.STOPBITS_1,
              SerialPort.PARITY_NONE);
           
             
        }		

        /**
        Paramaterized constructer.

        @param portName The name of the port.
        @param baudRate The baud rate.
        @param flowControlIn Type of flow control for receiving.
        @param flowControlOut Type of flow control for sending.
        @param databits The number of data bits.
        @param stopbits The number of stop bits.
        @param parity The type of parity.
        @param propertiesPath The path the the Till_Link.properties file
        */
        public SerialParameters(String portName, 
              int baudRate,
              int flowControlIn,
              int flowControlOut,
              int databits,
              int stopbits,
              int parity) 
      {

          this.portName = portName;
          this.baudRate = baudRate;
          this.flowControlIn = flowControlIn;
          this.flowControlOut = flowControlOut;
          this.databits = databits;
          this.stopbits = stopbits;
          this.parity = parity;
          
        }

        /**
        Sets port name.
        @param portName New port name.
        */
        public void setPortName(String portName) 
        {
          this.portName = portName;
        }

        /**
        Gets port name.
        @return Current port name.
        */
        public String getPortName() 
        {
          return portName;
        }

        /**
        Sets baud rate.
        @param baudRate New baud rate.
        */
        public void setBaudRate(int baudRate) 
        {
          this.baudRate = baudRate;
        }

        /**
        Sets baud rate.
        @param baudRate New baud rate.
        */
        public void setBaudRate(String baudRate) 
        {
          this.baudRate = Integer.parseInt(baudRate);
        }

        /**
        Gets baud rate as an int.
        @return Current baud rate.
        */
        public int getBaudRate() 
        {
          return baudRate;
        }

        /**
        Gets baud rate as a String.
        @return Current baud rate.
        */
        public String getBaudRateString() 
        {
          return Integer.toString(baudRate);
        }

        /**
        Sets flow control for reading.
        @param flowControlIn New flow control for reading type.
        */
        public void setFlowControlIn(int flowControlIn) 
        {
          this.flowControlIn = flowControlIn;
        }
        
        /**
        Sets flow control for reading.
        @param flowControlIn New flow control for reading type.
        */
        public void setFlowControlIn(String flowControlIn) 
        {
          this.flowControlIn = stringToFlow(flowControlIn);
        }

        /** 
        Gets flow control for reading as an int.
        @return Current flow control type.
        */
        public int getFlowControlIn() 
        {
          return flowControlIn;
        }

        /** 
        Gets flow control for reading as a String.
        @return Current flow control type.
        */
        public String getFlowControlInString() 
        {
          return flowToString(flowControlIn);
        }

        /**
        Sets flow control for writing.
        @param flowControlIn New flow control for writing type.
        */
        public void setFlowControlOut(int flowControlOut) 
        {
          this.flowControlOut = flowControlOut;
        }

        /**
        Sets flow control for writing.
        @param flowControlIn New flow control for writing type.
        */
        public void setFlowControlOut(String flowControlOut) 
        {
          this.flowControlOut = stringToFlow(flowControlOut);
        }

        /** 
        Gets flow control for writing as an int.
        @return Current flow control type.
        */
        public int getFlowControlOut() 
        {
          return flowControlOut;
        }

        /** 
        Gets flow control for writing as a String.
        @return Current flow control type.
        */
        public String getFlowControlOutString() 
        {
          return flowToString(flowControlOut);
        }

        /** 
        Sets data bits.
        @param databits New data bits setting.
        */
        public void setDatabits(int databits) 
        {
          this.databits = databits;
        }

        /** 
        Sets data bits.
        @param databits New data bits setting.
        */
        public void setDatabits(String databits) 
        {
          if (databits.equals("5")) 
          {
              this.databits = SerialPort.DATABITS_5;
          }
          if (databits.equals("6")) 
          {
              this.databits = SerialPort.DATABITS_6;
          }
          if (databits.equals("7")) 
          {
              this.databits = SerialPort.DATABITS_7;
          }
          if (databits.equals("8")) 
          {
              this.databits = SerialPort.DATABITS_8;
          }
        }

        /**
        Gets data bits as an int.
        @return Current data bits setting.
        */
        public int getDatabits() 
        {
          return databits;
        }

        /**
        Gets data bits as a String.
        @return Current data bits setting.
        */
        public String getDatabitsString() 
        {
          switch(databits) 
          {
              case SerialPort.DATABITS_5:
            return "5";
              case SerialPort.DATABITS_6:
            return "6";
              case SerialPort.DATABITS_7:
            return "7";
              case SerialPort.DATABITS_8:
            return "8";
              default:
            return "8";
          }
        }

        /**
        Sets stop bits.
        @param stopbits New stop bits setting.
        */
        public void setStopbits(int stopbits) 
        {
          this.stopbits = stopbits;
        }

        /**
        Sets stop bits.
        @param stopbits New stop bits setting.
        */
        public void setStopbits(String stopbits) 
        {
          if (stopbits.equals("1")) 
          {
              this.stopbits = SerialPort.STOPBITS_1;
          }
          if (stopbits.equals("1.5")) 
          {
              this.stopbits = SerialPort.STOPBITS_1_5;
          }
          if (stopbits.equals("2")) 
          {
              this.stopbits = SerialPort.STOPBITS_2;
          }
        }

        /**
        Gets stop bits setting as an int.
        @return Current stop bits setting.
        */
        public int getStopbits() 
        {
          return stopbits;
        }

        /**
        Gets stop bits setting as a String.
        @return Current stop bits setting.
        */
        public String getStopbitsString() 
        {
          switch(stopbits) 
          {
              case SerialPort.STOPBITS_1:
            return "1";
              case SerialPort.STOPBITS_1_5:
            return "1.5";
              case SerialPort.STOPBITS_2:
            return "2";
              default:
            return "1";
          }
        }

        /**
        Sets parity setting.
        @param parity New parity setting.
        */
        public void setParity(int parity) 
        {
          this.parity = parity;
        }

        /**
        Sets parity setting.
        @param parity New parity setting.
        */
        public void setParity(String parity) 
        {
          if (parity.equals("None")) 
          {
              this.parity = SerialPort.PARITY_NONE;
          }
          if (parity.equals("Even")) 
          {
              this.parity = SerialPort.PARITY_EVEN;
          }
          if (parity.equals("Odd")) 
          {
              this.parity = SerialPort.PARITY_ODD;
          }
        }

        /**
        Gets parity setting as an int.
        @return Current parity setting.
        */
        public int getParity() 
        {
          return parity;
        }

        /**
        Gets parity setting as a String.
        @return Current parity setting.
        */
        public String getParityString() 
        {
          switch(parity) 
          {
              case SerialPort.PARITY_NONE:
            return "None";
              case SerialPort.PARITY_EVEN:
            return "Even";
              case SerialPort.PARITY_ODD:
            return "Odd";
              default:
            return "None";
          }
        }

        /**
        Converts a String describing a flow control type to an
        int type defined in SerialPort.
        @param flowControl A string describing a flow control type.
        @return An int describing a flow control type.
        */
        private int stringToFlow(String flowControl) 
        {
          if (flowControl.equals("None")) 
          {
              return SerialPort.FLOWCONTROL_NONE;
          }
          if (flowControl.equals("Xon/Xoff Out")) 
          {
              return SerialPort.FLOWCONTROL_XONXOFF_OUT;
          }
          if (flowControl.equals("Xon/Xoff In")) 
          {
              return SerialPort.FLOWCONTROL_XONXOFF_IN;
          }
          if (flowControl.equals("RTS/CTS In")) 
          {
              return SerialPort.FLOWCONTROL_RTSCTS_IN;
          }
          if (flowControl.equals("RTS/CTS Out")) 
          {
              return SerialPort.FLOWCONTROL_RTSCTS_OUT;
          }
          return SerialPort.FLOWCONTROL_NONE;
        }

        /**
        Converts an int describing a flow control type to a 
        String describing a flow control type.
        @param flowControl An int describing a flow control type.
        @return A String describing a flow control type.
        */
        String flowToString(int flowControl) 
        {
          switch(flowControl) 
          {
              case SerialPort.FLOWCONTROL_NONE:
            return "None";
              case SerialPort.FLOWCONTROL_XONXOFF_OUT:
            return "Xon/Xoff Out";
              case SerialPort.FLOWCONTROL_XONXOFF_IN:
            return "Xon/Xoff In";
              case SerialPort.FLOWCONTROL_RTSCTS_IN:
            return "RTS/CTS In";
              case SerialPort.FLOWCONTROL_RTSCTS_OUT:
            return "RTS/CTS Out";
              default:
            return "None";
          }
        }
    }
    


Back to Contents Back to Home Page

PortRequestedDialog

    /*
     * @(#)PortRequestedDialog.java	1.3 98/06/04 SMI
     *
     * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
     *
     * Sun grants you ("Licensee") a non-exclusive, royalty free, license
     * to use, modify and redistribute this software in source and binary
     * code form, provided that i) this copyright notice and license appear
     * on all copies of the software; and ii) Licensee does not utilize the
     * software in a manner which is disparaging to Sun.
     *
     * This software is provided "AS IS," without a warranty of any kind.
     * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
     * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
     * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
     * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
     * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
     * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
     * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
     * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
     * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
     *
     * This software is not designed or intended for use in on-line control
     * of aircraft, air traffic, aircraft navigation or aircraft
     * communications; or in the design, construction, operation or
     * maintenance of any nuclear facility. Licensee represents and
     * warrants that it will not use or redistribute the Software for such
     * purposes.
     */
     //************************************
     // nothing changed
     //************************************ 
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.comm.*;

    /**
    Informs the user that an other application has requested the port they 
    are using, and then asks if they are willing to give it up. If the user
    answers "Yes" the port is closed and the dialog is closed, if the user 
    answers "No" the dialog closes and no other action is taken.
    */
    public class PortRequestedDialog extends Dialog implements ActionListener 
    {
        
        private Till_Link parent;

        /**
        Creates the a dialog with two buttons and a message asking the user if 
        they are willing to give up the port they are using.

        @param parent The main Till_Link object.
        */
        public PortRequestedDialog(Till_Link parent) 
        {
          super(parent, "Port Requested!", true);
          this.parent = parent;

          String lineOne = "Your port has been requested";
          String lineTwo = "by an other application.";
          String lineThree = "Do you want to give up your port?";
          Panel labelPanel = new Panel();
          labelPanel.setLayout(new GridLayout(3, 1));
          labelPanel.add(new Label(lineOne, Label.CENTER));
          labelPanel.add(new Label(lineTwo, Label.CENTER));
          labelPanel.add(new Label(lineThree, Label.CENTER));
          add(labelPanel, "Center");

          Panel buttonPanel = new Panel();
          Button yesButton = new Button("Yes");
          yesButton.addActionListener(this);
          buttonPanel.add(yesButton);
          Button noButton = new Button("No");
          noButton.addActionListener(this);
          buttonPanel.add(noButton);
          add(buttonPanel, "South");

          FontMetrics fm = getFontMetrics(getFont());
          int width = Math.max(fm.stringWidth(lineOne), 
         Math.max(fm.stringWidth(lineTwo), fm.stringWidth(lineThree)));

          setSize(width + 40, 150);
          setLocation(parent.getLocationOnScreen().x + 30, 
            parent.getLocationOnScreen().y + 30);
          setVisible(true);
        }
        
        /*
        Handles events generated by the buttons. If the yes button in pushed the
        port closing routine is called and the dialog is disposed of. If the "No"
        button is pushed the dialog is disposed of.
        */
       
        public void actionPerformed(ActionEvent e) 
        {
          String cmd = e.getActionCommand();

            /*
          if (cmd.equals("Yes")) 
          {
            parent.portClosed();
          }
        */	
          setVisible(false);
          dispose();
        }
      
    }
    


Back to Contents Back to Home Page

AlertDialog

    /*
     * @(#)AlertDialog.java	1.3 98/06/04 SMI
     *
     * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
     *
     * Sun grants you ("Licensee") a non-exclusive, royalty free, license
     * to use, modify and redistribute this software in source and binary
     * code form, provided that i) this copyright notice and license appear
     * on all copies of the software; and ii) Licensee does not utilize the
     * software in a manner which is disparaging to Sun.
     *
     * This software is provided "AS IS," without a warranty of any kind.
     * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
     * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
     * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
     * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
     * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
     * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
     * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
     * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
     * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
     *
     * This software is not designed or intended for use in on-line control
     * of aircraft, air traffic, aircraft navigation or aircraft
     * communications; or in the design, construction, operation or
     * maintenance of any nuclear facility. Licensee represents and
     * warrants that it will not use or redistribute the Software for such
     * purposes.
     */
     //***********************************
     // nothing changed
     //***********************************
     
    import java.awt.*;
    import java.awt.event.*;

    /**
    A single response modal alert dialog. This class is configurable for message 
    and title. The width of the dialog will be longer than the longest message 
    line.  When the OK button is pressed the dialog returns.
    */
    public class AlertDialog extends Dialog implements ActionListener 
    {

        /**
        Creates a new AlertDialog with three lines of message and
        a title.

        @param parent Any Frame.
        @param title The title to appear in the border of the dialog.
        @param lineOne The first line of the message in the dialog.
        @param lineTwo The second line of the message in the dialog.
        @param lineThree The third line of the message in the dialog.
        */
        public AlertDialog(Frame parent, 
               String title, 
               String lineOne, 
               String lineTwo,
               String lineThree) 
        {
          super(parent, title, true);

          Panel labelPanel = new Panel();
          labelPanel.setLayout(new GridLayout(3, 1));
          labelPanel.add(new Label(lineOne, Label.CENTER));
          labelPanel.add(new Label(lineTwo, Label.CENTER));
          labelPanel.add(new Label(lineThree, Label.CENTER));
          add(labelPanel, "Center");

          Panel buttonPanel = new Panel();
          Button okButton = new Button("OK");
          okButton.addActionListener(this);
          buttonPanel.add(okButton);
          add(buttonPanel, "South");

          FontMetrics fm = getFontMetrics(getFont());
          int width = Math.max(fm.stringWidth(lineOne), 
         Math.max(fm.stringWidth(lineTwo), fm.stringWidth(lineThree)));

          setSize(width + 40, 150);
          setLocation(parent.getLocationOnScreen().x + 30, 
            parent.getLocationOnScreen().y + 30);
          setVisible(true);
        }

        /**
        Handles events from the OK button. When OK is pressed the dialog becomes
        invisible, disposes of its self, and retruns.
        */
        public void actionPerformed(ActionEvent e) 
        {
          setVisible(false);
          dispose();
        }
    }
    


Back to Contents


logo - Valid XHTML 1.0!Valid CSS!Best viewed inMozilla FirefoxMozilla Firefox