2017-05-22 10 views
-1

私が取り組んでいるRXTXプロジェクトがあります。私は次のようにUPSを設定している:データがある場合、ストリームはどのようにゼロバイトを返しますか?

接続がなされている方法です、そしてそれは、次のトリガのデータが利用可能な場合に通知するはずのリスナー、持ってい
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() + ")"); 
      } 
     } 
} 

を、私は "入力ストリームが戻ってきて0バイトを返す"のを続ける。

これは意味がない。なぜなら、何も読んでいなければ、リスナーは最初に置かれるべきではないからだ。私は、アプリケーションを実行しようとしたと私は約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/

関連する問題