2

Hello Below is the code that read com port data five times and show in GUI. It is working like

  1. Reading the data five times available in the com port and finally gui is launched and display all the five data at a time. My goal is to to first GUi is launched then
  2. Read 1st data and display in the GUI
  3. Appending 2nd data and display in the GUI .. Appending 5th data and display in the GUI

GUi is created using Nebeans

   /*
     * 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 learn;

import jssc.SerialPort;
import jssc.SerialPortException;

/**
 *
 * @author Mica
 */
public class gui extends javax.swing.JFrame {

    /**
     * Creates new form gui
     */
        public gui() {
        initComponents();
         comport();

    }

    private void comport(){

    SerialPort serialPort = new SerialPort("COM3");

        int count = 1;


         while (count < 11) {
         try {

            serialPort.openPort();//Open serial port
            serialPort.setParams(9600, 8, 1, 0);//Set params.
            byte[] buffer = serialPort.readBytes(32);//Read 10 bytes from serial port
            final String readed = new String(buffer);
            System.out.println("««Readed from COM"  + ": " + readed);

            jTextArea1.append(readed+ "\n" );
            serialPort.closePort();//Close serial port

        }
        catch (SerialPortException ex) {
           System.out.println(ex);
        }
        count++;
        }

    }

    /**
     * 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">                          
    private void initComponents() {
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Create and display the form */

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {


                new gui().setVisible(true);

            }

        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mica
  • 45
  • 7

1 Answers1

2

Extend SwingWorker. You can read the serial port in your implementation of doInBackground() , publish() interim results, and append() the results to a JTextArea in your implementation of process(), as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Yap I can run the example that you have given me. Here when start button is pressed then data is displayed. Without using button can I get data on textarea when data is available on the com port? Any suggestion – Mica Dec 22 '14 at 02:22
  • The button is not required; you can loop and/or sleep in `doInBackground()` as needed. – trashgod Dec 22 '14 at 03:22