Linux Java进行串口通信

说明

我是使用的的是gnu的rxtx comm包以及librxtxSerial.so来进行串口通信。so文件需要根据你的系统来进行选择。否则在程序运行时会提示错误。
各系统版本依赖包

配置

  • Java路径配置
    cat /etc/profile
    cat /etc/profile.d/java.sh
    确定两个文件下的CLASSPATH配置过 J A V A H O M E / j r e / l i b 和 P A T H 配 置 过 JAVA_HOME/jre/lib 和PATH配置过 JAVAHOME/jre/libPATHJAVA_HOME/jre/lib,如果没配置上,请加上,否则会程序运行时会找不到相应的依赖包

  • 串口权限
    vi /etc/group
    将如下2段修改为(没有需新增):
    uucp:x:14:uucp 修改为:uucp:x:14:uucp,afc
    lock:x:54: 修改为:lock:x:54:afc

  • 选择依赖包
    JDK是32位,选择i686-unknown-linux-gnu目录;
    JDK是64位和操作系统是IA-64时,选择ia64-unkown-linux-gnu目录;
    JDK是64位和操作系统是x86_64时,选择x86_64-unknown-linux-gnu目录,把目录中的文件拷贝到/usr/java/jdk1.8.0_152/jre/lib/amd64;
    拷贝RXTXcomm.jar文件放在 /usr/java/jdk1.8.0_152/jre/lib/amd64/ext下

配置例子

因为我是x86_64以及64位的jdk因此的我配置如下图:
系统信息
JDK信息
依赖包目录
RXTX目录
如果选错了依赖包,或者是放置错了路径,会出现如下提示:

  • 选错依赖包
[ ]$java -classpath . SerialTest
Java HotSpot(TM) 64-Bit Server VM warning: You have loaded library /usr/java/jdk1.8.0_152/jre/lib/amd64/librxtxSerial.so which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c ', or link it with '-z noexecstack'.
java.lang.UnsatisfiedLinkError: /usr/java/jdk1.8.0_152/jre/lib/amd64/librxtxSerial.so: /usr/java/jdk1.8.0_152/jre/lib/amd64/librxtxSerial.so: 无法打开共享对象文件: 没有那个文件或目录 (Possible cause: can't load IA 64-bit .so on a AMD 64-bit platform) thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/java/jdk1.8.0_152/jre/lib/amd64/librxtxSerial.so: /usr/java/jdk1.8.0_152/jre/lib/amd64/librxtxSerial.so: 无法打开共享对象文件: 没有那个文件或目录 (Possible cause: can't load IA 64-bit .so on a AMD 64-bit platform)
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1845)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1122)
	at gnu.io.CommPortIdentifier.(CommPortIdentifier.java:83)
	at SerialTest.main(SerialTest.java:29)
  • 路径错误
[ ]$ java -classpath . SerialTest
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1122)
	at gnu.io.CommPortIdentifier.(CommPortIdentifier.java:83)
	at SerialTest.main(SerialTest.java:29)
[afc@cdmlc APP]$ 
  • 权限错误
check_group_uucp(): error testing lock file creation Error details:权限不够check_lock_status: No permission to create lock file.
please see: How can I use Lock Files with rxtx? in INSTALL

测试源码

  • SerialTest
import java.util.Enumeration;

import gnu.io.CommPortIdentifier;

public class SerialTest {

	public static void main(String[] args) {
		try {
			Enumeration portList = CommPortIdentifier.getPortIdentifiers();
			while (portList.hasMoreElements()) {
				CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
				System.out.println("获取到的串口名称为:COM" + portId.getPortType());
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

}

  • ShowJvmBit
import java.io.PrintStream;

public class ShowJvmBit
{
  public static void main(String[] paramArrayOfString)
  {
    String str = System.getProperty("sun.arch.data.model");
    System.out.println("This JVM is " + str + "-bit");
  }
}

你可能感兴趣的:(Serial,Linux,Java,串口)