|
|
|
@ -1,67 +1,99 @@
|
|
|
|
|
package com.xydl.cac.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.fazecast.jSerialComm.SerialPort;
|
|
|
|
|
import com.xydl.cac.model.StaticVariable;
|
|
|
|
|
import com.xydl.cac.service.SerialPortService;
|
|
|
|
|
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.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class SerialPortServiceImpl implements SerialPortService {
|
|
|
|
|
|
|
|
|
|
@Value("${cac.warnport.name}")
|
|
|
|
|
public boolean warnportname;
|
|
|
|
|
|
|
|
|
|
public List<SerialPort> serialPortList;
|
|
|
|
|
public String warnportname;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<SerialPort> discoverSerialPort() {
|
|
|
|
|
serialPortList = Arrays.asList(SerialPort.getCommPorts());
|
|
|
|
|
// 遍历串口列表并打印每个串口的名称
|
|
|
|
|
for (SerialPort port : serialPortList) {
|
|
|
|
|
System.out.println("获取串口: " + port.getSystemPortName());
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return serialPortList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//关闭串口
|
|
|
|
|
@Override
|
|
|
|
|
public boolean closeSerialPort(SerialPort port, String portname) {
|
|
|
|
|
for (SerialPort portItem : serialPortList) {
|
|
|
|
|
if (portItem.getSystemPortName().equals(portname)) {
|
|
|
|
|
if (portItem != null) {
|
|
|
|
|
System.out.println("断开端口" + portItem.getSystemPortName() + "连接");
|
|
|
|
|
return portItem.closePort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void closeSerialPort() {
|
|
|
|
|
if (StaticVariable.currentPort != null) {
|
|
|
|
|
SerialPort port = StaticVariable.currentPort;
|
|
|
|
|
port.closePort();
|
|
|
|
|
} else {
|
|
|
|
|
log.error("断开串口失败,没有对应的硬接点告警串口!");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//发送数据到串口
|
|
|
|
|
@Override
|
|
|
|
|
public void sendData(SerialPort port, boolean data) throws IOException {
|
|
|
|
|
byte[] buffer2;
|
|
|
|
|
if (data) {
|
|
|
|
|
buffer2 = new byte[]{00, (byte) 0xf1, (byte) 0xff};
|
|
|
|
|
} else {
|
|
|
|
|
buffer2 = new byte[]{00, (byte) 0x01, (byte) 0xff};
|
|
|
|
|
@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) {
|
|
|
|
|
if (data) {
|
|
|
|
|
port.writeBytes(openbuffer, openbuffer.length);
|
|
|
|
|
} else {
|
|
|
|
|
port.writeBytes(stopbuffer, stopbuffer.length);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(5000);
|
|
|
|
|
port.writeBytes(stopbuffer, stopbuffer.length);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
port.writeBytes(buffer2, buffer2.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean openSerialPort(SerialPort port) {
|
|
|
|
|
if (port.getSystemPortName().equals(warnportname)) {
|
|
|
|
|
int baudRate = 9600; // 波特率
|
|
|
|
|
int parity = SerialPort.EVEN_PARITY; // 校验位
|
|
|
|
|
int dataBits = 8; // 数据位
|
|
|
|
|
int stopBits = SerialPort.ONE_STOP_BIT; // 停止位
|
|
|
|
|
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); // 设置超时
|
|
|
|
@ -70,18 +102,22 @@ public class SerialPortServiceImpl implements SerialPortService {
|
|
|
|
|
}
|
|
|
|
|
boolean b = port.openPort();
|
|
|
|
|
if (b) {
|
|
|
|
|
log.info("打开硬接点告警串口成功!");
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
log.error("打开串口失败!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
// return ResponseResult.getErrorResult("Failed to open port " + port.getSystemPortName() + e.getMessage().toString());
|
|
|
|
|
log.error("打开串口失败" + e.getMessage());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// return ResponseResult.getSuccessResult("COM10 Serial port open");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.error("打开串口失败,找不到硬接点串口!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// return ResponseResult.getErrorResult("Failed to find port " + port.getSystemPortName());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|