告警硬接点装置代码调整和判断调整
parent
2d5491876b
commit
dfbb28af3d
@ -1,4 +1,4 @@
|
|||||||
package com.xydl.cac.service.impl;
|
package com.xydl.cac.serialport;
|
||||||
|
|
||||||
import com.fazecast.jSerialComm.SerialPort;
|
import com.fazecast.jSerialComm.SerialPort;
|
||||||
import com.fazecast.jSerialComm.SerialPortDataListener;
|
import com.fazecast.jSerialComm.SerialPortDataListener;
|
@ -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<SerialPort> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
|
||||||
}
|
|
@ -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<SerialPort> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue