2017-03-15 14 views
0

これは問題です。シリアルポート経由でデータを送信するArduino uno r3があります。データをバイト単位で受け取るためのjava guiコードがあります。配列を変換し、String stに格納してjTextAreaに表示します。 問題は、jtextAreaは文字列のst値がnullであると考えていることを示すものではありませんが、有名なSystem.out.print(st)を使用した場合、結果はコンソールに正しく表示されます。 何が間違っているのか分かりません。下のシリアルポートからデータを取得する責任を負うコードを投稿すると、同期されたserialEventメソッドが呼び出され、助けてください。 jTextArea1はprivateとして宣言されています。同じクラスと文字列STでのNetBeans IDEから貼り付けコピー、jxtextAreaの文字列テキストを表示するrxtxライブラリとSerialPortEventを介してJavaでデータを受信

public synchronized void serialEvent(SerialPortEvent oEvent) { 
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {   
     try {     
       int available = input.available(); 
      byte[] chunk = new byte[available]; 
      input.read(chunk, 0, available); 
      st = new String(chunk); 
      jTextArea1.append(st);         
     }catch (IOException e) {System.out.println("IO Error Occurred: " + e.toString());} 
+0

「JTextArea」がどのように定義され使用されているかわからないので、あなたに答えを与えることは不可能です。多分あなたはあなたの変数をシャドーイングしていますか?画面に追加したり、画面に表示されるコンポーネントに追加したりしていない可能性があります。イベントディスパッチスレッドをブロックしている可能性がありますか?多分、ユニコーンが森林を巡ってクロールしていますか? – MadProgrammer

+0

iam netbeansを使用して、jTextAreaをドラッグアンドドロップでフレームに追加しました。自動的に同じクラスのプライベートとして定義され、JFrameは初期化され、設定可能です(true)。JFrameおよびjTextAreaが。 –

+1

あなたの問題を示す[実行可能な例](http://stackoverflow.com/help/mcve)を提供することを検討してください – MadProgrammer

答えて

0

オクラホマ! JFrameの内部に最初にjPanelを作成し、JFrame内のコンポーネントを直接追加するのではなく、その中にすべてのコンポーネントを追加する必要がありました。今それは魅​​力のように動作します!あなたの懸念に非常に感謝します。

0

これは完全なJavaコードで おかげで非常に多くの同じクラスにpublicとして宣言され、このコードが実行され、コンソールに出力しないと問題は、JButtonを作成し、JButtonの下のjTextAreaに文字列を関連付けることでも動作しますが、問題はコード自体の中のjTextAreaに文字列を自動的に追加することです。

import java.io.*; 
import java.io.InputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import javax.swing.JTextArea; 
import javax.swing.JOptionPane; 
import java.io.OutputStream; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.nio.charset.Charset; 
import java.util.Enumeration; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
public class reading extends javax.swing.JFrame implements SerialPortEventListener{ 
    SerialPort serialPort; 
    public static String st; 
    public char[] c; 
    /** 
    * Creates new form reading 
    */ 
    private static final String PORT_NAMES[] = { 
      "/dev/tty.usbmodem411", // Mac OS X 
      "/dev/ttyUSB0", // Linux 
      "COM3", // Windows 
      }; 
    private InputStream input; 
    private OutputStream output; 
    private static final int TIME_OUT = 2000;  
    private static final int DATA_RATE = 9600; 
    public reading() { 
     initComponents(); 
     initialize(); 
     close();  
    } 
    public void initialize() { 
     CommPortIdentifier portId = null; 
     Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); 
     // iterate through, looking for the port 
     while (portEnum.hasMoreElements()) { 
      CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); 
      for (String portName : PORT_NAMES) { 
       if (currPortId.getName().equals(portName)) { 
        portId = currPortId; 
        break; 
       } 
      } 
     } 
     if (portId == null) { 
           jTextArea1.setText("scanner is not connected ! "); 
     } 
     try { 
      serialPort = (SerialPort) portId.open(this.getClass().getName(), 
        TIME_OUT); 
      // set port parameters 
      serialPort.setSerialPortParams(DATA_RATE, 
        SerialPort.DATABITS_8, 
        SerialPort.STOPBITS_1, 
        SerialPort.PARITY_NONE); 
      input = serialPort.getInputStream(); 
      output = serialPort.getOutputStream(); 
      // add event listeners 
      serialPort.addEventListener(this); 
      serialPort.notifyOnDataAvailable(true); 
     } catch (Exception e) {    
     } 
    }  
    public synchronized void close() { 
     if (serialPort != null) { 
      serialPort.removeEventListener(); 
      serialPort.close(); 
     } 
     }  
    public synchronized void serialEvent(SerialPortEvent oEvent) {  
     if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {   
      try {     
        int available = input.available(); 
       byte[] chunk = new byte[available]; 
       input.read(chunk, 0, available); 
       st = new String(chunk); 
       jTextArea1.append(st); 
       System.out.print(st); 
       try{ 
       Thread.sleep(5000);     
} catch(InterruptedException ex) { 
    Thread.currentThread().interrupt(); 
}                                               
       // Displayed results are codepage dependent                                                          
      } 
catch (IOException e) { 
System.out.println("IO Error Occurred: " + e.toString()); 

}   
     } 
     // Ignore all the other eventTypes, but you should consider the other ones. 
    }   
    /** 
    * 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. 
    */  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    /** 
    * 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() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(211, Short.MAX_VALUE) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(23, 23, 23)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(25, 25, 25) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(179, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (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(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 
reading main = new reading(); 
     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new reading().setVisible(true); 
       main.initialize(); 
      } 
     }); 
    }  
    // Variables declaration - do not modify      
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    // End of variables declaration     

    } 
関連する問題