程序运行总是报告如下异常:
javax.comm.PortInUseException: Port currently owned by Serial
at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:337)
at com.test.Test.<init>(Test.java:38)
at com.test.Test.main(Test.java:30)
Test.java类代码如下:import java.io.*;
import java.util.*;
import javax.comm.*;/**
 * 读串口的例程
 * @author zcj
 */public class Test {
static CommPortIdentifier portId;
static Enumeration portList;// 枚举类
InputStream inputStream;
SerialPort serialPort; public static void main(String[] args) {
//枚举出系统所有的RS232端口
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {

portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
 if (portId.getName().equals("COM1"))/* 找Windows下的第一个串口*/ {
//if (portId.getName().equals("/dev/term/a"))/* 找Unix-like系统下的第一个串口 */{
new Test().print();
}
}
}
} public void print() {
try {
//获得串口对象
serialPort = (SerialPort) portId.open("TestApp", 2000);

// 获取端口的输入流对象
inputStream = serialPort.getInputStream();

//读取输入流
int ch;
StringBuffer sb = new StringBuffer();
while((ch=inputStream.read())>0){
sb.append((char)ch);
}
System.out.println("读取到的内容为:"+sb.toString());

}catch (Exception e) {
e.printStackTrace();
}
serialPort.close();
}
}

解决方案 »

  1.   

    嗯,是的。
    异常是在下面这段代码处出现的: 
    serialPort = (SerialPort) portId.open("TestApp", 2000);但是我不知道这地方有什么问题呢?
      

  2.   

    你是一直都这样。还是现在才这样?
    你的comm那些文件都放对地方了吗?我有一年多没有做Comm口项目了。
    我以前也遇过很多类似问题。你先检查你的文件是否都放到对应的目录。
      

  3.   

    帮你测试过了。只要你把dll放入bin目录。把javax.comm.properties放入Lib目录。
    把comm.jar放入ext目录就OK了。代码本身没有问题。
    至于其它逻辑没看。如果有问题再提。
      

  4.   

    呵呵,我的文件放的位置都是对的,你说的那些我之前都看到过的。呵呵。
    不过现在问题已经解决了。是用ComTool这个软件工具测试的。
    不知道电脑怎么回事,8005这个端口被占用了。
    现在结贴,楼上的40分,1楼的10分。