2013-04-01 108 views
-1

代碼從http://rxtx.qbang.org/wiki/index.php/Event_Based_Two_Way_Communication爲什麼我的緩衝區在此代碼中返回爲空?

適應我試圖讀取與SerialReader類常見的緩衝區和發送緩衝區out了通過SerialWriter類系列,但緩衝區顯示爲空每次作家被調用。該代碼是使用TwoWaySerialCommTest的連接方法初始化(下面粘貼供參考)

public SerialWriter (OutputStream out) 
      { 
       this.out = out; 
      } 

      public SerialWriter (OutputStream out, byte[] buffer) 
      { 
        this.out = out; 
        this.buffer = buffer; 
      } 

      public void run() 
      { 
        while(true) 
        { 
          lock.lock(); 
          try 
         { 
          dataAvailable.await(); 
          System.out.println("Waking up"); 
          int i = 0; 
          if (this.buffer != null) 
          { 
            System.out.println("Buffer isn't empty"); 
            while(buffer[i] != ((byte)'\n') && i < buffer.length - 1) 
            { 
              this.out.write(buffer[i]); 
            } 
          } 
          else 
          { 
            System.out.println("Buffer is null"); 
            System.out.println(this.buffer.toString()); 
          } 
         } 
         catch (IOException e) 
         { 
          e.printStackTrace(); 
          System.exit(-1); 
         } 
         catch(Exception e) 
         { 
          e.printStackTrace(); 
         } 

          finally 
          { 
            lock.unlock(); 
          } 
        } 
      } 
     } 

串行Reader類

public static class SerialReader implements SerialPortEventListener 
     { 
      private InputStream in; 
      byte[] buffer; 

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

      public SerialReader (InputStream in, byte[] buffer) 
      { 
        this.in = in; 
        this.buffer = buffer; 
      } 

      public void serialEvent(SerialPortEvent arg0) { 
       lock.lock(); 
        int data; 
        if (buffer != null) 
        { 
        for(int i = 0; i < buffer.length; i++) 
        { 
          if (buffer[i] != 0) 
          { 
            System.out.print((char)buffer[i]); 
          } 
        } 
        } 
        buffer = new byte[1024]; 

       try 
       { 
        int len = 0; 
        while ((data = in.read()) > -1) 
        { 
         if (data == '\n') { 
          break; 
         } 
         buffer[len++] = (byte) data; 
        } 
        System.out.println(new String(buffer,0,len)); 
        for(int i = 0; i < buffer.length; i++) 
        { 
          if (buffer[i] != 0) 
          { 
            System.out.print((char)buffer[i]); 
          } 
        } 
        System.out.println(); 
        dataAvailable.signal(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
        System.exit(-1); 
       } 
       finally 
       { 
        lock.unlock(); 
       } 
      } 

     } 

TwoWaySerialCommTest(截斷)

import gnu.io.SerialPortEvent; 
    import gnu.io.SerialPortEventListener; 

    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import java.util.concurrent.locks.*; 

    /** 
    * This version of the TwoWaySerialComm example makes use of the 
    * SerialPortEventListener to avoid polling. 
    * 
    */ 
    public class TwoWaySerialCommTest 
    { 
      static Lock lock = new ReentrantLock(); 
      static Condition dataAvailable = lock.newCondition(); 
      public volatile byte[] buffer; 

      public TwoWaySerialCommTest() 
     { 
      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 , buffer))).start(); 

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

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

我相信'codereview'是適用於代碼的。 – madth3

回答

1

一個問題似乎是這樣的line:

buffer = new byte[1024]; 

我不認爲你想分配本地buffer到一個新的字節數組。這導致作者寫入它自己的數組中,並且而不是傳入的內容由讀者共享。指定buffer僅影響寫入器內部的局部變量。

此外,什麼停止輸入流覆蓋buffer?如果行長度大於1024,那麼你將得到一個數組越界異常。

最後,您並未將\n字符寫入buffer。因此,讀者將在輸出中打印多個\0空字符。

+0

感謝您的快速回答,我知道緩衝區不會超過1024,因爲我將此程序用作查詢| arduino草圖的響應框架。我試圖做的與你強調的那一行是用下一個讀取步驟的新的空緩衝區替換當前的緩衝區。我認爲這會將新的緩衝區分配給全局可訪問的緩衝區,因爲我將它的引用傳遞給構造函數中的讀寫器。 – SMC

+0

您正在傳入數組@LinuxN00b。然後將其分配給方法中的新數組。這根本不會改變全局數組。它仍在被讀者使用。 – Gray

+0

所以爲了實現我想要的功能,我可能需要傳遞一個對TwoWaySerialCommTest類的引用並訪問公共緩衝區來重置它? – SMC

相關問題