java 串口数据读取_java读取串口数据

该博客详细介绍了如何在Java中实现串口数据的读取,包括打开串口、设置波特率、数据位、停止位和校验位,以及监听和处理串口事件。通过创建`ComRunThr`类并实现`SerialPortEventListener`接口,实现了串口数据的实时监听和处理。当数据可用时,从输入流中读取并进行相应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package compackage;

import java.io.InputStream;

import java.util.Enumeration;

import javax.comm.CommPortIdentifier;

import javax.comm.SerialPort;

import javax.comm.SerialPortEvent;

import javax.comm.SerialPortEventListener;

import systemfunction.*;

public class ComRunThr implements Runnable,SerialPortEventListener{

private Thread m_thisThread;

public SerialPort m_SerialPort=null;

public ComRunThr(){

}

//串口,波特率,数据位,停止位,校验位

public void openPort(String portnum,int baudrate,int databits,int stopbits,int parity)throws Exception{

try{

//防止重复打开

if(ComArg.SerialMap.get(portnum)!=null){

ComRunThr thiscls=(ComRunThr)ComArg.SerialMap.get(portnum);

thiscls.closePort();

}

String webname=MemoryClass.getIni(SysTag.Sys_WebName,"");//web.xml的display-name

if(!MemoryClass.getIni("RunUnderDebug","").equals(""))System.out.println("准备打开串口:"+portnum);//测试输出

Enumeration portList=CommPortIdentifier.getPortIdentifiers();//获得一个枚举对象,即端口对象

while(portList.hasMoreElements()){

CommPortIdentifier portId=(CommPortIdentifier)portList.nextElement();

if(!MemoryClass.getIni("RunUnderDebug","").equals(""))System.out.println("端口扫描:"+portId.getName()+"-"+portId.getPortType());//测试输出

if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){//返回一个端口类型

if(!portId.getName().equals(portnum))continue;//找到端口对象

try{

m_SerialPort=(SerialPort)portId.open(webname,2000);//打开一个端口,程序名、阻塞时等待时间(毫秒)

/* open方法打开通讯端口,获得一个CommPort对象。它使程序独占端口。如果端口正被其他应用程序占用,将使用 CommPortOwnershipListener事件机制,传递一个PORT_OWNERSHIP_REQUESTED事件。每个端口都关联一个 InputStream 何一个OutputStream。如果端口是用open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非有close 被调用。有两个参数,第一个为应用程序名;第二个参数是在端口打开时阻塞等待的毫秒数。*/

m_SerialPort.addEventListener(this);//开启一个监听事件

m_SerialPort.notifyOnDataAvailable(true);//允许事件输入

//设置端口初始化数据,波特率、数据位、停止位、是否校检

m_SerialPort.setSerialPortParams(baudrate,databits,stopbits,parity);

if(!MemoryClass.getIni("RunUnderDebug","").equals(""))System.out.println("打开串口:"+portnum+":"+baudrate+":"+databits+":"+stopbits+":"+parity);//测试输出

}catch(Exception e){

System.out.println("打开串口失败:"+portnum+" "+e.toString());

e.printStackTrace();

}

}

}

if(m_SerialPort!=null){

m_thisThread=new Thread(this);

m_thisThread.start();

ComArg.SerialMap.put(portnum,this);

}

}catch(Exception e){e.printStackTrace();throw e;}

}

public void closePort(){

//if(m_SerialPort!=null)

this.m_SerialPort.close();

m_thisThread.stop();

ComArg.SerialMap.remove(m_SerialPort.getName());

}

public void serialEvent(SerialPortEvent event) {

switch(event.getEventType()) {

case SerialPortEvent.BI:/*Break interrupt,通讯中断*/

case SerialPortEvent.OE:/*Overrun error,溢位错误*/

case SerialPortEvent.FE:/*Framing error,传帧错误*/

case SerialPortEvent.PE:/*Parity error,校验错误*/

case SerialPortEvent.CD:/*Carrier detect,载波检测*/

case SerialPortEvent.CTS:/*Clear to send,清除发送*/

case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/

case SerialPortEvent.RI:/*Ring indicator,响铃指示*/

case SerialPortEvent.OUTPUT_BUFFER_EMPTY: /*Output buffer is empty,输出缓冲区清空*/

break;

case SerialPortEvent.DATA_AVAILABLE:/*Data available at the serial port,端口有可用数据。读到缓冲数组,输出到终端*/

try{

InputStream is=m_SerialPort.getInputStream();//获取端口传输的数据流

while (is.available() > 0) {

Integer ii=new Integer(is.read());

byte bt=ii.byteValue();

ComArg.SerialBuff.put(bt);

//if(!MemoryClass.getIni("RunUnderDebug","").equals("")){

//System.out.println(Integer.toHexString(bt).toUpperCase()+":"+ComArg.SerialBuff.position());//测试输出

//}

byte btr=0,btn=0;

if(ComArg.SerialBuff.position()>2){

btr=ComArg.SerialBuff.get(ComArg.SerialBuff.position()-2);

btn=ComArg.SerialBuff.get(ComArg.SerialBuff.position()-1);

}

if(btr==0xD && btn==0xA){

byte[] dst=new byte[ComArg.SerialBuff.position()];

for(int i=0;i

dst[i]=ComArg.SerialBuff.get(i);

}

//重新设置等待下次接收

ComArg.SerialBuff.clear();

ComDataProcThr datathr=new ComDataProcThr(dst);

datathr.start();

}

}

} catch (Exception e) {e.printStackTrace();}

break;

}

}

public void run() {

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值