Repository navigation
Expand file tree
/
Copy pathserial-bridge.js
More file actions
310 lines (270 loc) · 9.16 KB
/
Copy pathserial-bridge.js
File metadata and controls
310 lines (270 loc) · 9.16 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// serial-bridge.js — Web Serial API wrapper with NMEA buffering
// v5 — Fixed buffer overflow, backpressure, and robust error handling
class SerialBridge {
constructor() {
this.port = null;
this.reader = null;
this.writer = null;
this.isOpen = false;
this.lineBuffer = '';
this.readInProgress = false; // защита от параллельных чтений
this._reading = false;
this._closing = false;
// Callbacks
this.onMessage = null; // (rawLine: string) => void
this.onError = null; // (error: Error) => void
this.onClose = null; // () => void
}
/**
* Request port from user and open it
* @param {number} baudRate - defaults to 9600
*/
async open(baudRate = 9600) {
console.log('[SerialBridge] open() started, baudRate =', baudRate);
if (this.isOpen) {
console.warn('[SerialBridge] Attempted to open already open port');
return false;
}
try {
console.log('[SerialBridge] Requesting port...');
this.port = await navigator.serial.requestPort();
console.log('[SerialBridge] Port selected:', this.port);
console.log('[SerialBridge] Opening port with baud rate', baudRate);
await this.port.open({
baudRate: baudRate,
dataBits: 8,
stopBits: 1,
parity: 'none',
flowControl: 'none',
bufferSize: 65536
});
console.log('[SerialBridge] Port opened successfully');
await this._drainStaleData();
console.log('[SerialBridge] Stale data drained');
this.isOpen = true;
this.lineBuffer = '';
this.readInProgress = false;
this.writer = this.port.writable.getWriter();
this.reader = this.port.readable.getReader();
console.log('[SerialBridge] Reader and writer initialized');
this._readLoop();
console.log('[SerialBridge] _readLoop() launched');
return true;
} catch (err) {
if (err.name === 'SecurityError') {
const securityErr = new Error('Permission request requires user gesture (click/tap)');
console.error('[SerialBridge] SecurityError:', securityErr.message);
if (this.onError) this.onError(securityErr);
throw securityErr;
}
console.error('[SerialBridge] Failed to open port:', err);
if (this.onError) this.onError(err);
throw err;
}
}
/**
* Сбрасывает старые данные из буфера сразу после открытия порта
*/
async _drainStaleData() {
if (!this.port || !this.port.readable) return;
const reader = this.port.readable.getReader();
try {
let keepReading = 10;
while (keepReading > 0) {
try {
const readPromise = reader.read();
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('drain_timeout')), 100)
);
const { done } = await Promise.race([readPromise, timeoutPromise]);
if (done) { keepReading = 0; }
else { keepReading--; }
} catch (err) {
keepReading = 0; // таймаут — выходим
}
}
} finally {
try { reader.releaseLock(); } catch (e) {}
}
}
/*
async _drainStaleData() {
if (!this.port || !this.port.readable) {
return;
}
const reader = this.port.readable.getReader();
try {
let keepReading = 10;
while (keepReading > 0) {
try {
const { done } = await reader.read();
if (done) {
keepReading = 0;
} else {
keepReading = keepReading - 1;
}
} catch (err) {
// Игнорируем типичные ошибки при сбросе: NetworkError, AbortError и т. п.
// Они возникают, когда порт закрывается или прерывается чтение
keepReading = false;
}
}
} catch (outerErr) {
// Крайне редкий случай — логгируем для диагностики, но не прерываем работу
console.warn('[SerialBridge] Unexpected error in _drainStaleData:', outerErr);
} finally {
try {
reader.releaseLock();
} catch (e) {
// Игнорируем ошибки при освобождении блокировки
}
}
}
*/
/**
* Send raw string to port
*/
async send(message) {
if (!this.isOpen || !this.writer) {
throw new Error('Port not open or writer unavailable');
}
const data = new TextEncoder().encode(message);
try {
await this.writer.write(data);
} catch (err) {
console.error('[SerialBridge] Send error:', err);
}
}
/**
* Internal read loop — try/catch inside while
*/
async _readLoop() {
// Защита от повторного запуска цикла
if (this._reading) {
console.warn('[SerialBridge] _readLoop() called while already reading');
return;
}
this._reading = true;
const decoder = new TextDecoder();
console.log('[SerialBridge] Read loop started');
// Быстрая проверка на старте: если уже закрыт — выходим сразу
if (!this.isOpen || !this.reader || !this.port) {
console.log('[SerialBridge] Read loop skipped: not open or no reader/port');
this._reading = false;
return;
}
while (this.reader && this.isOpen && !this.readInProgress) {
try {
this.readInProgress = true;
// Таймаут для предотвращения «вечного ожидания» на чтении
const readPromise = this.reader.read();
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Read timeout')), 30000)
);
const { value, done } = await Promise.race([readPromise, timeoutPromise]);
this.readInProgress = false;
if (done) break;
if (!value || value.length === 0) continue;
const chunk = decoder.decode(value, {stram: true});
this.lineBuffer += chunk;
let idx;
while ((idx = this.lineBuffer.indexOf('\n')) >= 0) {
let line = this.lineBuffer.substring(0, idx);
this.lineBuffer = this.lineBuffer.substring(idx + 1);
line = line.replace(/\r$/, '').trim();
if (line && line.startsWith('$') && this.onMessage) {
try {
this.onMessage(line);
} catch (e) {
console.warn('[SerialBridge] onMessage error:', e.message);
}
}
}
// Защита от переполнения буфера
if (this.lineBuffer.length > 16384) {
console.warn('[SerialBridge] Buffer overflow, reset');
this.lineBuffer = '';
}
} catch (err) {
this.readInProgress = false;
if (err.message === 'Read timeout') {
console.warn('[SerialBridge] Read timeout, closing port');
await this.close();
break;
}
if (err.name === 'NetworkError' || err.name === 'AbortError') {
console.log('[SerialBridge] Port disconnected (' + err.name + ')');
break;
}
if (err.name === 'TypeError' && err.message.includes('closed')) {
break;
}
console.warn('[SerialBridge] Read error:', err.name);
// Небольшая пауза перед повторной попыткой, чтобы не перегружать цикл
await new Promise(r => setTimeout(r, 100));
}
}
console.log('[SerialBridge] Read loop ended');
this._reading = false;
this.isOpen = false;
if (this.onClose) {
try {
this.onClose();
} catch (e) {}
}
}
/**
* Close the port and release all resources
*/
async close() {
// Добавляем флаг, чтобы не обрабатывать повторный вызов
if (this._closing) {
console.log('[SerialBridge] Close already in progress, skipping');
return;
}
this._closing = true;
console.log('[SerialBridge] Closing...');
if (!this.isOpen || !this.port) {
console.log('[SerialBridge] Already closed or no port, skipping');
this.isOpen = false;
this._closing = false;
return;
}
try {
if (this.reader) {
try { await this.reader.cancel(); } catch (err) { console.warn('Reader cancel error:', err.message); }
try { this.reader.releaseLock(); } catch (e) {}
this.reader = null;
}
if (this.writer) {
try { this.writer.releaseLock(); } catch (e) {}
this.writer = null;
}
if (this.port) {
const closePromise = this.port.close();
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Port close timeout')), 5000)
);
try {
await Promise.race([closePromise, timeoutPromise]);
} catch (err) {
if (err.message !== 'Port close timeout') {
console.warn('Port close error:', err.message);
}
}
this.port = null;
}
} catch (e) {
console.warn('[SerialBridge] Close error:', e.message);
} finally {
this.isOpen = false;
this.lineBuffer = '';
this.readInProgress = false;
this._closing = false; // Сбрасываем флаг
console.log('[SerialBridge] Closed');
}
}
get connected() {
return this.isOpen && this.port !== null;
}
}