Read serial port with Netbeans GUI
807580Dec 19 2009 — edited Dec 21 2009I tried to create program for read data from serial in netbeans. and now I can read serial port and show in command.I want to show in Text Field.I have two code can read data from serial port but can show in command only.please help me to show data in Text field please.You can download my code at http://www.mediafire.com/?mmnnz3vyxnz or see my code here.
import java.io.*;
import java.util.*;
import gnu.io.*;
public class ReadPort extends javax.swing.JFrame implements Runnable, SerialPortEventListener{
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
/** Creates new form ReadPort */
public ReadPort() {
initComponents();
initSerail();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
m_shTxt = new javax.swing.JTextField();
m_exitButtn = new javax.swing.JButton();
m_okButtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
m_exitButtn.setText("Exit");
m_exitButtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
m_exitButtnActionPerformed(evt);
}
});
m_okButtn.setText("OK");
m_okButtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
m_okButtnActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(m_shTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(m_okButtn, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(m_exitButtn, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
.addGap(50, 50, 50))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(35, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(m_shTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(m_okButtn))
.addGap(18, 18, 18)
.addComponent(m_exitButtn)
.addGap(201, 201, 201))
);
pack();
}// </editor-fold>
private void m_exitButtnActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void m_okButtnActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void runPort(){
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM4") ){ // Change port
//if (portId.getName().equals("/dev/term/a") {
ReadPort reader = new ReadPort();
}}}}
public static void main(String args[]) {
runPort();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ReadPort().setVisible(true);
}
}); }
// Variables declaration - do not modify
private javax.swing.JButton m_exitButtn;
private javax.swing.JButton m_okButtn;
private javax.swing.JTextField m_shTxt;
// End of variables declaration
public void run() {
try { Thread.sleep(20000);}
catch (InterruptedException e) {}
}
public void initSerail() {
// initalize serial port
try {serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); }
catch (PortInUseException e) {}
try {inputStream = serialPort.getInputStream(); }
catch (IOException e) {}
try {serialPort.addEventListener(this); }
catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); }
catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void ReadData(){
// runs this if data has been received
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.inputStream.read(buffer)) > -1 ){
System.out.print(new String(buffer,0,len));
String s;
s =new String(buffer,0,len);
}}
catch ( IOException e )
{ e.printStackTrace();}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
ReadData();
break;
}}}