2017-05-22 8 views
-1

제가 작업하고있는 RXTX 프로젝트가 있습니다. 나는 다음과 같이이 업을 설정할 수 있습니다데이터가있는 경우 스트림은 0 바이트를 어떻게 반환합니까?

연결이되는 방법, 그것은 다음과 같은 트리거 데이터를 사용할 수있을 때 통지하기로했다 리스너있다
public void doConnect(ActionEvent event) 
    { 
     String selectedPort = (String)connectTabController.portList.getValue(); 
     System.out.println("Connecting to: " + selectedPort); 
     selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort); 
     CommPort commPort = null; 
     try 
     { 
      commPort = selectedPortIdentifier.open("AT QC ReponseTime", TIMEOUT); 
      serialPort = (SerialPort)commPort; 
      setConnected(true); 
      if (isConnected) 
        { 
         if (initIOStream() == true) 
           { 
            initListener(); 
            System.out.println("Initializing listener"); 
            connectTabController.gui_changeStatusLabel("Device Connected!"); 
           } 
        } 
     } 
     catch (PortInUseException e) 
     { 
      System.out.println("Port In use! " + e.toString()); 
     } 

     catch (Exception e) 
     { 
        System.out.println("Failed to open! " + e.toString()); 
     } 
    } 
    public boolean initIOStream() 
    { 
     //return value for whether opening the streams is successful or not 
     boolean successful = false; 

     try { 
      // 
      input = serialPort.getInputStream(); 
      output = serialPort.getOutputStream(); 
      writeData(RESETTPOD); 
      System.out.println("Writing Reset command"); 

      successful = true; 
      System.out.println("IO Stream opened successfully!"); 
      return successful; 

     } 
     catch (IOException e) { 
      System.out.println("I/O Streams failed to open. (" + e.toString() + ")"); 
      return successful; 
     } 
    } 


    public void initListener() 
    { 
     try 
     { 
      serialPort.addEventListener(this); 
      serialPort.notifyOnDataAvailable(true); 
     } 
     catch (TooManyListenersException e) 
     { 
      System.out.println("Too many listeners. (" + e.toString() + ")"); 

     } 
    } 

그러나

@Override 
    public void serialEvent(SerialPortEvent evt) { 

     BufferedReader reader = null; 

     if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) 
     { 
      try 
      { 
       reader = new BufferedReader(new InputStreamReader(input)); 

       if (reader.ready()) 
       { 
       fullLine = reader.readLine(); 
       System.out.println(fullLine + "\n"); 
       } 

      } 
      catch (Exception e) 
      { 
       System.out.println("@SerialEvent Failed to read data. (" + e.toString() + ")"); 
      } 
     } 
} 

을, "계속되는 입력 스트림 반환 된 제로 바이트"를 계속받습니다.

읽을 내용이 없으면 처음부터 청취자가 트리거되지 않아야하기 때문에 이해가되지 않습니다. 난 애플 리케이션을 실행 해봤는데 데이터를 보내는 장치의 버스트 - 출력에 해당하는 모든 1/3 초 마다이 오류 메시지가 계속. (PuttY와 같은 프로그램에서 잘 작동 함)

+0

는 동일한 입력 스트림에서 버퍼링 독자를 만드는 반복하지 있습니까? –

+0

글쎄, 네가 이제는 내가 그걸 무시한다고 생각할 때마다 새로운 버퍼링 된 리더를 만들 것이라고 생각한다. 그러나 initIOStrema 메서드에서 버퍼링 된 판독기를 만들면 (바로 입력/출력에 값을 할당 한 후에도) 동일한 문제가 발생합니다. –

+0

다른 스트림에서 래핑하는 경우 기본적으로 기본 스트림에 대한 참조를 유지하는 데 실수가 있습니다. 리더. –

답변

0

BufferedReader를 사용하려는 경우 javax.comm, CommPort 및 getInputStream에 대한 refence API 문서를 살펴보십시오. 그런 다음 임계 값에 대해 다른 설정을 사용하고 제한 시간을 받으십시오.

예). serialPort.enableReceiveThreshold (3); serialPort.enableReceiveTimeout (1);

https://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/