引maven
<dependency>
<groupId>com.github.purejavacomm</groupId>
<artifactId>purejavacomm</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
工具类
package com.fx.client.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.TooManyListenersException;
import purejavacomm.CommPort;
import purejavacomm.CommPortIdentifier;
import purejavacomm.NoSuchPortException;
import purejavacomm.PortInUseException;
import purejavacomm.SerialPort;
import purejavacomm.SerialPortEventListener;
import purejavacomm.UnsupportedCommOperationException;
public class SerialPortUtil {
@SuppressWarnings("unchecked")
public static List<String> getSerialPortList() {
List<String> systemPorts = new ArrayList<>();
Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
String portName = portList.nextElement().getName();
systemPorts.add(portName);
}
return systemPorts;
}
public static SerialPort openSerialPort(String serialPortName)
throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
SerialPortParameter parameter = new SerialPortParameter(serialPortName);
return openSerialPort(parameter);
}
public static SerialPort openSerialPort(String serialPortName, int baudRate)
throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
SerialPortParameter parameter = new SerialPortParameter(serialPortName, baudRate);
return openSerialPort(parameter);
}
public static SerialPort openSerialPort(String serialPortName, int baudRate, int timeout)
throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
SerialPortParameter parameter = new SerialPortParameter(serialPortName, baudRate);
return openSerialPort(parameter, timeout);
}
public static SerialPort openSerialPort(SerialPortParameter parameter)
throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
return openSerialPort(parameter, 2000);
}
public static SerialPort openSerialPort(SerialPortParameter parameter, int timeout)
throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(parameter.getSerialPortName());
CommPort commPort = portIdentifier.open(parameter.getSerialPortName(), timeout);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(parameter.getBaudRate(), parameter.getDataBits(), parameter.getStopBits(), parameter.getParity());
System.out.println("开启串口成功,串口名称:" + parameter.getSerialPortName());
return serialPort;
} else {
throw new NoSuchPortException();
}
}
public static void closeSerialPort(SerialPort serialPort) {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
System.out.println("关闭了串口:" + serialPort.getName());
}
}
public static void sendData(SerialPort serialPort, byte[] data) throws IOException {
OutputStream os = null;
try {
os = serialPort.getOutputStream();
os.write(data);
os.flush();
} catch (IOException e) {
throw e;
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static byte[] readData(SerialPort serialPort) throws IOException {
InputStream is = null;
byte[] bytes = null;
try {
is = serialPort.getInputStream();
int bufflenth = is.available();
while (bufflenth != 0) {
bytes = new byte[bufflenth];
is.read(bytes);
bufflenth = is.available();
}
} catch (IOException e) {
throw e;
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
public static void setListenerToSerialPort(SerialPort serialPort, SerialPortEventListener listener) throws TooManyListenersException {
serialPort.addEventListener(listener);
serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnBreakInterrupt(true);
}
}
调用
SerialPortUtil.openSerialPort( 端口 getSerialPortList().get(0));
SerialPortUtil.setListenerToSerialPort(serialPort, event -> {
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
byte[] bytes;
try {
bytes = SerialPortUtil.readData(serialPort);
baseTestService.readMsg(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
});