public String connect(String portName)
    {
        CommPortIdentifier portId = null;
        Enumeration<?> portEnum;
        
        try {
            portEnum = CommPortIdentifier.getPortIdentifiers();
        } catch (Exception e) {
            return e.getMessage();                          
        }

        // Iterate through list of serial ports.  Only continue if there is a match.
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }

        if (portId == null) {
            return "Could not find COM port.";
        }

        try {
            // Close port if it was previously open
            close(); 
            
            // open serial port, and use class name for the appName.
            serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);

            // set port parameters
            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            // open the streams
            input = serialPort.getInputStream();
            output = serialPort.getOutputStream();

            // add event listeners
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            // Return an error message if port can't be opened
            return e.toString();
        }
        return null; // No errors
    }