Windows 上的 Java 串行通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/264277/
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
Java Serial Communication on Windows
提问by Steve
I've been looking around for a Java API that can communicate with serial devices on Windows/Win32 but many of the APIs I've checked out are either for Linux, too outdated, or just had bad critics.
我一直在寻找可以与 Windows/Win32 上的串行设备进行通信的 Java API,但我检查过的许多 API 要么是针对 Linux,要么是过时的,要么只是受到了不好的批评。
Can someone recommend one to me that they've tried or knows about that is easy to implement on Windows XP?
有人可以向我推荐一个他们已经尝试过或知道这很容易在 Windows XP 上实现的吗?
回答by Murat Ayfer
I started looking for the same thing couple weeks ago, and I've been very happy with the multi-platform RXTXlibrary so far. Works with any Windows, Linux and OS X. Has a very clean, easy to understand API.
几周前我开始寻找同样的东西,到目前为止我对多平台RXTX库非常满意。适用于任何 Windows、Linux 和 OS X。具有非常干净、易于理解的 API。
edit: RXTX is also open source.
编辑:RXTX 也是开源的。
回答by Lawrence Dol
Without reservation, I recommend Java Serial Portfrom serialio.com; I had significant stability problems with the Sun, IBM and RxTx serial package. SerialPort has been rock solid in production 24/7 for over 5 years.
毫无保留,我推荐serialio.com上的Java Serial Port;我在使用 Sun、IBM 和 RxTx 串行包时遇到了严重的稳定性问题。5 年来,SerialPort 在 24/7 全天候生产中一直坚如磐石。
They support the standard Java serial API, as well as their own alternative proprietary one. I would stick with the standard API though, unless you really need something theirs has that the standard one doesn't, just to keep your options open.
它们支持标准的 Java 串行 API,以及它们自己的替代专有 API。不过,我会坚持使用标准 API,除非您真的需要他们拥有而标准 API 没有的东西,只是为了让您的选择保持开放。
回答by Martin Naskovski
Java is notorious for its flaky serial I/O support. At a previous job, we tried both RXTX and SerialIO for an application that streamed data at 56kbps from a Teknic servo controller, and found them to gobble up the CPU quite a bit. Perhaps for apps that don't require continuous streaming from a serial port, both of these libraries are good, but we didn't feel that streaming I/O from a serial port should be eating a sustained 15-30% of the CPU on the machine when it is much needed for other threads in the JVM that need to be responsive.
Java 因其不稳定的串行 I/O 支持而臭名昭著。在之前的工作中,我们为一个应用程序尝试了 RXTX 和 SerialIO,该应用程序以 56kbps 的速度从 Teknic 伺服控制器传输数据,发现它们大量占用 CPU。也许对于不需要从串行端口连续流式传输的应用程序来说,这两个库都很好,但我们不认为从串行端口流式传输 I/O 应该占用持续 15-30% 的 CPU当 JVM 中需要响应的其他线程非常需要机器时。
Instead, we created a server in C++ that would read the stream of data from the serial port on the servo, transform/packetize it and send it to our Java app in XML over a socket connection. The CPU load on the serial I/O server in C++? Barely creeping into 1% at its worst.
相反,我们用 C++ 创建了一个服务器,该服务器将从伺服器上的串行端口读取数据流,对其进行转换/打包并通过套接字连接以 XML 格式将其发送到我们的 Java 应用程序。C++ 中串行 I/O 服务器上的 CPU 负载?在最坏的情况下勉强爬到 1%。
There are certain things Java does well - serial I/O, in my opinion, isn't one of them, depending on the type of application...
Java 在某些方面做得很好 - 在我看来,串行 I/O 不是其中之一,这取决于应用程序的类型......
Ultimately, you should take even what I said with a grain of salt, and try both, RXTX and SerialIO (which is dirt cheap, like $50 or so for the java version) and if they meet your needs, go with it. Personally, I'd stick with SerialIO because it is supported and actively worked on. RXTX, not so much.
最终,您甚至应该对我所说的持保留态度,并同时尝试 RXTX 和 SerialIO(它非常便宜,Java 版本大约 50 美元左右),如果它们满足您的需求,请使用它。就我个人而言,我会坚持使用 SerialIO,因为它得到了支持并且正在积极开发中。RXTX,没那么多。
回答by Thibaut D.
I've written an open-source Java library because none of the existing ones fit my needs (outdated, closed-source, hard to modify, un-maintained, ...).
我编写了一个开源 Java 库,因为现有的库都不能满足我的需求(过时的、闭源的、难以修改的、未维护的……)。
It's called JSerial, it's MIT-licensed, and you can learn more here: https://github.com/thibautd/JSerial!
它称为 JSerial,已获得 MIT 许可,您可以在此处了解更多信息:https: //github.com/thibautd/JSerial!
Currently only supports Windows, but I have plans for supporting Linux. You can easily modify the native part with the latest Visual Studio if you need.
目前只支持 Windows,但我有支持 Linux 的计划。如果需要,您可以使用最新的 Visual Studio 轻松修改本机部分。
回答by Jason S
I've been using PureJavaCommfor the last five years or so. It's actively maintained, and is a pure Java (via JNA) implementation. RXTX has let me down because of a lack of maintenance and numerous subtle problems.
在过去五年左右的时间里,我一直在使用PureJavaComm。它被积极维护,是一个纯 Java(通过 JNA)实现。由于缺乏维护和许多微妙的问题,RXTX让我失望。