优化灯光
This commit is contained in:
@@ -51,40 +51,48 @@ public class SerialPortHandler {
|
||||
try {
|
||||
// 获取所有可用的串口
|
||||
SerialPort[] ports = SerialPort.getCommPorts();
|
||||
log.info("可用串口列表: {}", java.util.Arrays.stream(ports).map(SerialPort::getSystemPortName).toList());
|
||||
|
||||
// 查找指定的串口
|
||||
SerialPort targetPort = null;
|
||||
for (SerialPort port : ports) {
|
||||
if (port.getSystemPortName().equals(portName)) {
|
||||
serialPort = port;
|
||||
String portName = port.getSystemPortName();
|
||||
log.debug("检查串口: {}", portName);
|
||||
if (portName.equals(this.portName)) {
|
||||
targetPort = port;
|
||||
log.info("找到目标串口: {}", portName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (serialPort == null) {
|
||||
log.error("未找到串口: {}", portName);
|
||||
if (targetPort == null) {
|
||||
log.error("未找到串口: {}", this.portName);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 配置串口参数
|
||||
serialPort.setBaudRate(baudRate);
|
||||
serialPort.setNumDataBits(8);
|
||||
serialPort.setNumStopBits(1);
|
||||
serialPort.setParity(SerialPort.NO_PARITY);
|
||||
serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
|
||||
targetPort.setBaudRate(baudRate);
|
||||
targetPort.setNumDataBits(8);
|
||||
targetPort.setNumStopBits(1);
|
||||
targetPort.setParity(SerialPort.NO_PARITY);
|
||||
targetPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
|
||||
|
||||
// 打开串口
|
||||
boolean opened = serialPort.openPort();
|
||||
boolean opened = targetPort.openPort();
|
||||
if (opened) {
|
||||
// 设置读取超时时间(毫秒)
|
||||
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 1000, 0);
|
||||
inputStream = serialPort.getInputStream();
|
||||
outputStream = serialPort.getOutputStream();
|
||||
log.info("串口 {} 打开成功,波特率: {}", portName, baudRate);
|
||||
targetPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 1000, 0);
|
||||
this.serialPort = targetPort;
|
||||
this.inputStream = targetPort.getInputStream();
|
||||
this.outputStream = targetPort.getOutputStream();
|
||||
log.info("串口 {} 打开成功,波特率: {}", this.portName, baudRate);
|
||||
} else {
|
||||
log.error("串口 {} 打开失败", this.portName);
|
||||
}
|
||||
return opened;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("打开串口失败: {}", e.getMessage());
|
||||
log.error("打开串口 {} 失败: {}", this.portName, e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -103,15 +111,21 @@ public class SerialPortHandler {
|
||||
*/
|
||||
public void sendData(byte[] data) {
|
||||
try {
|
||||
// 验证串口是否正确打开
|
||||
if (serialPort == null || !serialPort.isOpen()) {
|
||||
log.error("串口 {} 未打开,无法发送数据", portName);
|
||||
return;
|
||||
}
|
||||
|
||||
if (outputStream == null) {
|
||||
log.error("输出流未初始化");
|
||||
log.error("串口 {} 输出流未初始化", portName);
|
||||
return;
|
||||
}
|
||||
outputStream.write(data);
|
||||
outputStream.flush();
|
||||
log.debug("发送: {}", new String(data));
|
||||
log.info("串口 {} 发送数据: {}", portName, new String(data));
|
||||
} catch (IOException e) {
|
||||
log.error("发送数据失败: {}", e.getMessage());
|
||||
log.error("串口 {} 发送数据失败: {}", portName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,9 +135,10 @@ public class SerialPortHandler {
|
||||
*/
|
||||
public void sendData(String data) {
|
||||
try {
|
||||
log.info("串口 {} 准备发送字符串: {}", portName, data);
|
||||
sendData(data.getBytes(StandardCharsets.US_ASCII));
|
||||
} catch (Exception e) {
|
||||
log.error("发送字符串数据失败: {}", e.getMessage());
|
||||
log.error("串口 {} 发送字符串数据失败: {}", portName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,6 +149,18 @@ public class SerialPortHandler {
|
||||
*/
|
||||
public String receiveResponse(int timeoutMs) {
|
||||
try {
|
||||
// 验证串口是否正确打开
|
||||
if (serialPort == null || !serialPort.isOpen()) {
|
||||
log.error("串口 {} 未打开,无法接收数据", portName);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 验证输入流是否初始化
|
||||
if (inputStream == null) {
|
||||
log.error("串口 {} 输入流未初始化", portName);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 等待数据到达
|
||||
long startTime = System.currentTimeMillis();
|
||||
while (inputStream.available() == 0 &&
|
||||
@@ -147,11 +174,11 @@ public class SerialPortHandler {
|
||||
if (available > 0) {
|
||||
int len = inputStream.read(buffer, 0, Math.min(available, buffer.length));
|
||||
String response = new String(buffer, 0, len);
|
||||
log.debug("接收: {}", response);
|
||||
log.info("串口 {} 接收数据: {}", portName, response);
|
||||
return response;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("接收响应失败: ", e);
|
||||
log.error("串口 {} 接收响应失败: {}", portName, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -164,6 +191,18 @@ public class SerialPortHandler {
|
||||
*/
|
||||
public String receiveResponse(int length, int timeoutMs) {
|
||||
try {
|
||||
// 验证串口是否正确打开
|
||||
if (serialPort == null || !serialPort.isOpen()) {
|
||||
log.error("串口 {} 未打开,无法接收数据", portName);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 验证输入流是否初始化
|
||||
if (inputStream == null) {
|
||||
log.error("串口 {} 输入流未初始化", portName);
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] buffer = new byte[length];
|
||||
int bytesRead = 0;
|
||||
long startTime = System.currentTimeMillis();
|
||||
@@ -181,11 +220,11 @@ public class SerialPortHandler {
|
||||
|
||||
if (bytesRead > 0) {
|
||||
String response = new String(buffer, 0, bytesRead);
|
||||
log.debug("接收: {}", response);
|
||||
log.info("串口 {} 接收数据: {}", portName, response);
|
||||
return response;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("接收响应失败: {}", e.getMessage());
|
||||
log.error("串口 {} 接收响应失败: {}", portName, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user