-3

可能重复:
为什么当我使用 NetBeans 6.8 和 Eclipse 运行此代码时输出会有所不同?

当我使用 Eclipse 和 NetBeans 6.8 运行以下代码时。我想查看计算机上可用的 COM 端口。在 Eclipse 中运行时,它会返回所有可用的 COM 端口,但在 NetBeans 中运行时,它似乎没有找到任何端口..

public static void test() {
    Enumeration lists=CommPortIdentifier.getPortIdentifiers();

    System.out.println(lists.hasMoreElements());
    while (lists.hasMoreElements()) {
        CommPortIdentifier cn =
          (CommPortIdentifier)lists.nextElement();

        if ((CommPortIdentifier.PORT_SERIAL==cn.getPortType())) {
            System.out.println(
              "Name is serail portzzzz " +
              cn.getName()+
              " Owned status " +
              cn.isCurrentlyOwned());

            try {
                SerialPort port1=(SerialPort)cn.open("ComControl",800000);
                port1.setSerialPortParams(
                  9600,
                  SerialPort.DATABITS_8,
                  SerialPort.STOPBITS_1,
                  SerialPort.PARITY_NONE);
                System.out.println("Before get stream");
                OutputStream out=port1.getOutputStream();
                InputStream input=port1.getInputStream();
                System.out.println("Before write");
                out.write("AT".getBytes());
                System.out.println("After write");
                int sample=0;
                //while((( sample=input.read())!=-1)){
                System.out.println("Before read");
                //System.out.println(input.read() + "TEsting ");
                //}
                System.out.println("After read");
                System.out.println(
                  "Receive timeout is " +
                  port1.getReceiveTimeout());
            }
            catch(Exception e) {
                System.err.println(e.getMessage());
            }
        }
        else {
            System.out.println(
              "Name is parallel portzzzz " +
              cn.getName() +
              " Owned status " +
              cn.isCurrentlyOwned() +
              cn.getPortType() +
              "    ");
        }
    }
}

使用 Netbeans 输出,

错误的

使用 Eclipse 输出,

true
Name is serail portzzzz COM1 Owned status false
Before get stream
Before write
After write
Before read
After read
Receive timeout is -1
Name is serail portzzzz COM2 Owned status false
Before get stream
Before write
After write
Before read
After read
Receive timeout is -1
Name is parallel portzzzz LPT1 Owned status false2
Name is parallel portzzzz LPT2 Owned status false2
4

1 回答 1

2

最初的猜测是您使用的库使用包含在 DLL 中的本机代码,并且无法找到该代码,从而在您之前错过了一个错误,并且代码回退到一个虚拟行为。

我会仔细查看初始化代码,看看那里发生了什么。

于 2010-12-26T11:52:29.023 回答