Hello Below is the code that read com port data five times and show in GUI. It is working like
- 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
- Read 1st data and display in the GUI
- 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
}