我正在监视 com 端口并获取可用的数据,该端口正在被另一个程序使用,所以是否可以在不打开它的情况下监视端口这是代码
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);
}
}
}
所以我可以直接看看 COM2 没有什么数据吗
CommPort thePort = com.open("port", 10);
谢谢