From dfbb28af3df2df1d84ff7f6a251bfb1c5751965e Mon Sep 17 00:00:00 2001 From: liuguijing <123456> Date: Tue, 10 Dec 2024 14:27:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E7=A1=AC=E6=8E=A5=E7=82=B9?= =?UTF-8?q?=E8=A3=85=E7=BD=AE=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4=E5=92=8C?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xydl/cac/controller/TestController.java | 17 +-- .../com/xydl/cac/model/StaticVariable.java | 3 +- .../impl => serialport}/DataListener.java | 2 +- .../xydl/cac/serialport/SerialPortServer.java | 137 ++++++++++++++++++ .../xydl/cac/service/SerialPortService.java | 16 -- .../service/impl/SerialPortServiceImpl.java | 137 ------------------ src/main/resources/application-dev.yml | 4 +- 7 files changed, 147 insertions(+), 169 deletions(-) rename src/main/java/com/xydl/cac/{service/impl => serialport}/DataListener.java (97%) create mode 100644 src/main/java/com/xydl/cac/serialport/SerialPortServer.java delete mode 100644 src/main/java/com/xydl/cac/service/SerialPortService.java delete mode 100644 src/main/java/com/xydl/cac/service/impl/SerialPortServiceImpl.java diff --git a/src/main/java/com/xydl/cac/controller/TestController.java b/src/main/java/com/xydl/cac/controller/TestController.java index f77711d..78c8927 100644 --- a/src/main/java/com/xydl/cac/controller/TestController.java +++ b/src/main/java/com/xydl/cac/controller/TestController.java @@ -2,8 +2,8 @@ package com.xydl.cac.controller; import com.xydl.cac.model.Response; import com.xydl.cac.model.StaticVariable; +import com.xydl.cac.serialport.SerialPortServer; import com.xydl.cac.service.IcdFileConfigService; -import com.xydl.cac.service.SerialPortService; import com.xydl.cac.socket.WebSocketServer; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -11,7 +11,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.socket.WebSocketSession; import javax.annotation.Resource; import java.io.IOException; @@ -30,7 +29,7 @@ public class TestController extends BasicController { WebSocketServer webSocketServer; @Resource - SerialPortService serialPortService; + SerialPortServer serialPortService; @GetMapping("compare61850") @ApiOperation("比对61850的不同点") @@ -90,12 +89,8 @@ public class TestController extends BasicController { @GetMapping("/discover") public Response discoverSerialPort() { - Boolean aBoolean = serialPortService.discoverSerialPort(); - if (aBoolean) { - return Response.success("发现了"); - } else { + serialPortService.discoverSerialPort(); return Response.fail("未找到"); - } } @GetMapping("/open") @@ -104,14 +99,14 @@ public class TestController extends BasicController { } @GetMapping("/send") - public String sendData(boolean data) throws IOException { + public String sendData() throws IOException { //发送数据注意,提前与接收设备沟通好协议,发送什么样类型的数据设备才可以进行响应,否则设备无响应 - serialPortService.sendData(data); + serialPortService.sendData(); return "Data sent>>>"; } @GetMapping("/close") - public String closeSerialPort(String portname) { + public String closeSerialPort() { serialPortService.closeSerialPort(); return "Serial port closed"; } diff --git a/src/main/java/com/xydl/cac/model/StaticVariable.java b/src/main/java/com/xydl/cac/model/StaticVariable.java index 708cd51..e8baf76 100644 --- a/src/main/java/com/xydl/cac/model/StaticVariable.java +++ b/src/main/java/com/xydl/cac/model/StaticVariable.java @@ -30,8 +30,7 @@ public class StaticVariable { public static List zsb_Cache = null; public static ConcurrentHashMap rule_Cache = new ConcurrentHashMap<>(); - public static SerialPort currentPort; - public static Date LastWarningTime;//最后一次硬接点告警时间 + public static void wait(int seconds) throws InterruptedException { for (int i = 0; i < seconds; i++) { diff --git a/src/main/java/com/xydl/cac/service/impl/DataListener.java b/src/main/java/com/xydl/cac/serialport/DataListener.java similarity index 97% rename from src/main/java/com/xydl/cac/service/impl/DataListener.java rename to src/main/java/com/xydl/cac/serialport/DataListener.java index 4306f3e..c46e87a 100644 --- a/src/main/java/com/xydl/cac/service/impl/DataListener.java +++ b/src/main/java/com/xydl/cac/serialport/DataListener.java @@ -1,4 +1,4 @@ -package com.xydl.cac.service.impl; +package com.xydl.cac.serialport; import com.fazecast.jSerialComm.SerialPort; import com.fazecast.jSerialComm.SerialPortDataListener; diff --git a/src/main/java/com/xydl/cac/serialport/SerialPortServer.java b/src/main/java/com/xydl/cac/serialport/SerialPortServer.java new file mode 100644 index 0000000..91356e5 --- /dev/null +++ b/src/main/java/com/xydl/cac/serialport/SerialPortServer.java @@ -0,0 +1,137 @@ +package com.xydl.cac.serialport; + +import com.fazecast.jSerialComm.SerialPort; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +@Service +@Slf4j +public class SerialPortServer { + + @Value("${cac.warnport.name}") + public String warnportname; + @Value("${cac.warnport.intervaltime:60}") + public Integer intervaltime; + @Value("${cac.warnport.warntime:5}") + public Integer warntime; + + public SerialPort currentPort;//当前可用串口 + public long LastWarningTime;//最后一次硬接点告警时间 + public boolean started = false; + + public void discoverSerialPort() { + if (currentPort == null) { + List serialPortList = new ArrayList<>(); + SerialPort[] commPorts = SerialPort.getCommPorts(); + if (commPorts != null && commPorts.length > 0) { + serialPortList = Arrays.asList(commPorts); + for (SerialPort port : serialPortList) { + log.info("获取串口: " + port.getSystemPortName()); + Pattern pattern = Pattern.compile("ttyCH341."); + String systemPortName = port.getSystemPortName(); + if (systemPortName != null) { + if (pattern.matcher(systemPortName).matches()) { + currentPort = port; + log.info("获取到硬接点告警串口: " + port.getSystemPortName()); + } + } + } + } else { + log.error("未获取到硬接点告警串口!"); + } + } + } + + public void closeSerialPort() { + if (currentPort != null) { + SerialPort port = currentPort; + port.closePort(); + } else { + log.error("断开串口失败,没有对应的硬接点告警串口!"); + } + } + + + //发送数据到串口 + @Async + public void sendData() { + if (!started) { + started = true; + if (openSerialPort()) { + byte[] openbuffer = new byte[]{00, (byte) 0xf1, (byte) 0xff};//发送串口告警 + byte[] stopbuffer = new byte[]{00, (byte) 0x01, (byte) 0xff};//关闭串口告警 + SerialPort port = currentPort; + if (port != null) { + long l = System.currentTimeMillis(); + if (LastWarningTime == 0) { + LastWarningTime = l; + } else { + if (l - LastWarningTime <= (intervaltime * 1000)) { + started = false; + return; + } + } + LastWarningTime = l; + port.writeBytes(openbuffer, openbuffer.length); + try { + Thread.sleep(warntime * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + port.writeBytes(stopbuffer, stopbuffer.length); + started = false; + } + } + } + started = false; + } + } + + + public Boolean openSerialPort() { + discoverSerialPort(); + if (currentPort != null) { + if (currentPort.isOpen()) { + log.info("硬接点告警串口已经打开!"); + return true; + } else { + int baudRate = 9600; // 波特率 + int parity = SerialPort.EVEN_PARITY; // 校验位 + int dataBits = 8; // 数据位 + int stopBits = SerialPort.ONE_STOP_BIT; // 停止位 + try { + boolean setComResult = currentPort.setComPortParameters(baudRate, dataBits, stopBits, parity); // 设置参数 + boolean setComTimes = currentPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 20000, 0); // 设置超时 + if (setComResult && setComTimes) { + currentPort.addDataListener(new DataListener(currentPort.getSystemPortName())); + } + boolean b = currentPort.openPort(); + if (b) { + log.info("打开硬接点告警串口成功!"); + return true; + } else { + currentPort = null; + log.error("打开串口失败!"); + return false; + } + } catch (Exception e) { + e.printStackTrace(); + currentPort = null; + log.error("打开串口失败" + e.getMessage()); + return false; + } + } + } else { + log.error("打开串口失败,找不到硬接点串口!"); + return false; + } + } + +} diff --git a/src/main/java/com/xydl/cac/service/SerialPortService.java b/src/main/java/com/xydl/cac/service/SerialPortService.java deleted file mode 100644 index 45fe2d4..0000000 --- a/src/main/java/com/xydl/cac/service/SerialPortService.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xydl.cac.service; - -import com.fazecast.jSerialComm.SerialPort; - -import java.io.IOException; -import java.util.List; - -public interface SerialPortService { - Boolean discoverSerialPort(); - - void closeSerialPort(); - - Boolean openSerialPort(); - - void sendData(boolean data) throws IOException; -} diff --git a/src/main/java/com/xydl/cac/service/impl/SerialPortServiceImpl.java b/src/main/java/com/xydl/cac/service/impl/SerialPortServiceImpl.java deleted file mode 100644 index efd069e..0000000 --- a/src/main/java/com/xydl/cac/service/impl/SerialPortServiceImpl.java +++ /dev/null @@ -1,137 +0,0 @@ -package com.xydl.cac.service.impl; - -import com.fazecast.jSerialComm.SerialPort; -import com.xydl.cac.model.StaticVariable; -import com.xydl.cac.service.SerialPortService; -import com.xydl.cac.util.DateUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; - -@Service -@Slf4j -public class SerialPortServiceImpl implements SerialPortService { - - @Value("${cac.warnport.name}") - public String warnportname; - @Value("${cac.warnport.intervaltime:60000}") - public Integer intervaltime; - @Value("${cac.warnport.warntime:5000}") - public Integer warntime; - - @Override - public Boolean discoverSerialPort() { - List serialPortList = new ArrayList<>(); - SerialPort[] commPorts = SerialPort.getCommPorts(); - if (commPorts != null && commPorts.length > 0) { - serialPortList = Arrays.asList(commPorts); - for (SerialPort port : serialPortList) { - log.info("获取串口: " + port.getSystemPortName()); - if (port.getSystemPortName().equals(warnportname)) { - StaticVariable.currentPort = port; - log.info("获取到硬接点告警串口: " + port.getSystemPortName()); - return true; - } - } - return false; - } else { - log.error("未获取到硬接点告警串口!"); - return false; - } - } - - @Override - public void closeSerialPort() { - if (StaticVariable.currentPort != null) { - SerialPort port = StaticVariable.currentPort; - port.closePort(); - } else { - log.error("断开串口失败,没有对应的硬接点告警串口!"); - } - } - - - //发送数据到串口 - @Override - @Async - public void sendData(boolean data) { - if (openSerialPort()) { - byte[] openbuffer = new byte[]{00, (byte) 0xf1, (byte) 0xff};//发送串口告警 - byte[] stopbuffer = new byte[]{00, (byte) 0x01, (byte) 0xff};//关闭串口告警 - SerialPort port = StaticVariable.currentPort; - if (port != null) { - Date date = new Date(); - if (StaticVariable.LastWarningTime == null) { - StaticVariable.LastWarningTime = new Date(); - } else { - long lastWarningTimeTime = StaticVariable.LastWarningTime.getTime(); - long time = date.getTime(); - if (time - lastWarningTimeTime <= intervaltime) { - return; - } - } - StaticVariable.LastWarningTime = date; - port.writeBytes(openbuffer, openbuffer.length); - try { - Thread.sleep(warntime); - } catch (InterruptedException e) { - e.printStackTrace(); - } finally { - port.writeBytes(stopbuffer, stopbuffer.length); - } - } - } - } - - - @Override - public Boolean openSerialPort() { - Boolean discover = false; - if (StaticVariable.currentPort == null) { - discover = discoverSerialPort(); - } else { - discover = true; - } - if (discover) { - SerialPort port = StaticVariable.currentPort; - if (port.isOpen()) { - log.info("硬接点告警串口已经打开!"); - return true; - } else { - int baudRate = 9600; // 波特率 - int parity = SerialPort.EVEN_PARITY; // 校验位 - int dataBits = 8; // 数据位 - int stopBits = SerialPort.ONE_STOP_BIT; // 停止位 - try { - boolean setComResult = port.setComPortParameters(baudRate, dataBits, stopBits, parity); // 设置参数 - boolean setComTimes = port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 20000, 0); // 设置超时 - if (setComResult && setComTimes) { - port.addDataListener(new DataListener(port.getSystemPortName())); - } - boolean b = port.openPort(); - if (b) { - log.info("打开硬接点告警串口成功!"); - return true; - } else { - log.error("打开串口失败!"); - return false; - } - } catch (Exception e) { - e.printStackTrace(); - log.error("打开串口失败" + e.getMessage()); - return false; - } - } - } else { - log.error("打开串口失败,找不到硬接点串口!"); - return false; - } - } - -} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index d6cface..e3ccde9 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -51,5 +51,5 @@ cac: rsakey: MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKNPuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gAkM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWowcSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99EcvDQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthhYhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3UP8iWi1Qw0Y= warnport: name: ttyCH341USB0 - intervaltime: 60000 - warntime: 5000 + intervaltime: 60 + warntime: 5