如何让 Java 使用 Linux 中的串口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/638553/
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
How do I get Java to use the serial port in Linux?
提问by Phillip Gibb
We make use of a java application that manages a pinpad via the serial port. This works perfectly on windows with the Sun Comm.jar, the supplied dll and the properties file.
我们使用通过串行端口管理密码键盘的 Java 应用程序。这在带有 Sun Comm.jar、提供的 dll 和属性文件的 Windows 上完美运行。
Now we are attempting to use this solution on Linux (actually it does run on various other flavours of linux out in the field) - with Ubuntu server mode.
现在我们正尝试在 Linux 上使用这个解决方案(实际上它确实在该领域的各种其他版本的 linux 上运行) - 使用 Ubuntu 服务器模式。
After much attempts - blood, sweat and almost tears we have this scenario:
经过多次尝试 - 鲜血、汗水和几乎眼泪,我们得到了这样的场景:
Java version 1.4.2_17 Linux - Ubuntu Comm libs - Comm3 supplied by sun with the default driver specified
Java 版本 1.4.2_17 Linux - Ubuntu Comm libs - Sun 提供的 Comm3,并指定了默认驱动程序
An external comm test shows the comm ports: /dev/ttyS0 and /dev/ttyS1 But the java application says unable to open port /dev/ttyS1
外部通信测试显示通信端口:/dev/ttyS0 和 /dev/ttyS1 但是 java 应用程序说无法打开端口 /dev/ttyS1
(using the RXRT files produces invalid port errors)
(使用 RXRT 文件会产生无效的端口错误)
Has anyone been able to use java 1.4.2 on linux for serial port communication and found a solution that I could apply in my scenario?
有没有人能够在 linux 上使用 java 1.4.2 进行串行端口通信,并找到了可以在我的场景中应用的解决方案?
greatly appreciated
非常感激
Phill
菲尔
回答by Kerry
I know this is an old question but I have created a Java package and native library to allow serial port read and write, currently in synchronous mode only.
我知道这是一个老问题,但我创建了一个 Java 包和本机库以允许串行端口读写,目前仅处于同步模式。
It's open source and available on GitHub:
它是开源的,可在 GitHub 上获得:
j232- The Java source code
j232- Java 源代码
libj232- j232's native library.
libj232- j232 的原生库。
The wiki page is here.
维基页面在这里。
Note this is a Linux only package and library.
请注意,这是一个仅限 Linux 的包和库。
回答by jassuncao
Make sure that you are using a user with permissions to access the serial ports. Some distributions put the serial ports in the uucp group, so make sure that the user belongs to that group.
确保您使用的是有权访问串行端口的用户。一些发行版将串口放在 uucp 组中,因此请确保用户属于该组。
回答by tonys
We've been using the SerialIO libraries for Java serial communications on both Windows and Linux for several years now with great results (and no, I don't work for them :-)):
几年来,我们一直在 Windows 和 Linux 上使用 SerialIO 库进行 Java 串行通信,并取得了很好的结果(不,我不为他们工作 :-)):
http://serialio.com/products/serialport/serialport.php
http://serialio.com/products/serialport/serialport.php
The libraries do not autodetect the available serial ports on Linux though - you have to manually configure them.
不过,这些库不会自动检测 Linux 上的可用串行端口 - 您必须手动配置它们。
回答by Jonathan Hess
It's been a while since I've done this. I remember that you can't just open /dev/ttyS0 as a file and I/O to it.
我已经有一段时间没有这样做了。我记得你不能只打开 /dev/ttyS0 作为文件和 I/O。
I remember having the same problem with Sun IO jars also.
我记得 Sun IO jar 也有同样的问题。
The solution I remember settling on was to create a small command-line program in C that echoed stdin to the serial port and serial port in to stdout. Then, I launched this program from my java process. I don't have the code but there are lots of examples on the web for c programs that write to linux serial ports.
我记得解决的解决方案是在 C 中创建一个小的命令行程序,它将 stdin 回显到串行端口,并将串行端口回显到 stdout。然后,我从我的 java 进程启动了这个程序。我没有代码,但网上有很多关于写入 linux 串行端口的 c 程序的示例。
回答by Phillip Gibb
We did not change much, nothing in the libraries used. but removing the use of an EventListener on the serial port object fixed the problem.
我们没有太大变化,使用的库中没有任何变化。但是删除串行端口对象上的 EventListener 解决了这个问题。
This actually allowed for our code to work on linux (jre1.4.2, slackware and Suns Comm3 drivers) where this was not a problem on windows - strange.
这实际上允许我们的代码在 linux(jre1.4.2、slackware 和 Suns Comm3 驱动程序)上工作,而这在 windows 上不是问题 - 很奇怪。
Phill
菲尔

