[英]How can I programmatically monitor the activity on an already opened serial port?
[英]monitor serial port that already open with java
我正在監視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);
謝謝
您不能監視串行端口是否被另一個程序打開,因為串行端口不能同時打開2次。
您可以使用PortMon之類的軟件手動嘗試監視串行端口。
但是,此軟件僅在Windows NT之前可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.