当前位置: 首页 > news >正文

【JAVA】读取windows的串口信息

  1. 首先去https://repo1.maven.org/maven2/下载rxtx的jar包,比如下载rxtx-2.1.7.jar放在项目的lib目录中
  2. 去这里https://files-cdn.cnblogs.com/files/blogs/666773/rxtx-win-x64.rar下载RXTXcomm.jar(放在项目的lib中)、rxtxParallel.dll和rxtxSerial.dll(放在项目的根目录中)
  3. 如下代码就是获取指定串口的数据
package sdf;import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;import java.io.IOException;
import java.io.InputStream;public class SerialPortReader implements SerialPortEventListener {private SerialPort serialPort;private InputStream inputStream;public static void main(String[] args) {SerialPortReader reader = new SerialPortReader();reader.connect("COM1");  // 修改为实际的串口名称reader.readData();}public void connect(String portName) {try {CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);if (portIdentifier.isCurrentlyOwned()) {System.out.println("Port is currently in use.");} else {//通过portIdentifier对象打开物理串口,"SerialPortReader"是给连接起的名称标识,2000表示打开端口的超时时间(单位:毫秒)serialPort = (SerialPort) portIdentifier.open("SerialPortReader", 2000);serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);inputStream = serialPort.getInputStream();serialPort.addEventListener(this);serialPort.notifyOnDataAvailable(true);}} catch (Exception e) {e.printStackTrace();}}public void readData() {try {while (true) {// 这里可以添加一些逻辑来处理读取到的数据Thread.sleep(1000);}} catch (InterruptedException e) {e.printStackTrace();}}@Overridepublic void serialEvent(SerialPortEvent event) {if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {try {byte[] buffer = new byte[inputStream.available()];inputStream.read(buffer);String data = new String(buffer);System.out.println("Received data: " + data);} catch (IOException e) {e.printStackTrace();}}}
}
  1. 获取所有端口代码
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.util.Enumeration;public class SerialPortReader {public static void main(String[] args) {// 列出所有可用串口Enumeration portList = CommPortIdentifier.getPortIdentifiers();while (portList.hasMoreElements()) {CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {System.out.println(portId.getName());}}// 打开指定串口String portName = "COM1";  // 修改为实际的串口名称try {CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);if (portIdentifier.isCurrentlyOwned()) {System.out.println("Port is currently in use.");} else {// 打开串口,参数分别为应用程序名称、超时时间SerialPort serialPort = (SerialPort) portIdentifier.open("SerialPortReader", 2000);// 设置串口参数,波特率、数据位、停止位、奇偶校验serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);}} catch (Exception e) {e.printStackTrace();}}
}
http://www.xdnf.cn/news/103789.html

相关文章:

  • SqlSugar与Entity Framework (EF)的SWOT分析
  • Inxpect 新推高性价比版毫米波安全雷达:以经济实用护航工业安全
  • 游戏开发核心技术解析——从引擎架构到攻防体系的完整技能树
  • 阿里云 AI 搜索开放平台:RAG智能化工作流助力 AI 搜索
  • 【C语言】C语言中的字符函数和字符串函数全解析
  • Pingora vs. Nginx vs. 其他主流代理服务器性能对比
  • 2024从Maven-MySQL-Nginx部署
  • LeetCode热题100--283.移动零--简单
  • Linux中进程的属性:状态
  • 3.4 Agent的生命周期管理:任务分解、状态管理与反馈机制
  • leetcode-排序
  • 迅为RK3562开发板ARM四核A53核心板多种系统适配全开源
  • C++学习-入门到精通-【0】计算机和C++简介
  • C++学习:六个月从基础到就业——C++学习之旅:STL迭代器系统
  • 网站架构演进之路:从单体到垂直,再到缓存优化
  • Python爬虫(2)Python爬虫入门:从HTTP协议解析到豆瓣电影数据抓取实战
  • day31 学习笔记
  • DB-Day11笔记-函数和存储过程面试题讲解
  • 操作系统 : Linux库制作与原理
  • PNG透明免抠设计素材大全26000+
  • DCAN,ECAN和MCAN的区别
  • Vue3 + TypeScript 实现二维码生成与展示
  • 开源AI客户端Cherry Studio本地化部署自建大模型服务在线平台
  • HTML+CSS对角背景变色
  • es-存储与搜索优化
  • 网络编程——通信三要素
  • 文档构建:Sphinx全面使用指南 — 强化篇
  • 常见基础电能
  • docker部署seafile修改默认端口并安装配置onlyoffice实现在线编辑
  • Shader CGInculde(六)