<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>rxtxcommon</artifactId><version>0.0.1-SNAPSHOT</version><name>rxtxcommon</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!--websocket--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>provided</scope></dependency><!--jSerialComm库串口通讯--><dependency><groupId>com.fazecast</groupId><artifactId>jSerialComm</artifactId><version>2.6.2</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.控制层
importcom.fazecast.jSerialComm.SerialPort;importcom.example.rxtxcommon.entity.ResponseResult;importcom.example.rxtxcommon.service.SerialPortService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.socket.WebSocketSession;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;@RestController@RequestMapping("/serial")publicclassSerialController{@AutowiredprivateSerialPortService serialPortService;publicList<SerialPort> serialPortList;publicint countsend;publicint countreceive;@GetMapping("/discover")publicResponseResult<List<String>>discoverSerialPort(){List<SerialPort> list1 = serialPortService.discoverSerialPort();List<String> list =newArrayList<>();
serialPortList = list1;
list1.forEach(item ->{
list.add(item.getSystemPortName());});ResponseResult<List<String>> response =newResponseResult<>(list.size()==0?500:1,"", list);return response;}@GetMapping("/open")publicResponseResult<Boolean>openSerialPort(SerialPort port1){WebSocketSession session =null;for(SerialPort port : serialPortList){if(port.getSystemPortName().equals("COM10")){return serialPortService.openSerialPort(port, session);}}returnResponseResult.getErrorResult("Failed to find port ");}@GetMapping("/send")publicStringsendData(SerialPort port1,String data)throwsIOException{//发送数据注意,提前与接收设备沟通好协议,发送什么样类型的数据设备才可以进行响应,否则设备无响应for(SerialPort port : serialPortList){if(port.getSystemPortName().equals("COM10")){
serialPortService.sendData(port, data);}}return"Data sent>>>"+(countsend++);}@GetMapping("/close")publicStringcloseSerialPort(SerialPort port1){for(SerialPort port : serialPortList){// 遍历串口列表并为每个串口设置参数
serialPortService.closeSerialPort(port);}return"Serial port closed";}}