diff --git a/acs/nladmin-system/nlsso-server/pom.xml b/acs/nladmin-system/nlsso-server/pom.xml index b91bdce..9712e87 100644 --- a/acs/nladmin-system/nlsso-server/pom.xml +++ b/acs/nladmin-system/nlsso-server/pom.xml @@ -37,7 +37,12 @@ dynamic-tp-spring-boot-starter-adapter-webserver 1.1.6.1 - + + + com.fazecast + jSerialComm + 2.10.4 + com.yomahub diff --git a/acs/nladmin-system/nlsso-server/src/test/java/org/nl/ApplicationTest.java b/acs/nladmin-system/nlsso-server/src/test/java/org/nl/ApplicationTest.java index 70ee8af..ada26c2 100644 --- a/acs/nladmin-system/nlsso-server/src/test/java/org/nl/ApplicationTest.java +++ b/acs/nladmin-system/nlsso-server/src/test/java/org/nl/ApplicationTest.java @@ -1,5 +1,6 @@ package org.nl; +import com.fazecast.jSerialComm.SerialPort; import org.springframework.boot.test.context.SpringBootTest; /** @@ -9,5 +10,36 @@ import org.springframework.boot.test.context.SpringBootTest; */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ApplicationTest { - + public static void main(String[] args) { + // 列出所有可用的串口 + SerialPort[] commPorts = SerialPort.getCommPorts(); + for (SerialPort port : commPorts) { + System.out.println(port.getSystemPortName()); + } + for (SerialPort serialPort : commPorts) { + boolean com5 = serialPort.getSystemPortName().equals("COM5"); + if (com5){ + try { + serialPort.openPort();// 打开串口 + serialPort.setComPortParameters(9600, 8, 1, 0);// 设置串口参数 + serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0); + byte[] buffer = new byte[1024]; + while (true){ + Thread.sleep(300); + int bytesRead = serialPort.readBytes(buffer, buffer.length); + if (bytesRead > 0) { + String input = new String(buffer, 0, bytesRead); + System.out.println(input); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (serialPort.isOpen()) { + serialPort.closePort();// 关闭串口 + } + } + } + } + } }