java实现串口通讯

时间:2023-11-11 13:04:50

此文章是基于   搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

一. 准备工作

  1. 将RXTXcomm.jar放到  %JAVA_HOME%\jre\lib\ext\  目录下

  2. 将rxtxSerial.dll放到  %JAVA_HOME%\jre\bin\  目录下

  

二. 相关程序介绍

  1. CommPort.java,串口类

package com.ims.interfaces.commPort;

public class CommPort {
/**
* 串口名称,如:COM1、COM2
*/
private String commPortName=""; /**
* 串口所有者名称,一般为应用程序的名称
*/
private String ownerName; /**
* 波特率
*/
private String baudRate=""; /**
* 数据位
*/
private String dataBit=""; /**
* 校验位
*/
private String partityBit=""; /**
* 停止位
*/
private String stopBit=""; public String getCommPortName() {
return commPortName;
} public void setCommPortName(String commPortName) {
this.commPortName = commPortName;
} public String getOwnerName() {
return ownerName;
} public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
} public String getBaudRate() {
return baudRate;
} public void setBaudRate(String baudRate) {
this.baudRate = baudRate;
} public String getDataBit() {
return dataBit;
} public void setDataBit(String dataBit) {
this.dataBit = dataBit;
} public String getPartityBit() {
return partityBit;
} public void setPartityBit(String partityBit) {
this.partityBit = partityBit;
} public String getStopBit() {
return stopBit;
} public void setStopBit(String stopBit) {
this.stopBit = stopBit;
} }

  2. CommPortManager.java,串口管理员类

package com.ims.interfaces.commPort;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Enumeration; public class CommPortManager {
/**
* 串口定义
*/
private CommPort commPort; /**
* 串口对象
*/
private SerialPort serialPort; /**
* 字节输入
*/
private InputStream in; /**
* 字节输出
*/
private OutputStream out; /**
* 字符输入
*/
private BufferedReader bufReader; /**
* 字符输出
*/
private BufferedWriter bufWriter; /**
* 数据输入
*/
private DataInputStream dataIn; /**
* 数据输出
*/
private DataOutputStream dataOut; /**
* 串口是否在使用
*/
private boolean isUse; public CommPortManager(CommPort commPort){
this.commPort = commPort;
} /**
* 打开串口
* @throws Exception
*/
public void open() throws Exception{
CommPortIdentifier commPortId = CommPortIdentifier.getPortIdentifier(commPort.getCommPortName()); // 第一个参数:通常设置为你的应用程序的名字;第二个参数:开启端口超时的毫秒数
serialPort = (SerialPort)commPortId.open(commPort.getOwnerName(), 5000);
serialPort.setOutputBufferSize(2048); in = serialPort.getInputStream();
out = serialPort.getOutputStream(); bufReader = new BufferedReader(new InputStreamReader(in, "Unicode"));
bufWriter = new BufferedWriter(new OutputStreamWriter(out)); dataIn = new DataInputStream(in);
dataOut = new DataOutputStream(out); // 设置串口参数
serialPort.setSerialPortParams(Integer.valueOf(commPort.getBaudRate()), Integer.valueOf(commPort.getDataBit()),
Integer.valueOf(commPort.getStopBit()), Integer.valueOf(commPort.getPartityBit()));
} /**
* 判断串口是否可用
*/
public boolean commPortEnable(){
boolean result = false;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
while (en.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL
&& portId.getName().equalsIgnoreCase(commPort.getCommPortName())) {
result = true;
}
}
return result;
} /**
* 设置串口接收超时时间
* @param timeout 超时时间,单位秒
* @throws Exception
*/
public void setReceiveTimeout(int timeout) throws Exception{
getSerialPort().enableReceiveTimeout(timeout*1000);
} public CommPort getCommPort() {
return commPort;
} public SerialPort getSerialPort() {
return serialPort;
} public InputStream getIn() {
return in;
} public OutputStream getOut() {
return out;
} public BufferedReader getBufReader() {
return bufReader;
} public BufferedWriter getBufWriter() {
return bufWriter;
} public DataInputStream getDataIn() {
return dataIn;
} public DataOutputStream getDataOut() {
return dataOut;
} public boolean isUse() {
return isUse;
} public void setUse(boolean isUse) {
this.isUse = isUse;
}
}