2013-07-01 34 views
-1

我有這個代碼寫入和讀取COM端口。Java javax.comm syncronous讀

import java.io.IOException; 
    import java.io.InputStream; 
    import java.util.Enumeration; 
    import java.util.TooManyListenersException; 

    import javax.comm.CommPortIdentifier; 
    import javax.comm.PortInUseException; 
    import javax.comm.SerialPort; 
    import javax.comm.SerialPortEvent; 
    import javax.comm.SerialPortEventListener; 
    import javax.comm.UnsupportedCommOperationException; 

    public class MainClass implements Runnable, SerialPortEventListener { 
     static CommPortIdentifier portId; 

     static Enumeration portList; 

     InputStream inputStream; 

     SerialPort serialPort; 

     Thread readThread; 

     public static void main(String[] args) { 
     portList = CommPortIdentifier.getPortIdentifiers(); 

     while (portList.hasMoreElements()) { 
      portId = (CommPortIdentifier) portList.nextElement(); 
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 
      // if (portId.getName().equals("COM1")) { 
      if (portId.getName().equals("/dev/term/a")) { 
       MainClass reader = new MainClass(); 
      } 
      } 
     } 
     } 

     public MainClass() { 
     try { 
      serialPort = (SerialPort) portId.open("MainClassApp", 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(); 
/*HERE I NEED TO WAIT THE ANSWER*/ 
     } 

     public void run() { 
     try { 
      Thread.sleep(20000); 
     } catch (InterruptedException e) { 
     } 
     } 

     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: 
      byte[] readBuffer = new byte[20]; 

      try { 
      while (inputStream.available() > 0) { 
       int numBytes = inputStream.read(readBuffer); 
      } 
      System.out.print(new String(readBuffer)); 
      } catch (IOException e) { 
      } 
      break; 
     } 
     } 
    } 

但我需要退出該方法(看到我放了佔位符),我需要的東西就像一個syncronous讀之前等待的答案,怎麼辦呢?

+0

代碼有點打了。無需進行線程和事件。爲什麼不直接寫入串口輸出流並從輸入流中讀取?這將是同步的。 如果你真的需要異步,那麼你在世界 – DomV

+0

但我不知道什麼時候輸入流將接收數據...如何停止主線程,直到打印機的答案? – Tobia

回答

0

對我來說,解決方案是:jssc.SerialPort 而不是rxtx或java.comm。

在這裏,我發現這個方法:

public java.lang.String readString(int byteCount, int timeout) 

等待一段字節,直到超時。