2012-03-30 67 views
3

我試圖從使用rxtx API的COM端口讀取。 COM端口連接到一個微控制器,這樣每次我按下微控制器板上的一個按鈕時,它都會返回一系列從0x01到0xff的字節數。我想在我的java控制檯上顯示這些數字,但它似乎可以讀取到0x40。所有其他的字節數字似乎都丟失了。我確信微控制器運行良好,因爲我在另一個終端程序中進行了測試,然後發出了正確的結果。所以我懷疑我的輸入流有什麼問題。有沒有人可以幫助我找到問題?以下是我的java代碼,這是我從google找到的一個典型的串行通信端口讀取示例代碼。從commport讀取不返回完整的結果在java

import gnu.io.CommPortIdentifier; 
import gnu.io.PortInUseException; 
import gnu.io.SerialPort; 
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import gnu.io.UnsupportedCommOperationException; 
import java.io.*; 
import java.util.*; 

public class SimpleRead 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("COM7")) { 
     //    if (portId.getName().equals("/dev/term/a")) { 
       SimpleRead reader = new SimpleRead(); 
      } 
     } 
    } 
} 

public SimpleRead() { 
    try { 
     serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000); 
    } catch (PortInUseException e) {System.out.println(e);} 
    try { 
     inputStream = serialPort.getInputStream(); 
    } catch (IOException e) {System.out.println(e);} 
try { 
     serialPort.addEventListener(this); 
} catch (TooManyListenersException e) {System.out.println(e);} 
    serialPort.notifyOnDataAvailable(true); 
    try { 
     serialPort.setSerialPortParams(115200, 
      SerialPort.DATABITS_8, 
      SerialPort.STOPBITS_1, 
      SerialPort.PARITY_NONE); 
    } catch (UnsupportedCommOperationException e) {System.out.println(e);} 
    readThread = new Thread(this); 
    readThread.start(); 
} 

public void run() { 
    try { 
     //System.out.println("1"); 
     Thread.sleep(20000); 
    } catch (InterruptedException e) {System.out.println(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[4049]; 

     try { 
      while (inputStream.available() > 0) { 
       int numBytes = inputStream.read(readBuffer); 
      } 
      for(Byte bytenum: readBuffer)System.out.print(Integer.toHexString(bytenum)+" "); 

     } catch (IOException e) {System.out.println(e);} 
     break; 
    } 
    if (serialPort != null) { 
       try { 
        // close the i/o streams. 
        inputStream.close(); 
       } catch (IOException ex) { 
        System.out.println(ex); 
       } 
       // Close the port. 
       serialPort.close(); 
      } 

} 

}

,這裏是部分結果(注40之後該字節號未成功讀取) 1 2 3 4 5 6 7 8 9 ABCDEF 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 0 0 0 0 0 0 0

謝謝

回答

0

串口發送結束部分({0})。你必須首先突破。你可以得到消息的下一部分。但在你的代碼中,這是不可能的,因爲你關閉了流和端口。直到收到所有數據爲止,循環執行。