-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
161 lines (113 loc) · 3.44 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!doctype html>
<html>
<head>
<title>网页版串口助手</title>
<script src="term.js"></script>
<script src="debugout.js"></script>
</head>
<body>
<div style="display:inline-block; vertical-align:top;">
<b>网页版串口助手</b>
<br>
串口波特率选择:
<select name="speed" id="SerialSpeed">
<option value="115200">115200</option>
<option value="1200">1200</option>
<option value="2400">2400</option>
<option value="4800">4800</option>
<option value="9600">9600</option>
<option value="19200">19200</option>
<option value="38400">38400</option>
<option value="57600">57600</option>
</select>
<button id="SerialConnectButton" type="button" disabled>连接</button>
<button onclick="bugout.downloadLog();">下载日志</button>
<div id="term">
</div>
</div>
<p> Use Ctrl+A+C to copy and Ctrl+A+V to paste</p>
<p> Source code: <a href=https://github.com/linyuxuanlin/Serial_on_Web</a> </p>
</body>
<script>
var bugout = new debugout();
var term;
function calculate_size(win) {
var cols = Math.max(80, Math.min(150, (win.innerWidth - 280) / 7)) | 0;
var rows = Math.max(24, Math.min(80, (win.innerHeight - 180) / 12)) | 0;
return [cols, rows];
}
(function() {
window.onload = function() {
var size = calculate_size(self);
term = new Terminal({
cols: size[0],
rows: size[1],
useStyle: true,
screenKeys: true,
cursorBlink: false
});
term.open(document.getElementById("term"));
};
window.addEventListener('resize', function() {
var size = calculate_size(self);
term.resize(size[0], size[1]);
});
}).call(this);
const connectButton = document.getElementById ('SerialConnectButton');
let port;
if ('serial' in navigator) {
connectButton.addEventListener('click', function () {
if (port) {
term.write('\x1b[31mDisconnected from Serial Port\x1b[m\r\n');
port.close();
port = undefined;
connectButton.innerText = '连接';
document.getElementById('SerialSpeed').disabled = false;
}
else {
connectButton.innerText = '断开';
getReader();
}
}
);
connectButton.disabled = false;
}
else {
const error = document.createElement('p');
error.innerHTML = '<p>Support for Serial Web API not enabled. Please enable it using chrome://flags/ and enable Experimental Web Platform fetures</p>';
}
let lineBuffer = '';
let latestValue = 0;
async function serialWrite(data) {
encoder = new TextEncoder();
const dataArrayBuffer = encoder.encode(data);
if (port && port.writable) {
const writer = port.writable.getWriter();
writer.write(dataArrayBuffer);
writer.releaseLock();
}
}
async function getReader() {
port = await navigator.serial.requestPort({});
var e = document.getElementById("SerialSpeed");
var strSpd = e.options[e.selectedIndex].value;
var speed = parseInt(strSpd);
await port.open({ baudRate: [speed] });
document.getElementById('SerialSpeed').disabled = true;
connectButton.innerText = 'Disconnect';
term.write('\x1b[31mWelcome to Serial_on_Web !\x1b[m\r\n');
const appendStream = new WritableStream({
write(chunk) {
term.write(chunk);
bugout.log(chunk);
}
});
port.readable
.pipeThrough(new TextDecoderStream())
.pipeTo(appendStream);
term.on('data', function(data) {
serialWrite(data);
});
}
</script>
</html>