IoT DAS Frameworks - Solar Plant Monitoring and Analytics/IOT_Gateway/Fronius_Protocol_Handler_Framework/FroniusConnector/src/serialportapp/SerialConnection.java
public void openConnection() throws SerialConnectionException {

        // Obtain a CommPortIdentifier object for the port you want to open.
        //logger.debug("In openConnection(): " + parameters.getPortName());
        try {
            //logger.debug("In openConnection(): inside try..catch block "
                    //+ parameters.getPortName());
            portId = CommPortIdentifier.getPortIdentifier(parameters
                    .getPortName());
           // logger.debug("In openConnection(): inside try block,portid=  "
                    //+ portId.getPortType());
            /*
             * portList = CommPortIdentifier.getPortIdentifiers();
             * 
             * while (portList.hasMoreElements()) {
             * System.out.println("here..1"); portId = (CommPortIdentifier)
             * portList.nextElement(); if (portId.getPortType() ==
             * CommPortIdentifier.PORT_SERIAL) { // if
             * (portId.getName().equals("COM1")) { if
             * (portId.getName().equals(parameters.getPortName())) {
             * System.out.println("here..2"); portId =
             * CommPortIdentifier.getPortIdentifier(parameters.getPortName());
             * System.out.println("here..3"); } } }
             */

        } catch (NoSuchPortException e) {
            e.printStackTrace();
            throw new SerialConnectionException(e.getMessage());
        } catch (Exception e1) {
            e1.printStackTrace();
            logger.debug("Exception" + e1);
            throw new SerialConnectionException(e1.getMessage());
        }

        //logger.debug("In openConnection(): port type= "
                //+ portId.getPortType());

        // 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("SerialPortApp", 2000);
            sPort = (SerialPort) portId.open("SerialPortApp", openPortTimeout);
        } catch (PortInUseException e) {
            throw new SerialConnectionException(e.getMessage());
        } catch (Exception 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");
        }

        // 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(3000);
        } catch (UnsupportedCommOperationException e) {
        }

        open = true;
        // System.out.println("In openConnection(): open flag-" + open);
    }
/**
     * 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
     * <code>SerialConnectionException</code>, 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.
     */
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.
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 {
        try {
            portId = CommPortIdentifier.getPortIdentifier(parameters
                    .getPortName());
            /*
             * portList = CommPortIdentifier.getPortIdentifiers();
             * 
             * while (portList.hasMoreElements()) {
             * System.out.println("here..1"); portId = (CommPortIdentifier)
             * portList.nextElement(); if (portId.getPortType() ==
             * CommPortIdentifier.PORT_SERIAL) { // if
             * (portId.getName().equals("COM1")) { if
             * (portId.getName().equals(parameters.getPortName())) {
             * System.out.println("here..2"); portId =
             * CommPortIdentifier.getPortIdentifier(parameters.getPortName());
             * System.out.println("here..3"); } } }
             */
        } catch (NoSuchPortException e) {
            e.printStackTrace();
            throw new SerialConnectionException(e.getMessage());
        } catch (Exception e1) {
            e1.printStackTrace();
            logger.debug("Exception" + e1);
            throw new SerialConnectionException(e1.getMessage());
        }
        try {
            sPort = (SerialPort) portId.open("SerialPortApp", openPortTimeout);
        } catch (PortInUseException e) {
            throw new SerialConnectionException(e.getMessage());
        } catch (Exception e) {
            throw new SerialConnectionException(e.getMessage());
        }
        try {
            setConnectionParameters();
        } catch (SerialConnectionException e) {
            sPort.close();
            throw e;
        }
        try {
            os = sPort.getOutputStream();
            is = sPort.getInputStream();
        } catch (IOException e) {
            sPort.close();
            throw new SerialConnectionException("Error opening i/o streams");
        }
        /*try {
         sPort.addEventListener(this);
         } catch (TooManyListenersException e) {
         sPort.close();
         throw new SerialConnectionException("too many listeners added");
         }*/
        sPort.notifyOnDataAvailable(true);
        sPort.notifyOnBreakInterrupt(true);
        try {
            sPort.enableReceiveTimeout(3000);
        } catch (UnsupportedCommOperationException e) {
        }
        open = true;
    }
public void openConnection ( ) throws SerialConnectionException { try { portId = CommPortIdentifier . getPortIdentifier ( parameters . getPortName ( ) ) ; /*
             * portList = CommPortIdentifier.getPortIdentifiers();
             * 
             * while (portList.hasMoreElements()) {
             * System.out.println("here..1"); portId = (CommPortIdentifier)
             * portList.nextElement(); if (portId.getPortType() ==
             * CommPortIdentifier.PORT_SERIAL) { // if
             * (portId.getName().equals("COM1")) { if
             * (portId.getName().equals(parameters.getPortName())) {
             * System.out.println("here..2"); portId =
             * CommPortIdentifier.getPortIdentifier(parameters.getPortName());
             * System.out.println("here..3"); } } }
             */ } catch ( NoSuchPortException e ) { e . printStackTrace ( ) ; throw new SerialConnectionException ( e . getMessage ( ) ) ; } catch ( Exception e1 ) { e1 . printStackTrace ( ) ; logger . debug ( "Exception" + e1 ) ; throw new SerialConnectionException ( e1 . getMessage ( ) ) ; } try { sPort = ( SerialPort ) portId . open ( "SerialPortApp" , openPortTimeout ) ; } catch ( PortInUseException e ) { throw new SerialConnectionException ( e . getMessage ( ) ) ; } catch ( Exception e ) { throw new SerialConnectionException ( e . getMessage ( ) ) ; } try { setConnectionParameters ( ) ; } catch ( SerialConnectionException e ) { sPort . close ( ) ; throw e ; } try { os = sPort . getOutputStream ( ) ; is = sPort . getInputStream ( ) ; } catch ( IOException e ) { sPort . close ( ) ; throw new SerialConnectionException ( "Error opening i/o streams" ) ; } /*try {
         sPort.addEventListener(this);
         } catch (TooManyListenersException e) {
         sPort.close();
         throw new SerialConnectionException("too many listeners added");
         }*/ sPort . notifyOnDataAvailable ( true ) ; sPort . notifyOnBreakInterrupt ( true ) ; try { sPort . enableReceiveTimeout ( 3000 ) ; } catch ( UnsupportedCommOperationException e ) { } open = true ; }
Attempts to open a serial connection and streams using the parameters in
 the SerialParameters object.
Attempts to open a serial connection and streams using the parameters in the SerialParameters object .
// Obtain a CommPortIdentifier object for the port you want to open. //logger.debug("In openConnection(): " + parameters.getPortName()); //logger.debug("In openConnection(): inside try..catch block " //+ parameters.getPortName()); // logger.debug("In openConnection(): inside try block,portid=  " //+ portId.getPortType()); //logger.debug("In openConnection(): port type= " //+ portId.getPortType()); // 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. //sPort = (SerialPort) portId.open("SerialPortApp", 2000); // Set the parameters of the connection. If they won't set, close the // port before throwing an exception. // Open the input and output streams for the connection. If they won't // open, close the port before throwing an exception. // Add this object as an event listener for the serial port. // Set notifyOnDataAvailable to true to allow event driven input. // Set notifyOnBreakInterrup to allow event driven break handling. // Set receive timeout to allow breaking out of polling loop during // input handling. // System.out.println("In openConnection(): open flag-" + open);