前置
1、jdk: 1.8
2、springboot:2.3.3.RELEASE
主要代码从网上copy的,打包一个32位的运行时,写个启动脚本,做到直接运行
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.sh.cares</groupId>
<artifactId>serialport</artifactId>
<version>1.0.RELEASE</version>
<name>serialport</name>
<description>串口读写</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.netbeans.external</groupId>
<artifactId>AbsoluteLayout</artifactId>
<version>RELEASE120-1</version>
</dependency>
<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtxcomm</artifactId>
<version>1.0</version>
<systemPath>${project.basedir}/lib/RXTXcomm.jar</systemPath>
<scope>system</scope>
</dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swing-layout</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.3.RELEASE</version>
<configuration>
<jvmArguments>
-Djava.library.path=${project.basedir}/lib/x64
</jvmArguments>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="dist">${project.build.directory}/${project.artifactId}</property>
<property name="app-name">${project.artifactId}-${project.version}</property>
<property name="jre">${dist}/jre</property>
<delete dir="${dist}" />
<mkdir dir="${dist}" />
<copy file="target/${app-name}.jar" tofile="${dist}/${app-name}.jar" />
<copy todir="${jre}" overwrite="true" >
<fileset dir="D:\soft\Java\JDK8_32\jre" erroronmissingdir="false"></fileset>
</copy>
<copy todir="${dist}/jre/bin/" overwrite="true" >
<fileset dir="${project.basedir}/lib/x86/" erroronmissingdir="false">
<include name="*.dll"></include>
</fileset>
</copy>
<copy file="${project.basedir}/cmd/start.cmd" tofile="${dist}/start.cmd" />
<zip basedir="${dist}" destfile="${project.build.directory}/${project.artifactId}.zip" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
工程结构
代码
start.cmd
echo off
set cwd=%~dp0
set path=%cwd%jre\bin;%path%
cd %cwd%
start javaw.exe -jar serialport-1.0.RELEASE.jar
SerialPortManager
package cn.sh.cares.serialport.manager;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import cn.sh.cares.serialport.utils.ArrayUtils;
import cn.sh.cares.serialport.utils.ShowUtils;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
/**
* 串口管理
*/
@SuppressWarnings("all")
public class SerialPortManager {
/**
* 查找所有可用端口
*
* @return 可用端口名称列表
*/
public static final ArrayList<String> findPorts() {
// 获得当前所有可用串口
Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
ArrayList<String> portNameList = new ArrayList<String>();
// 将可用串口名添加到List并返回该List
while (portList.hasMoreElements()) {
String portName = portList.nextElement().getName();
portNameList.add(portName);
}
return portNameList;
}
/**
* 打开串口
*
* @param portName 端口名称
* @param baudrate 波特率
* @return 串口对象
* @throws PortInUseException 串口已被占用
*/
public static final SerialPort openPort(String portName, int baudrate) throws PortInUseException {
try {
// 通过端口名识别端口
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
// 打开端口,并给端口名字和一个timeout(打开操作的超时时间)
CommPort commPort = portIdentifier.open(portName, 2000);
// 判断是不是串口
if (commPort instanceof SerialPort) {
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();
}
return serialPort;
}
} catch (NoSuchPortException e1) {
e1.printStackTrace();
}
return null;
}
/**
* 关闭串口
*
* @param serialport 待关闭的串口对象
*/
public static void closePort(SerialPort serialPort) {
if (serialPort != null) {
serialPort.close();
}
}
/**
* 往串口发送数据
*
* @param serialPort 串口对象
* @param order 待发送数据
*/
public static void sendToPort(SerialPort serialPort, byte[] order) {
OutputStream out = null;
try {
out = serialPort.getOutputStream();
out.write(order);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
out = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从串口读取数据
*
* @param serialPort 当前已建立连接的SerialPort对象
* @return 读取到的数据
*/
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();
}
}
return bytes;
}
/**
* 添加监听器
*
* @param port 串口对象
* @param listener 串口存在有效数据监听
*/
public static void addListener(SerialPort serialPort, DataAvailableListener listener) {
try {
// 给串口添加监听器
serialPort.addEventListener(new SerialPortListener(listener));
// 设置当有数据到达时唤醒监听接收线程
serialPort.notifyOnDataAvailable(true);
// 设置当通信中断时唤醒中断线程
serialPort.notifyOnBreakInterrupt(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
/**
* 串口监听
*/
public static class SerialPortListener implements SerialPortEventListener {
private DataAvailableListener mDataAvailableListener;
public SerialPortListener(DataAvailableListener mDataAvailableListener) {
this.mDataAvailableListener = mDataAvailableListener;
}
public void serialEvent(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 interface DataAvailableListener {
/**
* 串口存在有效数据
*/
void dataAvailable();
}
}
MainFrame
package cn.sh.cares.serialport.ui;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import cn.sh.cares.serialport.manager.SerialPortManager;
import cn.sh.cares.serialport.utils.ByteUtils;
import cn.sh.cares.serialport.utils.ShowUtils;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
/**
* 主界面
*
*/
@SuppressWarnings("all")
public class MainFrame extends JFrame {
// 程序界面宽度
public final int WIDTH = 530;
// 程序界面高度
public final int HEIGHT = 390;
// 数据显示区
private JTextArea mDataView = new JTextArea();
private JScrollPane mScrollDataView = new JScrollPane(mDataView);
// 串口设置面板
private JPanel mSerialPortPanel = new JPanel();
private JLabel mSerialPortLabel = new JLabel("串口");
private JLabel mBaudrateLabel = new JLabel("波特率");
private JComboBox mCommChoice = new JComboBox();
private JComboBox mBaudrateChoice = new JComboBox();
private ButtonGroup mDataChoice = new ButtonGroup();
private JRadioButton mDataASCIIChoice = new JRadioButton("ASCII", true);
private JRadioButton mDataHexChoice = new JRadioButton("Hex");
// 操作面板
private JPanel mOperatePanel = new JPanel();
private JTextArea mDataInput = new JTextArea();
private JButton mSerialPortOperate = new JButton("打开串口");
private JButton mSendData = new JButton("发送数据");
// 串口列表
private List<String> mCommList = null;
// 串口对象
private SerialPort mSerialport;
public MainFrame() {
initView();
initComponents();
actionListener();
initData();
}
/**
* 初始化窗口
*/
private void initView() {
// 关闭程序
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// 禁止窗口最大化
setResizable(false);
// 设置程序窗口居中显示
Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setBounds(p.x - WIDTH / 2, p.y - HEIGHT / 2, WIDTH, HEIGHT);
this.setLayout(null);
setTitle("串口通信");
}
/**
* 初始化控件
*/
private void initComponents() {
// 数据显示
mDataView.setFocusable(false);
mScrollDataView.setBounds(10, 10, 505, 200);
add(mScrollDataView);
// 串口设置
mSerialPortPanel.setBorder(BorderFactory.createTitledBorder("串口设置"));
mSerialPortPanel.setBounds(10, 220, 170, 130);
mSerialPortPanel.setLayout(null);
add(mSerialPortPanel);
mSerialPortLabel.setForeground(Color.gray);
mSerialPortLabel.setBounds(10, 25, 40, 20);
mSerialPortPanel.add(mSerialPortLabel);
mCommChoice.setFocusable(false);
mCommChoice.setBounds(60, 25, 100, 20);
mSerialPortPanel.add(mCommChoice);
mBaudrateLabel.setForeground(Color.gray);
mBaudrateLabel.setBounds(10, 60, 40, 20);
mSerialPortPanel.add(mBaudrateLabel);
mBaudrateChoice.setFocusable(false);
mBaudrateChoice.setBounds(60, 60, 100, 20);
mSerialPortPanel.add(mBaudrateChoice);
mDataASCIIChoice.setBounds(20, 95, 55, 20);
mDataHexChoice.setBounds(95, 95, 55, 20);
mDataChoice.add(mDataASCIIChoice);
mDataChoice.add(mDataHexChoice);
mSerialPortPanel.add(mDataASCIIChoice);
mSerialPortPanel.add(mDataHexChoice);
// 操作
mOperatePanel.setBorder(BorderFactory.createTitledBorder("操作"));
mOperatePanel.setBounds(200, 220, 315, 130);
mOperatePanel.setLayout(null);
add(mOperatePanel);
mDataInput.setBounds(25, 25, 265, 50);
mDataInput.setLineWrap(true);
mDataInput.setWrapStyleWord(true);
mOperatePanel.add(mDataInput);
mSerialPortOperate.setFocusable(false);
mSerialPortOperate.setBounds(45, 95, 90, 20);
mOperatePanel.add(mSerialPortOperate);
mSendData.setFocusable(false);
mSendData.setBounds(180, 95, 90, 20);
mOperatePanel.add(mSendData);
}
/**
* 初始化数据
*/
private void initData() {
mCommList = SerialPortManager.findPorts();
// 检查是否有可用串口,有则加入选项中
if (mCommList == null || mCommList.size() < 1) {
ShowUtils.warningMessage("没有搜索到有效串口!");
} else {
for (String s : mCommList) {
mCommChoice.addItem(s);
}
}
mBaudrateChoice.addItem("9600");
mBaudrateChoice.addItem("19200");
mBaudrateChoice.addItem("38400");
mBaudrateChoice.addItem("57600");
mBaudrateChoice.addItem("115200");
}
/**
* 按钮监听事件
*/
private void actionListener() {
// 串口
mCommChoice.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
mCommList = SerialPortManager.findPorts();
// 检查是否有可用串口,有则加入选项中
if (mCommList == null || mCommList.size() < 1) {
ShowUtils.warningMessage("没有搜索到有效串口!");
} else {
int index = mCommChoice.getSelectedIndex();
mCommChoice.removeAllItems();
for (String s : mCommList) {
mCommChoice.addItem(s);
}
mCommChoice.setSelectedIndex(index);
}
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
// NO OP
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
// NO OP
}
});
// 打开|关闭串口
mSerialPortOperate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("打开串口".equals(mSerialPortOperate.getText()) && mSerialport == null) {
openSerialPort(e);
} else {
closeSerialPort(e);
}
}
});
// 发送数据
mSendData.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendData(e);
}
});
}
/**
* 打开串口
*
* @param evt 点击事件
*/
private void openSerialPort(ActionEvent evt) {
// 获取串口名称
String commName = (String) mCommChoice.getSelectedItem();
// 获取波特率,默认为9600
int baudrate = 9600;
String bps = (String) mBaudrateChoice.getSelectedItem();
baudrate = Integer.parseInt(bps);
// 检查串口名称是否获取正确
if (commName == null || commName.equals("")) {
ShowUtils.warningMessage("没有搜索到有效串口!");
} else {
try {
mSerialport = SerialPortManager.openPort(commName, baudrate);
if (mSerialport != null) {
mDataView.setText("串口已打开" + "\r\n");
mSerialPortOperate.setText("关闭串口");
}
} catch (PortInUseException e) {
ShowUtils.warningMessage("串口已被占用!");
}
}
// 添加串口监听
SerialPortManager.addListener(mSerialport, new SerialPortManager.DataAvailableListener() {
@Override
public void dataAvailable() {
byte[] data = null;
try {
if (mSerialport == null) {
ShowUtils.errorMessage("串口对象为空,监听失败!");
} else {
// 读取串口数据
data = SerialPortManager.readFromPort(mSerialport);
// 以字符串的形式接收数据
if (mDataASCIIChoice.isSelected()) {
mDataView.append(new String(data) + "\r\n");
}
// 以十六进制的形式接收数据
if (mDataHexChoice.isSelected()) {
mDataView.append(ByteUtils.byteArrayToHexString(data) + "\r\n");
}
}
} catch (Exception e) {
ShowUtils.errorMessage(e.toString());
// 发生读取错误时显示错误信息后退出系统
System.exit(0);
}
}
});
}
/**
* 关闭串口
*
* @param evt 点击事件
*/
private void closeSerialPort(ActionEvent evt) {
SerialPortManager.closePort(mSerialport);
mDataView.setText("串口已关闭" + "\r\n");
mSerialPortOperate.setText("打开串口");
mSerialport = null;
}
/**
* 发送数据
*
* @param evt 点击事件
*/
private void sendData(ActionEvent evt) {
// 待发送数据
String data = mDataInput.getText().toString();
if (mSerialport == null) {
ShowUtils.warningMessage("请先打开串口!");
return;
}
if ("".equals(data) || data == null) {
ShowUtils.warningMessage("请输入要发送的数据!");
return;
}
// 以字符串的形式发送数据
if (mDataASCIIChoice.isSelected()) {
SerialPortManager.sendToPort(mSerialport, data.getBytes());
}
// 以十六进制的形式发送数据
if (mDataHexChoice.isSelected()) {
SerialPortManager.sendToPort(mSerialport, ByteUtils.hexStr2Byte(data));
}
}
public static void main(String args[]) {
}
}
ArrayUtils
package cn.sh.cares.serialport.utils;
/**
* 数组工具
*
*/
public class ArrayUtils {
/**
* 合并数组
*
* @param firstArray 第一个数组
* @param secondArray 第二个数组
* @return 合并后的数组
*/
public static byte[] concat(byte[] firstArray, byte[] secondArray) {
if (firstArray == null || secondArray == null) {
return null;
}
byte[] bytes = new byte[firstArray.length + secondArray.length];
System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
return bytes;
}
}
ByteUtils
package cn.sh.cares.serialport.utils;
import java.nio.ByteBuffer;
import java.util.Locale;
/**
* Byte转换工具
*
*/
public class ByteUtils {
/**
* 十六进制字符串转byte[]
*
* @param hex 十六进制字符串
* @return byte[]
*/
public static byte[] hexStr2Byte(String hex) {
if (hex == null) {
return new byte[]{};
}
// 奇数位补0
if (hex.length() % 2 != 0) {
hex = "0" + hex;
}
int length = hex.length();
ByteBuffer buffer = ByteBuffer.allocate(length / 2);
for (int i = 0; i < length; i++) {
String hexStr = hex.charAt(i) + "";
i++;
hexStr += hex.charAt(i);
byte b = (byte) Integer.parseInt(hexStr, 16);
buffer.put(b);
}
return buffer.array();
}
/**
* byte[]转十六进制字符串
*
* @param array byte[]
* @return 十六进制字符串
*/
public static String byteArrayToHexString(byte[] array) {
if (array == null) {
return "";
}
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length; i++) {
buffer.append(byteToHex(array[i]));
}
return buffer.toString();
}
/**
* byte转十六进制字符
*
* @param b byte
* @return 十六进制字符
*/
public static String byteToHex(byte b) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
return hex.toUpperCase(Locale.getDefault());
}
}
ShowUtils
package cn.sh.cares.serialport.utils;
import javax.swing.JOptionPane;
/**
* 提示框
*/
public class ShowUtils {
/**
* 消息提示
*
* @param message 消息内容
*/
public static void message(String message) {
JOptionPane.showMessageDialog(null, message);
}
/**
* 警告消息提示
*
* @param message 消息内容
*/
public static void warningMessage(String message) {
JOptionPane.showMessageDialog(null, message, "警告",
JOptionPane.WARNING_MESSAGE);
}
/**
* 错误消息提示
*
* @param message 消息内容
*/
public static void errorMessage(String message) {
JOptionPane.showMessageDialog(null, message, "错误",
JOptionPane.ERROR_MESSAGE);
}
/**
* 自定义的消息提示
*
* @param title 标题
* @param message 消息内容
*/
public static void plainMessage(String title, String message) {
JOptionPane.showMessageDialog(null, message, title,
JOptionPane.PLAIN_MESSAGE);
}
/**
* 带有选择功能的提示
*
* @param title 标题
* @param message 消息内容
* @return 是/否 0/1
*/
public static boolean selectMessage(String title, String message) {
int isConfirm = JOptionPane.showConfirmDialog(null, message, title,
JOptionPane.YES_NO_OPTION);
if (0 == isConfirm) {
return true;
}
return false;
}
}
SerialportApplication
package cn.sh.cares.serialport;
import cn.sh.cares.serialport.ui.MainFrame;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class SerialportApplication {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(SerialportApplication.class);
builder.headless(false).web(WebApplicationType.NONE).run(args);
java.awt.EventQueue.invokeLater(() -> new MainFrame().setVisible(true));
}
}
运行
代码库
https://github.com/cjwangd/serialport
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!