2012-04-26 9 views
2

私はcomポートの監視に取り組んでおり、利用可能なデータを取得しているので、ポートは別のプログラムで使用されているので、ポートを開いていないと監視することはできません これはコードです既にjavaで開いているシリアルポートをモニタする

package comtest; 

import javax.comm.*; 
import java.io.*; 

public class PortTyper { 

    public static void main(String[] args) { 

     try { 
      CommPortIdentifier com = 
        CommPortIdentifier.getPortIdentifier("COM2"); 

      CommPort thePort = com.open("port", 10); 

      CopyThread output = new CopyThread(thePort.getInputStream(), 
        System.out); 

      output.start(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 
} 

class CopyThread extends Thread { 

    InputStream theInput; 
    OutputStream theOutput; 


    CopyThread(InputStream in, OutputStream out) { 
     theInput = in; 
     theOutput = out; 
    } 

    @Override 
    public void run() { 

     try { 
      byte[] buffer = new byte[256]; 
      while (true) { 
       int bytesRead = theInput.read(buffer); 
       if (bytesRead == -1) { 
        break; 
       } 
       theOutput.write(buffer, 0, bytesRead); 
      } 
     } catch (IOException e) { 
      System.err.println(e); 
     } 
    } 
} 

はそう私はちょうど私がいる場合は、シリアルポートを監視することはできませんデータが

CommPort thePort = com.open("port", 10); 

おかげ

答えて

2

なしCOM2に来るかを見ることができますシリアルポートを同時に2回開くことができないため、別のプログラムによってtが開かれます。

-1

PortMonのようなソフトウェアを使用してシリアルポートを手動で監視することができます。

ただし、このソフトウェアはWindows NTでのみ使用できます。

関連する問題