费了好大的劲搞定Linux系统上用Java写串口通信的问题。
jdk中没有原生的串口api,网上找了半天的资料,大概知道了:Linux系统上用Java写串口程序,有两个包比较常用,一个是当年sun官方出的javacomm,但是找了半天都是老版本的居多,oracle官方不提供下载了,不爽。另一个是gnu的rxtx comm,看了一下还算靠谱,不过官方的wiki上(http://rxtx.qbang.org/wiki/index.php/Main_Page)说linux版本的在卸载usb转串口线的时候会崩溃,顿时心头一颤。下载下来之后试了一下,确实可以用,系统里几个原生的串口都识别了,但唯独我的Arduino板子不识别。
捉急之时想到了Arduino IDE里面是有Serial Monitor的,而且也是用Java写的,于是去Arduino的安装目录里面找找,果然发现了在arduino安装目录下的lib目录下,有RXTXcomm.jar、librxtxSerial.so、librxtxSerial64.so这三个文件(我的Linux是64位的,不知道32位的是不是没有64.so这个文件),可以去Arduino官网上下载一个IDE,把这几个文件复制出来,按照rxtx wiki上说明使用,用法是一样的。
如果也是用的64位的系统,可以从这里下载:http://download.csdn.net/detail/bhq2010/5141550
如果要脱离eclipse单独执行串口通信程序,将RXTXcomm.jar文件复制到$JAVA_HOME/jre/lib/ext目录下,将librxtxSerial.so和librxtxSerial64.so复制到$JAVA_HOME/jre/lib/amd64/目录下(如果是32位系统应该是i386或者i686,而不是amd64)就OK了,官方wiki上描述的安装步骤很麻烦,做了一下不行。
下面是一个例程,是官方的wiki里例程的改进版【PS:以下代码在win7上用不了,不能用BufferedReader包装InputStream,不知道为啥,有知道原因的童鞋请告诉我】:
package test;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class TwoWaySerialComm
{
public TwoWaySerialComm()
{
super();
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader r = new BufferedReader(reader);
(new Thread(new SerialReader(r))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
/** */
public static class SerialReader implements Runnable
{
BufferedReader in;
public SerialReader ( BufferedReader in )
{
this.in = in;
}
public void run ()
{
try
{
String line;
while ((line = in.readLine()) != null){
System.out.println(line);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
/** */
public static class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public void run ()
{
try
{
int c = 0;
while ( ( c = System.in.read()) > -1 )
{
this.out.write(c);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
static void listPorts()
{
@SuppressWarnings("unchecked")
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while ( portEnum.hasMoreElements() )
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) );
}
}
static String getPortTypeName ( int portType )
{
switch ( portType )
{
case CommPortIdentifier.PORT_I2C:
return "I2C";
case CommPortIdentifier.PORT_PARALLEL:
return "Parallel";
case CommPortIdentifier.PORT_RAW:
return "Raw";
case CommPortIdentifier.PORT_RS485:
return "RS485";
case CommPortIdentifier.PORT_SERIAL:
return "Serial";
default:
return "unknown type";
}
}
public static void main ( String[] args )
{
listPorts();
try
{
(new TwoWaySerialComm()).connect("/dev/ttyACM0");
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
分享到:
相关推荐
Linux系统下用Java开发串口通信程序的类库,从Arduino的IDE中扒出来的,比官方的更加好用,Bug少,支持Arduino设备与Linux之间的串口通信;Linux系统下用Java开发串口通信程序的类库,从Arduino的IDE中扒出来的,比...
RaspberryPi树莓派处理器使用的是ARM架构,而一般电脑则为x86或x64架构,均不能适用,故只能在系统中重新编译安装RXTX源码,此so文件为在arm架构的树莓派编译制作的文件,直接放在jre目录即可运行
支持LINUX系统下串口通信,亲测有效,使用了第三方软件进行补充LINUX环境不能调用串口资源包的问题
JAVA访问串口RXTX包,包含演示代码,windows 64位系统和linux 64位下测试可以使用。演示代码用了netty包,开发工具Idea, windows下安装包方法: a.复制rxtxSerial.dll 到%JAVA_HOME%\jreX/bin/ b.复制RXTXcomm....
java串口通信RXTX在windows64位、Windows32位、Linux的jar集合 官方网站地址:http://rxtx.qbang.org/wiki/index.php/Download 分支地址(64bit)开源项目地址:http://fizzed.com/oss/rxtx-for-java 复制rxtxSerial...
常见的Java串口包有SUN在1998年发布的串口通信 API:comm2.0.jar(Windows下)、comm3.0.jar(Linux/Solaris);都打包到一起了
JAVA串口通信包comm2.0.3.zip(linux环境)
NULL 博文链接:https://dengzhangtao.iteye.com/blog/1905143
同时支持linux和Windows下java与串口通信的rxtx包,亲测可用 linux下的安装路径为: cp librxtxSerial.so $JAVA_HOME/jre/lib/amd64/ cp RXTXcomm.jar $JAVA_HOME/jre/lib/ext windows下安装路径为: Copy ...
利用RXTX进行java串口开发,及开发部署过程中遇到的问题解决,具体详见https://blog.csdn.net/j1805l1377/article/details/106335723
Java串口通信,RXTXcomm.jar,含win-x86/win-x64/win-ia64/Linux-x86_x64/Linux-i386
java rxtx串口通讯windows平台 64位,32位, linux平台64位,32位平台全部都有
java串口通信RXTX在windows和linux(64位和32位)的jar集合
commapi基础 RS232基础 Win32&Linux;下串口编程的差异 串口实现全双工通讯的Java类库等共8个文件,全面掌握JAVA串口编程
个人精心搜集编写的,针对笔记本没有串口的情况下在ubuntu系统中开发串口通讯程序的例子,包括环境配置和例子程序,绝对经典
JAVA串口通讯本地库(rxtxSerial2.2) 1.多平台支持(macOS,Linux,aarch64) 2.SerialPortDemo.jar为演示程序 3.相应平台的库名应改为librxtxSerial.xxx再使用;
本文档主要讲述的是Java串口通信编程指南;在java中,利用Java Communication包可以操作串口,但官方的包在3.0之后就只支持Linux和Solaris平台了,Windows平台的只支持到98年出的 2.0版本,不过在XP下还能使用。另外...
Linux系统下用Java开发串口通信程序的类库,从Arduino的IDE中扒出来的,比官方的更加好用,Bug少,支持Arduino设备与Linux之间的串口通信
这是基于Linux下采用RS485通信基础。代码的详解,没部分都有,。