1.添加依赖
点击查看代码
<dependency>
<groupId>org.scream3r</groupId>
<artifactId>jssc</artifactId>
<version>2.8.0</version>
</dependency>
点击查看代码
package com.pangus.ims.custom.client.lannRay.util;
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @program: ims-custom-client-lannray
* @description: JSSC 库的串口工具类
* @author: 王帅令
* @create: 2024-10-16 09:20
**/
public class SerialPortJSSCUtil {
private boolean isEventListenerAdded = false;
public SerialPort serialPort;
public SerialPortJSSCUtil(String portName) {
serialPort = new SerialPort(portName);
}
public void openPort(int baudRate, int dataBits, int stopBits, int parity) throws SerialPortException {
serialPort.openPort();
serialPort.setParams(baudRate, dataBits, stopBits, parity);
}
public void closePort() throws SerialPortException {
if (serialPort.isOpened()) {
serialPort.closePort();
}
}
public void writeData(String data) throws SerialPortException {
serialPort.writeBytes(data.getBytes());
}
public void setSerialPortEventListener(SerialPortEventListener listener) throws SerialPortException {
if (!isEventListenerAdded) {
serialPort.addEventListener(listener);
isEventListenerAdded = true;
}
}
public boolean isPortOpened() {
return serialPort.isOpened();
}
public static void main(String[] args) {
SerialPortJSSCUtil serialUtil = new SerialPortJSSCUtil("COM1");
try {
serialUtil.openPort(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialUtil.setSerialPortEventListener(new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR()) {
try {
byte[] buffer = serialUtil.serialPort.readBytes(event.getEventValue());
System.out.println(new String(buffer));
} catch (SerialPortException ex) {
ex.printStackTrace();
}
}
}
});
System.out.println("Serial port opened and listening...");
// Example of writing data to the serial port
//serialUtil.writeData("Hello, Serial Port!");
} catch (SerialPortException ex) {
ex.printStackTrace();
} finally {
try {
serialUtil.closePort();
} catch (SerialPortException ex) {
ex.printStackTrace();
}
}
}
}
点击查看代码
/**
* 读取出口数据数据
*
**/
public void readSerialData() throws IOException {
try {
if (!serialPortJSSCUtil.isPortOpened()){
serialPortJSSCUtil.openPort(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
serialPortJSSCUtil.setSerialPortEventListener(new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR()) {
try {
byte[] buffer = serialPortJSSCUtil.serialPort.readBytes(event.getEventValue());
System.out.println(new String(buffer));
String RecvStr=new String(buffer);
if(!StringUtils.isEmpty(RecvStr)) {
ContentBody body=new ContentBody();
LinkedHashMap<String, Object> recvData = new LinkedHashMap<>();
recvData.put("RecvData",RecvStr);
body.setExtendData(recvData);
//构建iotBaseMessage
final ContentMessage<ContentBody> message = new ContentMessage<>();
message.setBody(body);
message.setDeviceType(getDeviceType());
//发送数据到上层系统
sendMessage(message);
}
} catch (SerialPortException ex) {
ex.printStackTrace();
}
}
}
});
} catch (SerialPortException ex) {
ex.printStackTrace();
}
}
5.将读取方法放入到线程中
点击查看代码
@Override
public void doWork() {
try {
//读串口数据
readSerialData();
//加延时
// TimeUnit.MINUTES.sleep(10); // 延时10分钟
} catch (final RemoteNetWorkException ex) {
throw new DriverException(ex.getMessage(), ex);
} catch (final DriverException ex) {
//驱动解析异常,需要人工干预的,直接抛出异常
throw ex;
} catch (final Exception ex) {
//其他异常,例如数据上传到MES,MES返回提示标签不存在的业务类异常,这种也要删原始日志文件
throw new DriverException(ex.getMessage(), ex);
}
}