Browse Source

add 串口工具测试

master
周俊杰 3 weeks ago
parent
commit
9de4204626
  1. 7
      acs/nladmin-system/nlsso-server/pom.xml
  2. 34
      acs/nladmin-system/nlsso-server/src/test/java/org/nl/ApplicationTest.java

7
acs/nladmin-system/nlsso-server/pom.xml

@ -37,7 +37,12 @@
<artifactId>dynamic-tp-spring-boot-starter-adapter-webserver</artifactId>
<version>1.1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fazecast/jSerialComm -->
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.10.4</version>
</dependency>
<!-- 日志链路追踪 https://tlog.yomahub.com/pages/f62a84/#%E5%90%8C%E6%AD%A5%E6%97%A5%E5%BF%97-->
<dependency>
<groupId>com.yomahub</groupId>

34
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();// 关闭串口
}
}
}
}
}
}

Loading…
Cancel
Save