Java RS232 communication using Swing and RXTX -
i'm trying communicate between pc (windows 7 using netbeans , rxtx) using serial port. have prolem serial event(public void serialevent(serialportevent arg0)). event should recive data , write them in label (jlabel1.settext("sfazds")). unfortunately event don't change label text , don't why. below code:
import gnu.io.commport; import gnu.io.commportidentifier; import gnu.io.serialport; import gnu.io.serialportevent; import gnu.io.serialporteventlistener; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; public class rs extends javax.swing.jframe implements serialporteventlistener{ static outputstream out; static inputstream in; public rs() { initcomponents(); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jbutton1 = new javax.swing.jbutton(); jlabel1 = new javax.swing.jlabel(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); addwindowlistener(new java.awt.event.windowadapter() { public void windowopened(java.awt.event.windowevent evt) { formwindowopened(evt); } }); jbutton1.settext("jbutton1"); jbutton1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton1actionperformed(evt); } }); jlabel1.settext("jlabel1"); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(54, 54, 54) .addcomponent(jbutton1)) .addgroup(layout.createsequentialgroup() .addgap(203, 203, 203) .addcomponent(jlabel1))) .addcontainergap(217, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(21, 21, 21) .addcomponent(jbutton1) .addgap(34, 34, 34) .addcomponent(jlabel1) .addcontainergap(222, short.max_value)) ); pack(); }// </editor-fold> private void jbutton1actionperformed(java.awt.event.actionevent evt) { byte[] b = new byte[2]; b[0] = 'a'; b[1] = 'b'; try{ out.write(b); } catch(exception e){ e.printstacktrace(); } } private void formwindowopened(java.awt.event.windowevent evt) { try{ (new rs()).connect("com3"); } catch(exception e){ e.printstacktrace(); } } void connect(string portname) throws exception{ commportidentifier portidentifier = commportidentifier.getportidentifier(portname); if (portidentifier.iscurrentlyowned()){ joptionpane.showmessagedialog(null, "błąd: port jest obecnie w użyciu"); } else{ commport commport = portidentifier.open(this.getclass().getname(), 2000); if (commport instanceof serialport){ serialport serialport = (serialport) commport; serialport.setserialportparams(57600,serialport.databits_8,serialport.stopbits_1,serialport.parity_none); in = serialport.getinputstream(); out = serialport.getoutputstream(); (new thread(new serialwriter(out))).start(); serialport.addeventlistener(this); serialport.notifyondataavailable(true); } else{ joptionpane.showmessagedialog(null, "tylko port szeregowy może być podłączony!"); } } } @override public void serialevent(serialportevent arg0) { int data; byte[] buffer = new byte[1024]; try{ int len = 0; while ( ( data = in.read()) > -1 ){ if ( data == '\n' ) { break; } buffer[len++] = (byte) data; } system.out.print(new string(buffer,0,len)); jlabel1.settext("sfazds"); joptionpane.showmessagedialog(null, new string(buffer,0,len)); } catch ( ioexception e ){ system.exit(-1); } } public static class serialwriter implements runnable{ outputstream out; public serialwriter(outputstream out){ this.out = out; } @override public void run(){ try{ int c = 0; while ((c = system.in.read()) > -1){ this.out.write(c); } } catch(ioexception e){ system.exit(-1); } } } public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (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(rs.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(rs.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(rs.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(rs.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new rs().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jbutton jbutton1; private javax.swing.jlabel jlabel1; // end of variables declaration }
your output not synchronized eventdispatchertread. try following:
swingutilities.invokelater(new runnable() { @override public void run() { jlabel1.settext("sfazds"); } });
Comments
Post a Comment