-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
46 lines (42 loc) · 1.43 KB
/
renderer.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
const serialport = require('serialport');
const $portsSelect = document.querySelector('select#ports');
const $colorViewer = document.querySelector('#color-viewer');
const $baudrateSelect = document.querySelector('select#baudrate');
const $openSerialButton = document.querySelector('button#open-serial');
let activeSerial;
let message = '';
$openSerialButton.addEventListener('click', () => {
const port = $portsSelect.value;
const baudrate = $baudrateSelect.value >>> 0;
if(activeSerial) activeSerial.close();
activeSerial = serialport(port, { baudRate: baudrate });
activeSerial.on('data', (chunk) => {
const data = String(chunk);
const index = data.indexOf('\n');
if(data.includes('\n')) {
message += data;
console.log(message);
$colorViewer.style.backgroundColor = message;
message = '';
} else {
message += data;
}
});
});
serialport.list((err, ports) => {
if(err) {
document.getElementById('error').textContent = err.message
return
} else if(ports.length === 0) {
document.getElementById('error').textContent = 'No ports discovered'
} else {
document.getElementById('error').textContent = ''
console.log('ports', ports);
ports.filter(port => port.serialNumber).forEach(port => {
const $option = document.createElement('option');
$option.value = port.comName;
$option.label = port.comName;
$portsSelect.appendChild($option);
});
}
});