Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Do you have a simple serial port code for netbeans?

807580Jan 11 2010 — edited Jan 29 2010
I tried to read data from serial port with GUI in Netbeans. but I can show data in command only.I can't show in text field. I have to use serial port in my project. This is my code for read data from serial port. It show in command only. How to read data from serial and show in text field.I used to append replace setText but It error. Maybe It can't used append in window application with Netbeans..
public void serialEvent(SerialPortEvent event) { 
switch (event.getEventType()) { 
case SerialPortEvent.BI: 
case SerialPortEvent.OE: 
case SerialPortEvent.FE: 
case SerialPortEvent.PE: 
case SerialPortEvent.CD: 
case SerialPortEvent.CTS: 
case SerialPortEvent.DSR: 
case SerialPortEvent.RI: 
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: 
break; 
case SerialPortEvent.DATA_AVAILABLE: 
// we get here if data has been received 
byte[] readBuffer = new byte[20]; 
try { 
// read data 
while (inputStream.available() > 0) { 
int numBytes = inputStream.read(readBuffer); 
} 
// print data 
String result = new String(readBuffer); 
System.out.print(result); // Here can show data from serial port.
m_shTxt.setText(result); // Here can't show anything.When I use append It error.I cannot show result in text field. 
} catch (Exception e) { 
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 26 2010
Added on Jan 11 2010
11 comments
284 views