From 9de4204626d8d7ddf90a2cca58de87d37c485113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E4=BF=8A=E6=9D=B0?= <9463626+zhou-junjiezjj@user.noreply.gitee.com> Date: Tue, 5 Nov 2024 15:32:58 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E4=B8=B2=E5=8F=A3=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- acs/nladmin-system/nlsso-server/pom.xml | 7 +++- .../src/test/java/org/nl/ApplicationTest.java | 34 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) 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();// 关闭串口 + } + } + } + } + } }