2017-01-24 3 views
0

ポート接続を設定するためにtwoWaySerialCommオブジェクトを宣言しました。 twoWaySerialCommクラスの中で、私は 'SerialReader'というクラスの入ってくるシリアルデータを読み込むためのスレッドを作成しました。メインスレッド「twoWaySerialComm」からシリアルデータにアクセスするにはどうすればよいですか?双方向シリアル通信 - シリアルリーダースレッドから読み取られた文字列にアクセスできない

私は受信したシリアルデータをwhileループ内の 'SerialReader'クラスの 'hex'文字列に読み込んでいました。 whileループ内に入ってくるデータを印刷することはできますが、 'twoWaySerialComm'クラスのこの 'hex'文字列にアクセスすることはできません。 whileループの外側で 'hex'文字列にアクセスできないのはなぜですか?どのようにこれを行うことができますか?

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.NoSuchPortException; 
import gnu.io.PortInUseException; 
import gnu.io.SerialPort; 
import gnu.io.UnsupportedCommOperationException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author asanka 
*/ 
public class TwoWaySerialComm { 

    private static TwoWaySerialComm twoWaySerialComm; 

    String read; 

    private TwoWaySerialComm() throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException { 
     String portName = "/dev/ttyUSB0"; 
     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(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 

       InputStream in = serialPort.getInputStream(); 
       OutputStream out = serialPort.getOutputStream(); 

       SerialReader reader; 
       (new Thread(reader = new SerialReader(in))).start(); 
       read = reader.readHex(); 

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

    public static TwoWaySerialComm getTwoWaySerialComm() throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException { 
     if (twoWaySerialComm == null) { 
      twoWaySerialComm = new TwoWaySerialComm(); 
     } 
     return twoWaySerialComm; 
    } 

    public String data() { 
     return read; 
    } 

    /** 
    * 
    */ 
    public static class SerialReader implements Runnable { 

     String hex; 
     InputStream in; 

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

     public void run() { 
      byte[] buffer = new byte[1024]; 

      int len; 
      try { 
       while ((len = this.in.read(buffer)) > -1) { 

        hex = new String(buffer, 0, len).replaceAll("\\s", ""); 
        Thread.sleep(1000); 
        System.out.println(hex); 
       } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(TwoWaySerialComm.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

     public String readHex() { 
      return hex; 
     } 
    } 

    /** 
    * 
    */ 
    public static class SerialWriter implements Runnable { 

     OutputStream out; 

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

     public void run() { 
      try { 
       int c = 0; 
       while ((c = System.in.read()) > -1) { 
        this.out.write(c); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

答えて

0

私が見る限り、別のスレッドで読み取り操作を実行しています。 readHex()メソッドを呼び出すと、そのスレッドはまだ16進数フィールドにまだ入力していません。

新しいスレッドでjoin()を呼び出す必要があります。 または、java.util.concurrentパッケージのCompletableFutureを使用します。

関連する問題