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