java RXTXcomm 串口通信

2024-09-07 04:58
文章标签 java 通信 串口 rxtxcomm

本文主要是介绍java RXTXcomm 串口通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

RXTXcomm:提供了 Windows x64, x86, ia64 and Linux x86, x86_64等操作系统支持。
下载地址 http://fizzed.com/oss/rxtx-for-java
使用RXTXcomm首先要安排JRE环境,开发IED可能eclipse.
1.下载系统相应的RXTXcomm。
2.将rxtxSerial.dll、rxtxParallel.dll复制到\jre\bin目录下。
将RXTXcomm.jar复制到\jre\lib\ext目录下。
可能还需要把rxtxParallel.dll、rxtxSerial.dll拷贝到:C:\WINDOWS\system32下。

API概述

接口

CommDriver 可负载设备(the loadable device)驱动程序接口的一部分
CommPortOwnershipListener 传递各种通讯端口的所有权事件
ParallelPortEventListener 传递并行端口事件
SerialPortEventListener 传递串行端口事件

CommPort 通讯端口 CommDriver 可负载设备(the loadable device)驱动程序接口的一部分
CommPortOwnershipListener 传递各种通讯端口的所有权事件
ParallelPortEventListener 传递并行端口事件
SerialPortEventListener 传递串行端口事件
SerialPortEvent 异常类
NoSuchPortException 当驱动程序不能找到指定端口时抛出
PortInUseException 当碰到指定端口正在使用中时抛出
UnsupportedCommOperationException 驱动程序不允许指定操作时抛出

重要类详述

CommPort类

描述被底层系统支持的端口的抽象类。包含一些高层的IO控制方法,这些方法对于所有不同的通讯端口来说是通用的。SerialPort(串口) 和ParallelPort(并口)都是它的子类。
CommPortIdentifier
主要用于对串口进行管理和设置,是对串口进行访问控制的核心类。主要方法如下:
[java] view plain copy
addPortName(String, int, CommDriver) 添加端口名到端口列表里

addPortOwnershipListener(CommPortOwnershipListener) 添加端口拥有的监听器

removePortOwnershipListener(CommPortOwnershipListener) 移除端口拥有的监听器

getCurrentOwner() 得到当前占有端口的对象或应用程序

getName() 得到端口名称

getPortIdentifier(CommPort) 得到参数打开的端口的CommPortIdentifier类型对象

getPortIdentifier(String) 得到以参数命名的端口的CommPortIdentifier类型对象

getPortIdentifiers() 得到系统中的端口列表

getPortType() 得到端口的类型

isCurrentlyOwned() 判断当前端口是否被占用

open(FileDescriptor) 用文件描述的类型打开端口

open(String, int) 打开端口,两个参数:程序名称,延迟时间(毫秒数)

SerialPort 串口参数的函数

getBaudRate() 得到波特率

getParity() 得到检验类型

getDataBits() 得到数据位数

getStopBits() 得到停止位数

setSerialPortParams(int, int, int, int) 设置串口参数依次为(波特率,数据位,停止位,奇偶检验)

close() 关闭串口

getOutputStream() 得到OutputStream类型的输出流

getInputStream() 得到InputStream类型的输入流

事件及事件方法

isCD() 是否有载波

isCTS() 是否清除发送

isDSR() 数据是否准备就绪

isDTR() 数据终端是否准备就绪

isRI() 是否响铃侦测

isRTS() 是否要求发送

addEventListener(SerialPortEventListener) 向SerialPort对象中添加串口事件监听器

removeEventListener() 移除SerialPort对象中的串口事件监听器

getEventType() 得到发生的事件类型返回值为int型

sendBreak(int) 设置中断过程的时间,参数为毫秒值

setRTS(boolean) 设置或清除RTS位

setDTR(boolean) 设置或清除DTR位

notifyOnBreakInterrupt(boolean) 设置中断事件

notifyOnCarrierDetect(boolean) 设置载波监听事件

notifyOnCTS(boolean) 设置清除发送事件

notifyOnDataAvailable(boolean) 设置串口有数据的事件

notifyOnDSR(boolean) 设置数据备妥事件

notifyOnFramingError(boolean) 设置发生错误事件

notifyOnOutputEmpty(boolean) 设置发送缓冲区为空事件

