-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserialManager.js
630 lines (475 loc) · 16.7 KB
/
serialManager.js
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
export class serialManager {
constructor(modal) {
this.encoder = new TextEncoder();
this.decoder = new TextDecoder();
this.consoleDiv = document.getElementById("console");
this.port;
this.modal = modal;
this.receiveBuffer = [];
this.sentCommandBuffer = [""];
this.sentCommandBufferIndex = 0;
this.bootCommands = [
"G90",
"M260 A112 B1 S1",
"M260 A109",
"M260 B48",
"M260 B27",
"M260 S1",
"M260 A112 B2 S1",
"M260 A109",
"M260 B48",
"M260 B27",
"M260 S1",
"G0 F35000"
];
}
appendToConsole(message, direction){
let newConsoleEntry = document.createElement('p')
let timestamp = new Date().toISOString();
let dir = "";
if(direction){
dir = "[SEND]"
}
else{
dir = "[RECE]"
}
newConsoleEntry.innerHTML = dir + " - " + timestamp + " - " + message + '\n';
this.consoleDiv.appendChild(newConsoleEntry)
this.consoleDiv.scrollTop = this.consoleDiv.scrollHeight;
}
// drops and returns last element from buffer
getNextBufferLine() {
let nextLine = this.receiveBuffer.shift();
return nextLine
}
clearBuffer(){
while(this.receiveBuffer.length > 0){
this.receiveBuffer.shift();
}
}
dec2bin(dec) {
return (dec >>> 0).toString(2);
}
delay = (delayInms) => {
return new Promise(resolve => setTimeout(resolve, delayInms));
};
async connect() {
if (!navigator.serial){
this.modal.show("Browser Support", "Please use a browser that supports WebSerial, like Chrome, Opera, or Edge. <a href='https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API#browser_compatibility'>Supported Browsers.");
return false
}
const usbVendorId = 0x0483;
this.port = await navigator.serial.requestPort({ filters: [{ usbVendorId }] })
console.log("Port Selected.")
await this.port.open({
baudRate: 115200,
bufferSize: 255,
dataBits: 8,
flowControl: "none",
parity: "none",
stopBits: 1
})
console.log("Port Opened.")
// const { clearToSend, dataCarrierDetect, dataSetReady, ringIndicator} = await this.port.getSignals()
// console.log({ clearToSend, dataCarrierDetect, dataSetReady, ringIndicator})
this.listen()
document.querySelector("#connect").style.background = 'green';
document.querySelector("#connect").style.color = 'white';
document.querySelector("#connect").innerHTML = 'Connected';
//send boot commands
this.send(this.bootCommands)
return true
}
// this needs to listen to marlin constantly
// it comes in randomly, so we have to filter by newlines an add
// to buffer based on the newlines
async listen() {
while (this.port?.readable) {
console.log("Port is readable: Starting to listen.")
let metabuffer = ""
let consoleDiv = document.getElementById("console");
const reader = this.port.readable.getReader()
try {
while (true) {
const { value, done } = await reader.read()
if (done) {
// |reader| has been canceled.
console.log("Closing reader.");
break;
}
const decoded = this.decoder.decode(value)
metabuffer = metabuffer.concat(decoded);
while(metabuffer.indexOf("\n") != -1){
let splitted = metabuffer.split('\n');
// console.log(splitted)
this.receiveBuffer.push(splitted[0])
this.appendToConsole(splitted[0], false);
metabuffer = metabuffer.split('\n').slice(1).join('\n');
}
}
} catch (error) {
console.error('Reading error.', error)
} finally {
reader.releaseLock()
}
}
}
async send(commandArray) {
console.log("sending: ", commandArray);
if (this.port?.writable) {
const writer = await this.port.writable.getWriter()
for (const element of commandArray) {
await writer.write(this.encoder.encode(element + "\n"))
this.appendToConsole(element, true);
}
writer.releaseLock()
}
else{
this.modal.show("Cannot Write", "Cannot write to port. Have you connected?");
}
}
async sendRepl() {
let command = [document.querySelector("#repl-input").value];
//adding current command to buffer for uparrow access later, at position 1 to preserve a "" option
this.sentCommandBuffer.splice(1, 0, command[0])
//making sure we reset the index back to 0
this.sentCommandBufferIndex = 0;
this.send(command);
}
// control
async leftAirOn(){
const commandArray = [
"M106",
"M106 P1 S255"
]
await this.send(commandArray);
}
async leftAirOff(){
const commandArray = [
"M107",
"M107 P1"
]
await this.send(commandArray);
}
async rightAirOn(){
const commandArray = [
"M106 P2 S255",
"M106 P3 S255"
]
await this.send(commandArray);
}
async rightAirOff(){
const commandArray = [
"M107 P2",
"M107 P3"
]
await this.send(commandArray);
}
async ledOn(){
const commandArray = [
"M150 P255 R255 U255 B255"
]
await this.send(commandArray);
}
async ledOff(){
const commandArray = [
"M150 P0"
]
await this.send(commandArray);
}
async disableSteppers(){
const commandArray = [
"M18"
]
await this.send(commandArray);
}
async readLeftVac(){
if(!this.port?.writable){
this.modal.show("Cannot Write", "Cannot write to port. Have you connected?");
return false
}
const commandArrayLeft = [
"M260 A112 B1 S1"
]
const delayVal = 50;
this.clearBuffer();
let msb, csb, lsb;
const regex = new RegExp('data:(..)');
//send command array
await this.send(commandArrayLeft);
this.clearBuffer();
await this.send(["M260 A109 B6 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
msb = currLine.match("data:(..)")[1];
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B7 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
csb = currLine.match("data:(..)")[1];
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B8 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
lsb = currLine.match("data:(..)")[1];
break
}
}
// convert hex string to int
//msb = parseInt(msb, 16);
// get biggest bit to determine sign
//let readingSign = (msb & (1 << 7)) === 0 ? 1 : -1;
// clear biggest bit for actual value calc
//msb &= 0x7F;
let result = parseInt(msb+csb+lsb, 16);
if(result & (1 << 23)){
result = result - 2**24
}
let resp = await this.modal.show("Left Vacuum Sensor Value", result);
this.clearBuffer();
}
async readRightVac(){
if(!this.port?.writable){
this.modal.show("Cannot Write", "Cannot write to port. Have you connected?");
return false
}
const commandArrayRight = [
"M260 A112 B2 S1",
]
let msb, csb, lsb;
const regex = new RegExp('data:(..)');
const delayVal = 50;
this.clearBuffer();
//send command array
await this.send(commandArrayRight);
await this.delay(delayVal);
this.clearBuffer();
await this.send(["M260 A109 B6 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
msb = currLine.match("data:(..)")[1];
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B7 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
csb = currLine.match("data:(..)")[1];
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B8 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
lsb = currLine.match("data:(..)")[1];
break
}
}
// // convert hex string to int
// msb = parseInt(msb, 16);
// // get biggest bit to determine sign
// let readingSign = (msb & (1 << 7)) === 0 ? 1 : -1;
// // clear biggest bit for actual value calc
// msb &= 0x7F;
// let rightVal = parseInt(msb.toString(16)+csb+lsb, 16) * readingSign;
let result = parseInt(msb+csb+lsb, 16);
if(result & (1 << 23)){
result = result - 2**24
}
await this.modal.show("Right Vacuum Sensor Value", result);
}
// tests
async testTMC(){
if(!this.port?.writable){
this.modal.show("Cannot Write", "Cannot write to port. Have you connected?");
return false
}
let testDataBuffer = "";
const commandArray = [
"M122"
]
//clean out receive buffer
await this.clearBuffer();
//send command array
await this.send(commandArray);
await this.delay(5000);
//check receieve buffer
console.log(this.receiveBuffer);
//adding to test buffer
for(let i = 0; i<this.receiveBuffer.length; i++){
testDataBuffer = testDataBuffer.concat(this.receiveBuffer[i] + "\n");
}
let resp = await this.modal.show("Stepper Driver Test Complete", "Test is complete. Click OK to download test report.");
if(resp == true){
let filename = new Date().toISOString();
filename = filename + "-tmctest.txt"
this.download(filename, testDataBuffer);
}
}
async testVac(){
if(!this.port?.writable){
this.modal.show("Cannot Write", "Cannot write to port. Have you connected?");
return false
}
let testDataBuffer = "";
const commandArrayLeft = [
"M260 A112 B1 S1",
"M260 A109",
"M260 B48",
"M260 B10",
"M260 S1"
]
const commandArrayRight = [
"M260 A112 B2 S1",
"M260 A109",
"M260 B48",
"M260 B10",
"M260 S1"
]
const delayVal = 100;
this.clearBuffer();
let msb, csb, lsb;
const regex = new RegExp('data:(..)');
//send command array
await this.send(commandArrayLeft);
await this.send(["M260 A109 B6 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
msb = currLine.match("data:(..)")[1];
testDataBuffer = testDataBuffer.concat("Left MSB - " + msb + "\n");
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B7 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
csb = currLine.match("data:(..)")[1];
testDataBuffer = testDataBuffer.concat("Left CSB - " + csb + "\n");
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B8 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
lsb = currLine.match("data:(..)")[1];
testDataBuffer = testDataBuffer.concat("Left LSB - " + lsb + "\n");
break
}
}
let leftVal = parseInt(msb+csb+lsb, 16);
if(leftVal & (1 << 23)){
leftVal = leftVal - 2**24
}
testDataBuffer = testDataBuffer.concat("Left Val - " + leftVal + "\n");
// NOW RIGHT SENSOR
this.clearBuffer();
//send command array
await this.send(commandArrayRight);
await this.send(["M260 A109 B6 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
msb = currLine.match("data:(..)")[1];
testDataBuffer = testDataBuffer.concat("Right MSB - " + msb + "\n");
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B7 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
csb = currLine.match("data:(..)")[1];
testDataBuffer = testDataBuffer.concat("Right CSB - " + csb + "\n");
break
}
}
this.clearBuffer();
await this.send(["M260 A109 B8 S1"]);
await this.send(["M261 A109 B1 S1"]);
await this.delay(delayVal);
console.log("current buffer length: ", this.receiveBuffer.length)
for (var i=0, x=this.receiveBuffer.length; i<x; i++) {
let currLine = this.receiveBuffer[i];
let result = regex.test(currLine);
if(result){
lsb = currLine.match("data:(..)")[1];
testDataBuffer = testDataBuffer.concat("Right LSB - " + lsb + "\n");
break
}
}
let rightVal = parseInt(msb+csb+lsb, 16);
if(rightVal & (1 << 23)){
rightVal = rightVal - 2**24
}
testDataBuffer = testDataBuffer.concat("Right Val - " + rightVal + "\n");
console.log(leftVal, rightVal)
let resp = await this.modal.show("Vacuum Sensor Test Complete", "Test is complete. Click OK to download test report.");
if(resp == true){
let filename = new Date().toISOString();
filename = filename + "-vactest.txt"
this.download(filename, testDataBuffer);
}
}
download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
}