2013-12-16 144 views
-1

我試圖從com端口(條形碼掃描器)讀取數據並將其寫入文本框。我對Java相對來說比較陌生,所以我相信我錯過了一些基本的東西,但是關於串行通信的文檔是有限的。我試圖從條形碼閱讀器獲得輸入,以寫入我的fxml文檔中內置的名爲「txtONE」的文本框。這裏是我的代碼:從com端口讀取數據並將其寫入到java文本框中

package javafxapplication7; 

    import javafx.application.Application; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.Parent; 
    import javafx.scene.Scene; 
    import javafx.stage.Stage; 
    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; 

    /** 
    * 

    */ 
    public class JavaFXApplication7 extends Application { 

     @Override 
     public void start(Stage stage) throws Exception { 
      Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

      Scene scene = new Scene(root); 

      stage.setScene(scene); 
      stage.show(); 
     } 

     public JavaFXApplication7() 
     { 
      super(); 
     } 

     void connect (String portName) throws Exception 
     { 
      CommPortIdentifier portIdentifier =   CommPortIdentifier.getPortIdentifier(portName); 
      if (portIdentifier.isCurrentlyOwned()) 
      { 
       System.out.println("Error: Port is currently in use"); 
      } 
    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); 

      InputStream in = serialPort.getInputStream(); 
      OutputStream out = serialPort.getOutputStream(); 

      (new Thread(new SerialWriter(out))).start(); 

      serialPort.addEventListener(new SerialReader(in)); 
      serialPort.notifyOnDataAvailable(true); 

     } 
     else 
     { 
      System.out.println("Error: Only serial ports are handled by this program."); 
     } 
    }  
} 

/** 
* Handles the input coming from the serial port. A new line character 
* is treated as the end of a block in this example. 
*/ 
public static class SerialReader implements SerialPortEventListener 
{ 
    private InputStream in; 
    private byte[] buffer = new byte[1024]; 

    public SerialReader (InputStream in) 
    { 
     this.in = in; 
    } 

    @Override 
    public void serialEvent(SerialPortEvent arg0) { 
     int data; 

     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)); 
      txtOne.text = new String(buffer,0,len); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
      System.exit(-1); 
     }    
    } 

} 
+0

你期望的輸出是什麼,你會得到什麼輸出? – iWumbo

+0

我想讓數據寫入我的jframe名稱「txtOne」中的文本框,我所能管理的就是讓它寫入系統。它不識別txtONE。 – HM88

回答

0

在Java讀取COM端口的簡單方式,使用JNI(Java本地接口) 其他方式讀取條碼使用存儲的數據,當數據讀取和保存(數據庫/文件)用java讀取。但我在JNI中使用java的串口,你讀取存儲的數據必須是第二種方式。

相關問題