web串口调试助手,Java后端代码,前端代码
时间: 2023-07-09 21:04:35 浏览: 223
以下是一个简单的Web串口调试助手的Java后端代码和前端代码。
Java后端代码:
```java
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
public class SerialCommunication {
private static SerialPort serialPort;
private static OutputStream outputStream;
private static InputStream inputStream;
public static void init(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.err.println("Error: Port is currently in use");
} else {
serialPort = (SerialPort) portIdentifier.open("SerialCommunication", 2000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
}
}
public static void write(String data) throws Exception {
outputStream.write(data.getBytes());
outputStream.flush();
}
public static String read() throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
len = inputStream.read(buffer);
if (len > 0) {
return new String(buffer, 0, len);
}
return null;
}
public static void close() {
if (serialPort != null) {
serialPort.close();
}
}
public static void main(String[] args) throws Exception {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
init("COM3");
write("Hello World!");
String result = read();
System.out.println(result);
close();
}
}
```
前端代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web Serial Communication</title>
<style>
#message {
width: 100%;
height: 500px;
font-size: 14px;
}
#input {
width: 100%;
height: 30px;
font-size: 14px;
}
</style>
</head>
<body onload="init()">
<h1>Web Serial Communication</h1>
<select id="port"></select>
<button onclick="connect()">Connect</button>
<button onclick="disconnect()">Disconnect</button>
<hr>
<textarea id="message" readonly></textarea>
<hr>
<input type="text" id="input">
<button onclick="send()">Send</button>
</body>
<script>
var portSelect = document.getElementById('port');
var messageTextarea = document.getElementById('message');
var inputText = document.getElementById('input');
var reader = null;
var writer = null;
async function init() {
let ports = await navigator.serial.getPorts();
for (let port of ports) {
let option = document.createElement("option");
option.text = port.name;
portSelect.add(option);
}
}
async function connect() {
let portName = portSelect.options[portSelect.selectedIndex].value;
let port = await navigator.serial.requestPort({serialNumber: portName});
await port.open({baudRate: 9600});
let {reader, writer} = await port.readable.getWriter();
this.reader = reader;
this.writer = writer;
readLoop();
}
async function readLoop() {
while (true) {
let result = await reader.read();
if (result.done) {
console.log('EOF');
return;
}
messageTextarea.value += new TextDecoder().decode(result.value);
}
}
async function disconnect() {
if (reader) {
reader.cancel();
reader.releaseLock();
reader = null;
}
if (writer) {
writer.close();
writer = null;
}
}
async function send() {
let data = inputText.value;
writer.write(new TextEncoder().encode(data));
inputText.value = '';
}
</script>
</html>
```
注意,这里使用的是Web Serial API,需要在Chrome浏览器中开启实验性功能才能正常使用。
阅读全文