serial.ChartFromSerial.java Source code

Java tutorial

Introduction

Here is the source code for serial.ChartFromSerial.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package serial;

import com.fazecast.jSerialComm.SerialPort;
import java.awt.BorderLayout;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author John
 */
public class ChartFromSerial extends javax.swing.JFrame {

    //Graph Labels
    private final String graphName = "Dataset";
    private final String xAxisLabel = "Number of Samples";
    private final String yAxisLabel = "Magnitude";

    //Serial variables
    private int[] baudRates = { 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, 115200,
            230400, 250000 };
    private SerialPort chosenPort;
    private SerialPort[] portNames;

    //Graph variables
    private XYSeries defaultSeries;
    private XYSeriesCollection defaultDataset;
    private JFreeChart defaultChart;
    private ChartPanel graph;
    private int samples = 0;

    //textLog variables
    private JTextArea text;
    private JScrollPane textScrollPane;

    //thread variables
    private Thread serialThread;
    private boolean autoScrollEnabled;

    /**
     * Creates new form JavaArduinoInterfacingAttempt
     */
    public ChartFromSerial() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println(e);
        }
        initComponents();

        //set autoscroll
        autoScroll_chkBx.setSelected(true);
        autoScrollEnabled = true;

        //create the graph
        defaultSeries = new XYSeries(graphName);
        defaultDataset = new XYSeriesCollection(defaultSeries);
        defaultChart = ChartFactory.createXYLineChart(graphName, xAxisLabel, yAxisLabel, defaultDataset);
        graph = new ChartPanel(defaultChart);
        chartPanel.add(graph, BorderLayout.CENTER);
        graph.setVisible(true);

        //create the text log
        text = new JTextArea();
        text.setEditable(false);
        textScrollPane = new JScrollPane(text);
        textScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        textPanel.add(textScrollPane, BorderLayout.CENTER);
        textScrollPane.setVisible(true);

        //Populate the combo box
        portNames = SerialPort.getCommPorts();
        while (portNames.length == 0) {
            if (JOptionPane.showConfirmDialog(rootPane, "No connected devices found.\nWould you like to refresh?",
                    "Could not connect to any devices.", JOptionPane.YES_NO_OPTION,
                    JOptionPane.ERROR_MESSAGE) == JOptionPane.NO_OPTION) {
                buttonsOff();
                return;
            } else {
                portNames = SerialPort.getCommPorts();
            }
        }
        portList_jCombo.removeAllItems();
        for (SerialPort portName : portNames) {
            portList_jCombo.addItem(portName.getSystemPortName());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        refreshButton = new javax.swing.JButton();
        portList_jCombo = new javax.swing.JComboBox();
        baudRate_jCombo = new javax.swing.JComboBox();
        connect_jBtn = new javax.swing.JButton();
        pause_jBtn = new javax.swing.JButton();
        autoScroll_chkBx = new javax.swing.JCheckBox();
        toTopText = new javax.swing.JButton();
        toBotText = new javax.swing.JButton();
        textPanel = new javax.swing.JPanel();
        chartPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Java/Arduino Interfacing Attempt");
        setName("window"); // NOI18N
        setResizable(false);

        jPanel1.setMaximumSize(new java.awt.Dimension(698, 445));
        jPanel1.setMinimumSize(new java.awt.Dimension(698, 445));
        jPanel1.setPreferredSize(new java.awt.Dimension(698, 445));

        refreshButton.setText("Refresh Port List");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        portList_jCombo.setToolTipText("");
        portList_jCombo.setName(""); // NOI18N
        portList_jCombo.setPreferredSize(new java.awt.Dimension(74, 25));

        baudRate_jCombo.setModel(
                new javax.swing.DefaultComboBoxModel(new String[] { "300", "600", "1200", "2400", "4800", "9600",
                        "14400", "19200", "28800", "31250", "38400", "57600", "115200", "230400", "250000" }));
        baudRate_jCombo.setSelectedIndex(5);
        baudRate_jCombo.setToolTipText("");
        baudRate_jCombo.setName(""); // NOI18N
        baudRate_jCombo.setPreferredSize(new java.awt.Dimension(74, 25));

        connect_jBtn.setText("Connect");
        connect_jBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connect_jBtnActionPerformed(evt);
            }
        });

        pause_jBtn.setText("Pause");
        pause_jBtn.setEnabled(false);
        pause_jBtn.setPreferredSize(new java.awt.Dimension(74, 25));
        pause_jBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pause_jBtnActionPerformed(evt);
            }
        });

        autoScroll_chkBx.setText("AutoScroll");
        autoScroll_chkBx.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        autoScroll_chkBx.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autoScroll_chkBxActionPerformed(evt);
            }
        });

        toTopText.setText("Top");
        toTopText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toTopTextActionPerformed(evt);
            }
        });

        toBotText.setText("Bottom");
        toBotText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toBotTextActionPerformed(evt);
            }
        });

        textPanel.setAutoscrolls(true);
        textPanel.setLayout(new java.awt.BorderLayout());

        chartPanel.setPreferredSize(new java.awt.Dimension(600, 254));
        chartPanel.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(textPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 201,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createSequentialGroup().addComponent(autoScroll_chkBx)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(toTopText)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(toBotText)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(portList_jCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(baudRate_jCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(connect_jBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(pause_jBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(chartPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 463,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap()));

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
                new java.awt.Component[] { baudRate_jCombo, connect_jBtn, pause_jBtn, portList_jCombo });

        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(connect_jBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(portList_jCombo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(pause_jBtn, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(baudRate_jCombo, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(refreshButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(chartPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGap(2, 2, 2))
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(autoScroll_chkBx).addComponent(toTopText)
                                                .addComponent(toBotText))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 370,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))));

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL,
                new java.awt.Component[] { baudRate_jCombo, connect_jBtn, pause_jBtn, portList_jCombo });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGap(0, 0, 0)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Listen to data from parallel thred
    private void connect_jBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connect_jBtnActionPerformed
        defaultSeries.clear();
        text.setText("");
        if ("Connect".equals(connect_jBtn.getText())) {

            //Attempt to connect to to serial port
            chosenPort = SerialPort.getCommPort(portList_jCombo.getSelectedItem().toString());
            chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
            chosenPort.setBaudRate(Integer.parseInt(baudRate_jCombo.getSelectedItem().toString()));

            if (chosenPort.openPort()) {
                createSerialThread(chosenPort);
                buttonsConnected();
                //                if(createSerialThread(chosenPort)){
                //                    buttonsConnected();
                //                } else {
                //                    JOptionPane.showMessageDialog(rootPane, "Failed to create a serial connection.", "Serial connection failed.", JOptionPane.ERROR_MESSAGE);
                //                    chosenPort.closePort();
                //                }
            } else {
                if (JOptionPane.showConfirmDialog(rootPane,
                        "Error at " + portList_jCombo.getSelectedItem().toString() + "\nWould you like to refresh?",
                        "Trouble connecting to " + portList_jCombo.getSelectedItem().toString(),
                        JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE) == JOptionPane.YES_OPTION) {
                    hardRefresh();
                } else {
                    buttonsOff();
                }
            }
        } else {
            //Attempt to disconnect from the serial port
            chosenPort.closePort();
            softRefresh();
            samples = 0;

        }
    }//GEN-LAST:event_connect_jBtnActionPerformed

    private void pause_jBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pause_jBtnActionPerformed
        if ("Play".equals(pause_jBtn.getText())) {
            while (!chosenPort.openPort()) {
                if (JOptionPane.showConfirmDialog(rootPane,
                        "Error at " + portList_jCombo.getSelectedItem().toString() + "\nWould you like to refresh?",
                        "Trouble connecting to " + portList_jCombo.getSelectedItem().toString(),
                        JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE) == JOptionPane.NO_OPTION) {
                    buttonsOff();
                    return;
                } else {
                    portNames = SerialPort.getCommPorts();
                    if (portNames.length > 0) {
                        if (portNames.length >= 1) {
                            Object[] possibilities = new Object[portNames.length];
                            for (int i = 0; i < portNames.length; i++) {
                                possibilities[i] = portNames[i].getSystemPortName();
                            }
                            String s = (String) JOptionPane.showInputDialog(rootPane,
                                    "Other ports are available.\nSelect one to continue on that port or cancel to disconnect.",
                                    "Another port is available", JOptionPane.PLAIN_MESSAGE, null, possibilities,
                                    null);
                            chosenPort.closePort();
                            if (s != null) {
                                chosenPort = SerialPort.getCommPort(s);
                                chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                                chosenPort.setBaudRate(
                                        Integer.parseInt(baudRate_jCombo.getSelectedItem().toString()));
                                softRefresh();
                                portList_jCombo.setSelectedItem(s);
                                if (chosenPort.openPort()) {
                                    createSerialThread(chosenPort);
                                    pause_jBtn.setText("Pause");
                                    connect_jBtn.setEnabled(true);
                                    return;
                                }
                            } else {
                                buttonsOff();
                                return;
                            }
                        } else {
                            if (JOptionPane.showConfirmDialog(rootPane,
                                    portNames[0].getSystemPortName()
                                            + " is available. Would you like to use it instead?",
                                    "Another port is available",
                                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                                chosenPort.closePort();
                                chosenPort = SerialPort.getCommPort(portNames[0].getSystemPortName());
                                chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                                chosenPort.setBaudRate(
                                        Integer.parseInt(baudRate_jCombo.getSelectedItem().toString()));
                                softRefresh();
                                portList_jCombo.setSelectedItem(portNames[0].getSystemPortName());
                                if (chosenPort.openPort()) {
                                    createSerialThread(chosenPort);
                                    pause_jBtn.setText("Pause");
                                    connect_jBtn.setEnabled(true);
                                    return;
                                }
                            } else {
                                buttonsOff();
                                return;
                            }
                        }
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "You are currently not connected to any devices.",
                                "No devices found", JOptionPane.INFORMATION_MESSAGE);
                        softRefresh();
                        buttonsOff();
                        return;
                    }
                }

            }
            createSerialThread(chosenPort);
            pause_jBtn.setText("Pause");
        } else {
            chosenPort.closePort();
            pause_jBtn.setText("Play");
        }
    }//GEN-LAST:event_pause_jBtnActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        // TODO add your handling code here:
        hardRefresh();
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void autoScroll_chkBxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autoScroll_chkBxActionPerformed
        // TODO add your handling code here:
        autoScrollEnabled = !autoScrollEnabled;
    }//GEN-LAST:event_autoScroll_chkBxActionPerformed

    private void toTopTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toTopTextActionPerformed
        // TODO add your handling code here:
        text.setCaretPosition(0);
    }//GEN-LAST:event_toTopTextActionPerformed

    private void toBotTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toBotTextActionPerformed
        // TODO add your handling code here:
        text.setCaretPosition(text.getDocument().getLength());
    }//GEN-LAST:event_toBotTextActionPerformed

    private boolean attemptConnection(String desiredPort, int baudRate) {
        //Port Setup
        chosenPort = SerialPort.getCommPort(desiredPort);
        chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
        chosenPort.setBaudRate(baudRate);
        //Making sure all goes well
        chosenPort.openPort();
        if (chosenPort.openPort()) {
            createSerialThread(chosenPort);
            buttonsConnected();
            //            buttonsConnected();
            //            if (createSerialThread(chosenPort)){
            //                buttonsConnected();
            //            } else {
            //                JOptionPane.showMessageDialog(rootPane, "Failed to create a serial connection.", "Serial connection failed.", JOptionPane.ERROR_MESSAGE);
            //                chosenPort.closePort();
            //            }
        } else {
            //Prompt user for response
            if (JOptionPane.showConfirmDialog(rootPane,
                    "Error at " + portList_jCombo.getSelectedItem().toString() + "\nWould you like to refresh?",
                    "Trouble connecting to " + portList_jCombo.getSelectedItem().toString(),
                    JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE) == JOptionPane.YES_OPTION) {
                hardRefresh();
            } else {
                buttonsOff();
            }
        }
        return false;
    }

    private void defaultButtonStates() {
        refreshButton.setEnabled(true);
        portList_jCombo.setEnabled(true);
        baudRate_jCombo.setEnabled(true);
        pause_jBtn.setText("Pause");
        pause_jBtn.setEnabled(false);
        connect_jBtn.setText("Connect");
        connect_jBtn.setEnabled(true);
    }

    private void buttonsOff() {
        refreshButton.setEnabled(true);
        portList_jCombo.setEnabled(false);
        baudRate_jCombo.setEnabled(false);
        pause_jBtn.setText("Pause");
        pause_jBtn.setEnabled(false);
        connect_jBtn.setText("Connect");
        connect_jBtn.setEnabled(false);
    }

    private void buttonsConnected() {
        refreshButton.setEnabled(false);
        portList_jCombo.setEnabled(false);
        baudRate_jCombo.setEnabled(false);
        pause_jBtn.setText("Pause");
        pause_jBtn.setEnabled(true);
        connect_jBtn.setText("Disconnect");
        connect_jBtn.setEnabled(true);
    }

    public void softRefresh() {
        buttonsOff();
        portList_jCombo.removeAllItems();
        portNames = SerialPort.getCommPorts();
        if (portNames.length == 0) {
            portList_jCombo.addItem("Refresh");
        } else {
            for (SerialPort portName : portNames) {
                portList_jCombo.addItem(portName.getSystemPortName());
            }
        }
        chosenPort.closePort();
    }

    public void hardRefresh() {
        portNames = SerialPort.getCommPorts();
        while (portNames.length == 0) {
            buttonsOff();
            if (JOptionPane.showConfirmDialog(rootPane, "No connected devices found.\nWould you like to refresh?",
                    "Could not connect to any devices.", JOptionPane.YES_NO_OPTION,
                    JOptionPane.ERROR_MESSAGE) == JOptionPane.NO_OPTION) {
                return;
            } else {
                portNames = SerialPort.getCommPorts();
            }
        }
        portList_jCombo.removeAllItems();
        for (SerialPort portName : portNames) {
            portList_jCombo.addItem(portName.getSystemPortName());
        }
        defaultButtonStates();
        if (chosenPort != null) {
            chosenPort.closePort();
        }
    }

    public void createSerialThread(SerialPort desiredPort) {
        //Create Serial Scanning Thread
        serialThread = new Thread() {
            @Override
            public void run() {
                try (Scanner serialScanner = new Scanner(desiredPort.getInputStream())) {
                    int numIn;
                    while (serialScanner.hasNextLine()) {
                        String textIn = serialScanner.nextLine();
                        try {
                            numIn = Integer.parseInt(textIn);
                            defaultSeries.add(samples, numIn);
                            text.append(samples++ + ": " + numIn + "\n");
                            if (autoScrollEnabled) {
                                text.setCaretPosition(text.getDocument().getLength());
                            }
                        } catch (NumberFormatException e) {
                            System.out.println("Unexpected value at " + desiredPort.getSystemPortName() + ": \""
                                    + textIn + "\"");
                        }
                    }
                } catch (Exception e) {
                    System.out.println(e);
                    samples = 0;
                    if (JOptionPane.showConfirmDialog(rootPane,
                            "The serial connection has fialed.\n Would you like to refresh?",
                            "Serial connection failed.", JOptionPane.YES_NO_OPTION,
                            JOptionPane.ERROR_MESSAGE) == JOptionPane.YES_OPTION) {
                        hardRefresh();
                        defaultSeries.clear();
                        text.setText("");
                    } else {
                        buttonsOff();
                    }
                }
            }
        };
        serialThread.start();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChartFromSerial.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChartFromSerial.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChartFromSerial.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChartFromSerial.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new ChartFromSerial().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox autoScroll_chkBx;
    private javax.swing.JComboBox baudRate_jCombo;
    private javax.swing.JPanel chartPanel;
    private javax.swing.JButton connect_jBtn;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton pause_jBtn;
    private javax.swing.JComboBox portList_jCombo;
    private javax.swing.JButton refreshButton;
    private javax.swing.JPanel textPanel;
    private javax.swing.JButton toBotText;
    private javax.swing.JButton toTopText;
    // End of variables declaration//GEN-END:variables
}