notifyOnParityError(boolean) 设置发生奇偶检验错误事件

notifyOnRingIndicator(boolean) 设置响铃侦测事件

串口参数的静态成员变量

成员变量 说明 成员变量 说明 成员变量 说明
DATABITS_5 数据位为5 STOPBITS_2 停止位为2 PARITY_ODD 奇检验

DATABITS_6 数据位为6 STOPBITS_1 停止位为1 PARITY_MARK 标记检验

DATABITS_7 数据位为7 STOPBITS_1_5 停止为1.5 PARITY_NONE 空格检验

DATABITS_8 数据位为8 PARITY_EVEN 偶检验 PARITY_SPACE 无检验

代码实例:

package javaCOM;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;
import java.util.Enumeration;
import java.util.TooManyListenersException;import com.sun.org.apache.bcel.internal.generic.NEW;import gnu.io.CommDriver;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import javafx.scene.chart.PieChart.Data;//import com.apple.dnssd.*;
//import com.sun.org.apache.bcel.internal.generic.I2C;
//import com.sun.org.apache.bcel.internal.generic.NEW;/*** Created by gbr on 13-12-11.*/
public class ReadCom implements SerialPortEventListener {//枚举类型Enumeration<CommPortIdentifier> portList;// 检测系统可用端口private CommPortIdentifier portIdentifier;// 端口private SerialPort port;// 输入/输出流private InputStream inputStream;private OutputStream outputStream;public void getPortList() {// 获得系统支持的所有端口(串口,并口)portList = CommPortIdentifier.getPortIdentifiers();while(portList.hasMoreElements()) {portIdentifier = (CommPortIdentifier)portList.nextElement();// CommPortIdentifier.PORT_SERIAL :串口// CommPortIdentifier.PORT_PARALLEL :并口// CommPortIdentifier.PORT_RS485 :RS485// CommPortIdentifier.PORT_I2C :I2C// CommPortIdentifier.PORT_RAW if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {System.out.println(portIdentifier.getName());if (portIdentifier.getName().equals("COM3")) {try {// open:打开串口,第一个参数应用程序名称 字符串可随意填写,第二个参数阻塞时等待多少毫秒port = (SerialPort)portIdentifier.open("ReadCom888", 2000);// 串口设置监听port.addEventListener(this);// 设置可监听port.notifyOnDataAvailable(true);// 设置串口通信参数// 波特率,数据位,停止位,校验方式port.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);outputStream = port.getOutputStream();inputStream = port.getInputStream();} catch (PortInUseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TooManyListenersException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnsupportedCommOperationException e) { // TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}@Overridepublic void serialEvent(SerialPortEvent event) {// TODO Auto-generated method stub// TODO Auto-generated method stub/* *  SerialPortEvent.BI:/*Break interrupt,通讯中断 *  SerialPortEvent.OE:/*Overrun error,溢位错误 *  SerialPortEvent.FE:/*Framing error,传帧错误 *  SerialPortEvent.PE:/*Parity error,校验错误 *  SerialPortEvent.CD:/*Carrier detect,载波检测  *  SerialPortEvent.CTS:/*Clear to send,清除发送  *  SerialPortEvent.DSR:/*Data set ready,数据设备就绪 *  SerialPortEvent.RI:/*Ring indicator,响铃指示 *  SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空 *  SerialPortEvent.DATA_AVAILABLE:*/  switch (event.getEventType()) {case SerialPortEvent.BI:case SerialPortEvent.OE:case SerialPortEvent.FE:case SerialPortEvent.PE:case SerialPortEvent.CD:case SerialPortEvent.CTS:case SerialPortEvent.DSR:case SerialPortEvent.RI:case SerialPortEvent.OUTPUT_BUFFER_EMPTY:break;case SerialPortEvent.DATA_AVAILABLE: // readData();break;default:break;}   }public void readData() {byte[] rbuff = new byte[1024];int hasRead = 0;try {while((hasRead=inputStream.read(rbuff)) > 0) {System.out.print(new String(rbuff, 0, hasRead));break;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void sendData(String data) {try {outputStream.write(data.getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {new ReadCom().getPortList();}}

运行错误:

gnu.io.PortInUseException: Unknown Application:串口端口被占用
一般关掉占用端口的应用即可。

这篇关于java RXTXcomm 串口通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144125

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件