在 Windows 上使用 RXTX Java 库的 NoSuchPortException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/274179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:21:49  来源:igfitidea点击:

NoSuchPortException using RXTX Java library on Windows?

javawindowsrxtx

提问by Steve

I have followed the instructions to setup rxtx on windows from http://www.jcontrol.org/download/readme_rxtx_en.html.

我已按照说明在 Windows 上从http://www.jcontrol.org/download/readme_rxtx_en.html设置 rxtx 。

What I did exactly was copy rxtxSerial.dll to "C:\Program Files\Java\jdk1.6.0_07\jre\bin" and copied RXTXcomm.jar to "C:\Program Files\Java\jdk1.6.0_07\jre\lib\ext" (my JAVA_HOME variable is set to C:\Program Files\Java\jdk1.6.0_07\jre)

我所做的就是将 rxtxSerial.dll 复制到“C:\Program Files\Java\jdk1.6.0_07\jre\bin”并将 RXTXcomm.jar 复制到“C:\Program Files\Java\jdk1.6.0_07\jre\” lib\ext”(我的 JAVA_HOME 变量设置为 C:\Program Files\Java\jdk1.6.0_07\jre)

I also added RXTXcomm.jar to my eclipse project.

我还将 RXTXcomm.jar 添加到我的 eclipse 项目中。

But when I run it, it still says "NoSuchPortException"

但是当我运行它时,它仍然显示“NoSuchPortException”

Devel Library
=========================================
Native lib Version = RXTX-2.0-7pre1
Java lib Version   = RXTX-2.0-7pre1
java.lang.ClassCastException: gnu.io.RXTXCommDriver cannot be cast to gnu.io.CommDriver thrown while loading gnu.io.RXTXCommDriver
gnu.io.NoSuchPortException
    at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:218)
    at TwoWaySerialComm.connect(TwoWaySerialComm.java:20)
    at TwoWaySerialComm.main(TwoWaySerialComm.java:107)

In my java file, I tell it:

在我的 java 文件中,我告诉它:

        try
        {
            (new TwoWaySerialComm()).connect("COM4");
        }

and I've also tried the Java Comm API. Both cannot recognize my serial port but I am sure I followed the instruction correctly. There files are there.

我也尝试过 Java Comm API。两者都无法识别我的串行端口,但我确信我正确地遵循了说明。那里有文件。

Does anybody have any idea what it could be?

有谁知道它可能是什么?

回答by Ken Gentle

Try putting rxtxSerial.dllin

尝试把rxtxSerial.dll

C:\Program Files\Java\jdk1.6.0_07\jre\lib\bin
                                      ^^^

回答by Lawrence Dol

It may be that your system does not have a COM4 defined or it's not accessible. It's hard to guess what may be wrong, because you haven't posted you port init code - what you posted looks like wrapper code.

可能是您的系统没有定义 COM4 或无法访问。很难猜测可能有什么问题,因为您还没有发布端口初始化代码 - 您发布的内容看起来像是包装器代码。

Here is my working init code using the javax.comm API (but using SerialPort from serialio.com):

这是我使用 javax.comm API 的工作初始化代码(但使用来自 serialio.com 的 SerialPort):

// name comes from config and is "COM1", "COM2", ...
SerialPort port=(SerialPort)CommPortIdentifier.getPortIdentifier(name).open("YourPortOwnerIdHere",5000);      // owner and ms timeout
port.setSerialPortParams(bau,dtb,stb,par);
port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN|SerialPort.FLOWCONTROL_RTSCTS_OUT);
port.enableReceiveTimeout(1000);

Hopefully this points you in the right direction.

希望这为您指明了正确的方向。

回答by Peter

you can use CommPortIdentifier.getPortIdentifiers()

您可以使用 CommPortIdentifier.getPortIdentifiers()

to identify all possible ports your system finds.

识别系统找到的所有可能的端口。

回答by Daniel Schneller

I am not too familiar with RXTX, but is this normal?

我对RXTX不太熟悉,但这正常吗?

java.lang.ClassCastException: gnu.io.RXTXCommDriver cannot be cast to gnu.io.CommDriver thrown while loading gnu.io.RXTXCommDriver

Otherwise maybe the problem is not with the port itself after all, but something with the classes themselves? Just a guess.

否则也许问题不在于端口本身,而在于类本身?只是一个猜测。

