新增硬接点继电器串口通讯
parent
2aa628e214
commit
b2c80558f0
@ -0,0 +1,16 @@
|
||||
package com.xydl.cac.service;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public interface SerialPortService {
|
||||
List<SerialPort> discoverSerialPort();
|
||||
|
||||
boolean closeSerialPort(SerialPort port, String portname);
|
||||
|
||||
Boolean openSerialPort(SerialPort port);
|
||||
|
||||
void sendData(SerialPort port, boolean data) throws IOException;
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.xydl.cac.service.impl;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
import com.fazecast.jSerialComm.SerialPortDataListener;
|
||||
import com.fazecast.jSerialComm.SerialPortEvent;
|
||||
import com.xydl.cac.util.ByteUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.compress.utils.ByteUtils;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
public class DataListener implements SerialPortDataListener {
|
||||
private WebSocketSession session;
|
||||
private String portName;
|
||||
|
||||
//通过websocket打开监听设备的时候绑定session返回给前端数据
|
||||
public DataListener(String portName) {
|
||||
this.portName = portName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getListeningEvents() {
|
||||
return SerialPort.LISTENING_EVENT_DATA_WRITTEN;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void serialEvent(SerialPortEvent event) {
|
||||
String hexString = "";
|
||||
if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_WRITTEN) {
|
||||
byte[] newData = new byte[event.getSerialPort().bytesAvailable()];
|
||||
int numRead = event.getSerialPort().readBytes(newData, newData.length);
|
||||
hexString = ByteUtil.bytesToHexString(newData);
|
||||
}
|
||||
System.out.println("监听接收串口" + portName + ";数据:" + hexString);
|
||||
session.sendMessage(new TextMessage(hexString));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,82 @@
|
||||
package com.xydl.cac.service.impl;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
import com.xydl.cac.service.SerialPortService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class SerialPortServiceImpl implements SerialPortService {
|
||||
public List<SerialPort> serialPortList;
|
||||
|
||||
@Override
|
||||
public List<SerialPort> discoverSerialPort() {
|
||||
serialPortList = Arrays.asList(SerialPort.getCommPorts());
|
||||
// 遍历串口列表并打印每个串口的名称
|
||||
for (SerialPort port : serialPortList) {
|
||||
System.out.println("获取串口: " + port.getSystemPortName());
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
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};
|
||||
}
|
||||
port.writeBytes(buffer2, buffer2.length);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean openSerialPort(SerialPort port) {
|
||||
if (port.getSystemPortName().equals("COM6")) {
|
||||
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) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// return ResponseResult.getErrorResult("Failed to open port " + port.getSystemPortName() + e.getMessage().toString());
|
||||
return false;
|
||||
}
|
||||
// return ResponseResult.getSuccessResult("COM10 Serial port open");
|
||||
}
|
||||
// return ResponseResult.getErrorResult("Failed to find port " + port.getSystemPortName());
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue