JAVA学习笔记20210423_串口
2021/4/23 12:26:49
本文主要是介绍JAVA学习笔记20210423_串口,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
获取本地串口
尝试了两种jar包:comm和RXTXcomm
由于comm只能适配32位虚拟机,运行会报错且找不到串口:
D:\PractiseJava\SerialPort>java -classpath comm.jar; ListPorts Error loading win32com: java.lang.UnsatisfiedLinkError: C:\Program Files\Java\jdk-15.0.2\bin\win32com.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform No serial port found
因此使用RXTXcomm包:RXTX下载
- 拷贝 rxtxSerial.dll 到 JAVA_HOME\jre\bin目录中
- 拷贝 rxtxParallel.dll 到 JAVA_HOME\jre\bin目录中
- 拷贝 RXTXcomm.jar 到工作目录中
/** * this is a serial port opration class * @author XYM_ * @date 2021-4-23 * @version1.0 */ import java.util.*; import gnu.io.*; public class ListPortsRXTX{ public static void main(String[] args){ CommPortIdentifier portId = null; Enumeration<?> allPorts = CommPortIdentifier.getPortIdentifiers(); while(allPorts.hasMoreElements() == true){ portId = (CommPortIdentifier)allPorts.nextElement(); if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){ System.out.println("Serial Port: " + portId.getName()); } } } }
运行结果:
D:\PractiseJava\SerialPort>java -classpath RXTXcomm.jar; ListPortsRXTX Serial Port: COM1 Serial Port: COM2
设置参数
/** * this is a serial port opration class * @author XYM_ * @date 2021-4-23 * @version1.0 */ import java.util.*; import gnu.io.*; public class OpenAndClose{ public static void main(String[] args){ // search and list all ports CommPortIdentifier portId = null; Enumeration<?> allPorts = CommPortIdentifier.getPortIdentifiers(); while(allPorts.hasMoreElements() == true){ portId = (CommPortIdentifier)allPorts.nextElement(); if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){ System.out.println("Serial Port: " + portId.getName()); } } // get the ports CommPortIdentifier com1 = null; CommPortIdentifier com2 = null; try{ com1 = CommPortIdentifier.getPortIdentifier("COM1"); com2 = CommPortIdentifier.getPortIdentifier("COM2"); }catch(NoSuchPortException e){ e.printStackTrace(); } // open the ports SerialPort serialCom1 = null; SerialPort serialCom2 = null; try{ //firstparam: the owner of the port opened //secondparam: the time (ms) to wait when the port is in use serialCom1 = (SerialPort)com1.open("OpenAndClose",1000); serialCom2 = (SerialPort)com2.open("OpenAndClose",1000); }catch(PortInUseException e){ e.printStackTrace(); } // set the param of the ports try{ serialCom1.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE ); serialCom2.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE ); }catch(UnsupportedCommOperationException e){ e.printStackTrace(); } // close the ports serialCom1.close(); serialCom2.close(); } }
运行后可以在设备管理器中看到端口设置已被改写:
数据读写
ref:
串口数据的发送与监听读取
Java实现串口数据读写
使用Java实现串口通信
这篇关于JAVA学习笔记20210423_串口的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?