Java串口编程(开源RxTx实现串口)

来源:互联网 发布:流星 网络骑士 编辑:程序博客网 时间:2024/05/20 13:11
RxTx是提供串口和并口通信的开源Java类库。该项目提供了Windows、Linux、Mac os X、Solaris操作系统下的兼容javax.comm串口通讯包API的实现。RXTXcomm.jar包是封装了comm.jar的方法

安装:

1.copy rxtxSerial.dll to [JDK-directory]/jre/bin/rxtxSerial.dll
2.copy RXTXcomm.jar to [JDK-directory]/jre/lib/ext/RXTXcomm.jar
3.copy rxtxParallel.dll to windows/system32

实例:
package com;import gnu.io.*;import java.util.Enumeration;public class CommPort {static Enumeration<CommPortIdentifier> portList;static CommPortIdentifier portId;/** * 列出本地连接的所有串口名称 * @param args */@SuppressWarnings("unchecked")public static void main(String[] args) {// TODO Auto-generated method stubportList = CommPortIdentifier.getPortIdentifiers();while(portList.hasMoreElements()){portId = (CommPortIdentifier)portList.nextElement();if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){System.out.println(portId.getName());}}}}
结果:

0 1
原创粉丝点击