java 串口 rxtx_【Java】基于RXTX的Java串口通信

本文详细介绍了如何使用Java的RXTX库进行串口通信,包括查找可用串口、打开串口、设置波特率、发送和接收数据、关闭串口以及添加数据监听器等功能。示例代码展示了完整的串口操作流程。
摘要由CSDN通过智能技术生成

public classSerialPortManager {/*** 查找可用端口

*

*@return可用端口名称列表*/

public static final ListfindPorts() {//获得当前所有可用串口

Enumeration portList =CommPortIdentifier.getPortIdentifiers();

List ports = new ArrayList<>();while(portList.hasMoreElements()) {

String portName=portList.nextElement().getName();

ports.add(portName);

}returnports;

}/*** 打开串口

*

*@paramportName 端口名称

*@parambaudrate 波特率

*@return串口对象

*@throwsPortInUseException 端口被占用*/

public static SerialPort openPort(String portName, int baudrate) throwsPortInUseException {try{//通过端口名识别端口

CommPortIdentifier portIdentifier =CommPortIdentifier.getPortIdentifier(portName);//打开端口,并给端口名字和一个Timeout

CommPort commPort = portIdentifier.open(portName, 2000);//判断是不是串口

if (commPort instanceofSerialPort) {

SerialPort serialPort=(SerialPort) commPort;try{//设置一下串口的波特率等参数//数据位:8//停止位:1//校验位:None

serialPort.setSerialPortParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE);

}catch(UnsupportedCommOperationException e) {

e.printStackTrace();

}returnserialPort;

}

}catch(NoSuchPortException e) {

e.printStackTrace();

}return null;

}/*** 关闭串口*/

public static voidclosePort(SerialPort serialPort) {if (serialPort != null) {

serialPort.close();

}

}/*** 发送数据

*

*@paramserialPort 串口对象

*@paramorder 待发送数据*/

public static void sendMessageToPort(SerialPort serialPort, byte[] order) {

OutputStream out= null;try{

out=serialPort.getOutputStream();

out.write(order);

out.flush();

}catch(IOException e) {

e.printStackTrace();

}finally{if (out != null) {try{

out.close();

out= null;

}catch(IOException e) {

e.printStackTrace();

}

}

}

}/*** 读取数据*/

public static byte[] readFromPort(SerialPort serialPort) {

InputStream in= null;byte[] bytes ={};try{

in=serialPort.getInputStream();//缓冲区大小为一个字节

byte[] readBuffer = new byte[1];int bytesNum =in.read(readBuffer);while (bytesNum > 0) {

bytes=ArrayUtils.concat(bytes, readBuffer);

bytesNum=in.read(readBuffer);

}

}catch(IOException e) {

e.printStackTrace();

}finally{try{if (in != null) {

in.close();

in= null;

}

}catch(IOException e) {

e.printStackTrace();

}

}returnbytes;

}/*** 添加监听器

*

*@paramserialPort 串口对象

*@paramlistener 串口存在有效数据监听*/

public static voidaddListener(SerialPort serialPort, DataAvailableListener listener) {try{//给串口添加监听器

serialPort.addEventListener(newSerialPortListener(listener));//设置当有数据到达时唤醒监听接收线程

serialPort.notifyOnDataAvailable(true);//设置当通信中断时唤醒中断线程

serialPort.notifyOnBreakInterrupt(true);

}catch(TooManyListenersException e) {

e.printStackTrace();

}

}/*** 串口监听*/

public static class SerialPortListener implementsSerialPortEventListener {privateDataAvailableListener mDataAvailableListener;publicSerialPortListener(DataAvailableListener mDataAvailableListener) {this.mDataAvailableListener =mDataAvailableListener;

}public voidserialEvent(SerialPortEvent serialPortEvent) {switch(serialPortEvent.getEventType()) {case SerialPortEvent.DATA_AVAILABLE: //1.串口存在有效数据

if (mDataAvailableListener != null) {

mDataAvailableListener.dataAvailable();

}break;case SerialPortEvent.OUTPUT_BUFFER_EMPTY: //2.输出缓冲区已清空

break;case SerialPortEvent.CTS: //3.清除待发送数据

break;case SerialPortEvent.DSR: //4.待发送数据准备好了

break;case SerialPortEvent.RI: //5.振铃指示

break;case SerialPortEvent.CD: //6.载波检测

break;case SerialPortEvent.OE: //7.溢位(溢出)错误

break;case SerialPortEvent.PE: //8.奇偶校验错误

break;case SerialPortEvent.FE: //9.帧错误

break;case SerialPortEvent.BI: //10.通讯中断

ShowUtils.errorMessage("与串口设备通讯中断");break;default:break;

}

}

}/*** 串口存在有效数据监听*/

public interfaceDataAvailableListener {/*** 串口存在有效数据*/

voiddataAvailable();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值