回答by johnstosh

I agree that you're problem looks like a ClassCastException and not the other.

我同意你的问题看起来像一个 ClassCastException 而不是另一个。

For windows, I'm using "Windows Java Serial Com Port Driver" at http://www.engidea.com/blog/informatica/winjcom/winjcom.htmland it is much easier for me to set up.

对于 Windows,我使用http://www.engidea.com/blog/informatica/winjcom/winjcom.html 上的“Windows Java Serial Com Port Driver”,设置起来要容易得多。

In either case, you want the DLL in the BIN directory, not LIB\BIN as was suggested. At least that's what's working for me. I'm using NetBeans and I've also found it helpful to put the jar and dll into various bin and lib\ext folders in the JDK.

在任何一种情况下,您都希望 DLL 位于 BIN 目录中,而不是建议的 LIB\BIN 中。至少这对我有用。我正在使用 NetBeans,我还发现将 jar 和 dll 放入 JDK 中的各种 bin 和 lib\ext 文件夹很有帮助。

Note that if you have multiple versions of the JRE on your machine, you might not be using the one that you think you are using. Also, as a practical matter I've found it more helpful to just copy both the jar and dll into the various bin and lib\ext folders. Makes it just a paste, paste, paste operation.

请注意,如果您的机器上有多个版本的 JRE,您可能没有使用您认为正在使用的那个版本。此外,作为一个实际问题,我发现将 jar 和 dll 都复制到各种 bin 和 lib\ext 文件夹中更有帮助。使它只是一个粘贴,粘贴,粘贴操作。

For windows, I recommend "Windows Java Serial Com Port Driver" because it solved my problems with USB serial ports. I had fits with RXTX because it would crash when the USB was unplugged. winjcom solved that problem and others as well. It has very helpful error exceptions.

对于windows,我推荐“Windows Java Serial Com Port Driver”,因为它解决了我的USB串口问题。我用过 RXTX,因为拔掉 USB 时它会崩溃。winjcom 也解决了这个问题和其他问题。它有非常有用的错误异常。

Also, make sure your serial drivers are up-to-date. Downloading an update fixed my other bug. -Stosh

另外,请确保您的串行驱动程序是最新的。下载更新修复了我的另一个错误。-斯托什

回答by johnstosh

I also had a problem when closing the serialPort within the serialEvent function. Maybe it's a deadlock problem, where the close method waits forever for serialEvent's lock to be released. Starting a new thread to close the port worked for me.

在 serialEvent 函数中关闭 serialPort 时我也遇到了问题。也许这是一个死锁问题,close 方法永远等待 serialEvent 的锁被释放。启动一个新线程来关闭端口对我有用。

回答by wiking

You can also try an alternative solution that was specifically implemented for Windows. There should be plenty available, one of them you can get from http://www.caerustech.com/JCommWin32.php

您还可以尝试专门为 Windows 实现的替代解决方案。应该有很多可用的,其中之一可以从http://www.caerustech.com/JCommWin32.php获得

Shultz

舒尔茨

回答by Memafe

For your question, my code is the following:

对于您的问题,我的代码如下:

if (idPuerto == null)
{
            formulario = form;
            boolean encontrado = false;


            listaPuertos = CommPortIdentifier.getPortIdentifiers();

            while( listaPuertos.hasMoreElements() && encontrado == false )
            {
              idPuerto = (CommPortIdentifier)listaPuertos.nextElement();
              //System.out.println(idPuerto.getName());

              if( idPuerto.getPortType() == CommPortIdentifier.PORT_SERIAL )
              {
                if( idPuerto.getName().equals(RFIDBascApp.ComBasc) )
                {        
                    encontrado = true;
                    logger.AddInfoUser("Puerto serie encontrado");

                  }
                }
              }

回答by darkphoenix

@Pinheiro you might want to take a look at this

@Pinheiro 你可能想看看这个

回答by Hamedz

You had NoSuchPortException, so first of all iterate on all available ports!

你有NoSuchPortException,所以首先迭代所有可用的端口!

import gnu.io.CommPortIdentifier;        
import java.util.Enumeration;  

public class ListAvailablePorts {  

    public void list() {  
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();  

        while(ports.hasMoreElements()){  
            CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
            System.out.println(port.getName());
        }
    }  

    public static void main(String[] args) {  
        new ListAvailablePorts().list();  
    }  
}