Package net.helipilot50.arduinoserial

Source Code of net.helipilot50.arduinoserial.TempPressure

package net.helipilot50.arduinoserial;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

import org.jdom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.awt.Insets;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;

public class TempPressure {
  SerialPort serialPort;
  /** The port we're normally going to use. */
  private static final String PORT_NAMES[] = {
    "/dev/tty.usbserial-A9007UX1", // Mac OS X
    "/dev/ttyUSB0", // Linux
    "COM5", // Windows
  };
  /** Buffered input stream from the port */
  private InputStream input;
  /** The output stream to the port */
  private OutputStream output;
  /** Milliseconds to block while waiting for port open */
  private static final int TIME_OUT = 2000;
  /** Default bits per second for COM port. */
  private static final int DATA_RATE = 9600;
 
  XMLInputFactory inputFactory = null;   
    XMLStreamReader xmlReader = null;

    boolean readUpdates = true;
  private JFrame frmTemperatureAndPressure;
  private JTextField temperatureTextField;
  private JTextField pressureTextField;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    final TempPressure window = new TempPressure();
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          window.initialize();
          window.frmTemperatureAndPressure.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
         
    try {
      window.processXML();
    } catch (XMLStreamException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
         
       
  }

  /**
   * This should be called when you stop using the port.
   * This will prevent port locking on platforms like Linux.
   */
  public synchronized void close() {
    if (serialPort != null) {
      serialPort.removeEventListener();
      serialPort.close();
    }
  }

  @Override
  protected void finalize() throws Throwable {
    close();
    if (xmlReader != null)
       xmlReader.close();
    super.finalize();
  }


  public SerialPort getSerialPort() {
    return serialPort;
  }

  public InputStream getInput() {
    return input;
  }

  public OutputStream getOutput() {
    return output;
  }

  /**
   * Create the application.
   */
  public TempPressure() {
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    // iterate through, looking for the port
    while (portEnum.hasMoreElements()) {
      CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
      for (String portName : PORT_NAMES) {
        if (currPortId.getName().equals(portName)) {
          portId = currPortId;
          break;
        }
      }
    }

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

    try {
      // 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) {
      System.err.println(e.toString());
    }
   
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frmTemperatureAndPressure = new JFrame();
    frmTemperatureAndPressure.setIconImage(Toolkit.getDefaultToolkit().getImage(TempPressure.class.getResource("/net/helipilot50/arduinoserial/arduino_thumb.png")));
    frmTemperatureAndPressure.setResizable(false);
    frmTemperatureAndPressure.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        readUpdates = false;
      }
    });
    frmTemperatureAndPressure.setTitle("Temperature and Pressure");
    frmTemperatureAndPressure.setBounds(100, 100, 300, 110);
    frmTemperatureAndPressure.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0};//, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 1.0};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
    frmTemperatureAndPressure.getContentPane().setLayout(gridBagLayout);
   
    JLabel lblTemperature = new JLabel("Temperature C");
    lblTemperature.setFont(new Font("Tahoma", Font.BOLD, 24));
    GridBagConstraints gbc_lblTemperature = new GridBagConstraints();
    gbc_lblTemperature.insets = new Insets(0, 0, 5, 5);
    gbc_lblTemperature.anchor = GridBagConstraints.EAST;
    gbc_lblTemperature.gridx = 0;
    gbc_lblTemperature.gridy = 0;
    frmTemperatureAndPressure.getContentPane().add(lblTemperature, gbc_lblTemperature);
   
    temperatureTextField = new JTextField();
    temperatureTextField.setEditable(false);
    temperatureTextField.setFont(new Font("Tahoma", Font.BOLD, 24));
    lblTemperature.setLabelFor(temperatureTextField);
    GridBagConstraints gbc_temperatureTextField = new GridBagConstraints();
    gbc_temperatureTextField.weightx = 1.0;
    gbc_temperatureTextField.fill = GridBagConstraints.HORIZONTAL;
    gbc_temperatureTextField.insets = new Insets(0, 0, 5, 5);
    gbc_temperatureTextField.gridx = 1;
    gbc_temperatureTextField.gridy = 0;
    frmTemperatureAndPressure.getContentPane().add(temperatureTextField, gbc_temperatureTextField);
    temperatureTextField.setColumns(10);
   
    JLabel lblNewLabel = new JLabel("Pressure hPa");
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
    gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
    gbc_lblNewLabel.gridx = 0;
    gbc_lblNewLabel.gridy = 1;
    frmTemperatureAndPressure.getContentPane().add(lblNewLabel, gbc_lblNewLabel);
   
    pressureTextField = new JTextField();
    pressureTextField.setEditable(false);
    pressureTextField.setFont(new Font("Tahoma", Font.BOLD, 24));
    lblNewLabel.setLabelFor(pressureTextField);
    GridBagConstraints gbc_pressureTextField = new GridBagConstraints();
    gbc_pressureTextField.weightx = 1.0;
    gbc_pressureTextField.insets = new Insets(0, 0, 0, 5);
    gbc_pressureTextField.fill = GridBagConstraints.HORIZONTAL;
    gbc_pressureTextField.gridx = 1;
    gbc_pressureTextField.gridy = 1;
    frmTemperatureAndPressure.getContentPane().add(pressureTextField, gbc_pressureTextField);
    pressureTextField.setColumns(10);
  }

  public void updateValues(final float temperature, final int pressure){
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          pressureTextField.setText(Integer.toString(pressure));
          temperatureTextField.setText(Float.toString(temperature));
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
 
  public void processXML() throws XMLStreamException{
   
      BMPHandler handler = new BMPHandler();
     
      SAXParserFactory spf = SAXParserFactory.newInstance();
     
      InputSource is = new InputSource(this.getInput());
     
      try {

        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();

        //parse the file and also register this class for call backs
        while (readUpdates){
          try {
            sp.parse(is, handler);
           
          } catch(SAXException se) {
            // dont care, there may be no chars in the stream or an incomplete document
          }
          updateValues(handler.getTemperature(), handler.getPressure());
        }
      }catch(SAXException se) {
        se.printStackTrace();
      }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
  }
}
TOP

Related Classes of net.helipilot50.arduinoserial.TempPressure

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.