java监听串口,我看了下3个实现方式
JavaComm;RXTX; PureJavaComm
发现JavaComm太陈旧;PureJavaComm运行都困难。只有RXTX支持的比较好。
使用RXTX需要下载对应的32位或64位DLL文件。下载地址 http://rxtx.qbang.org/wiki/index.php/Download
RXTXcomm.jar 拷贝到 %JAVA_HOME%\jre\lib\ext
rxtxSerial.dll ;rxtxParallel 拷贝到 in %JAVA_HOME%\jre\bin 同时拷贝到 C:\Windows\System32 目录
pom文件内容如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xx</groupId>
<artifactId>ShowData</artifactId>
<version>1.0.0</version>
<name>ShowDataFromSerial</name>
<description>获取串口数据展示到swing上面</description>
<inceptionYear>2011-2018</inceptionYear>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.purejavacomm</groupId>
<artifactId>purejavacomm</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
监听程序代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.sunit.window.MainWindows2;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
public class SerialComm4MainWindows {
public SerialComm4MainWindows() {
super();
}
public void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
(new Thread(new SerialReader(in))).start();
} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
/** 读串口数据*/
public static class SerialReader implements Runnable {
InputStream in;
public SerialReader(InputStream in) {
this.in = in;
}
public void run() {
byte[] buffer = new byte[1024];
int len = -1;
try {
while ((len = this.in.read(buffer)) > -1) {
String getValue = new String(buffer, 0, len);
if(getValue != null && getValue.length() > 2) {
MainWindows2.parseValue(getValue); //获取值往另一个地方丢
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/** */
public static class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
try {
int c = 0;
while ((c = System.in.read()) > -1) {
this.out.write(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
(new SerialComm4MainWindows()).connect("COM5");
} catch (Exception e) {
e.printStackTrace();
}
}
}