-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
69 lines (59 loc) · 1.51 KB
/
test.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
/*
THIS FILE IS NOT FUNCTIONAL
*/
const comms = require('./index.js');
const serial = require('serialport');
var inputHandler;
var validDevices = [];
var device;
var commandType;
process.stdin.on('data', function (text) {
process.stdin.pause();
inputHandler(text);
});
function getDevices(){
console.log('getting devices');
return new Promise((fulfill, reject) => {
serial.list().then(devices => {
console.log('list returned');
devices.forEach((d) => {
if(d.manufacturer == 'FTDI') validDevices.push(d);
});
if(validDevices.length == 0) reject('No devices found');
else fulfill(validDevices);
}).catch(err => {
console.log(err);
reject(err);
});
});
}
function setDevice(input){
var i = parseInt(input);
device = new comms.NcdSerial(validDevices[i-1].path, 115200);
selectCommandType();
}
function selectDevice(input){
getDevices().then(() => {
inputHandler = setDevice;
validDevices.forEach((d, i) => {
console.log(" "+(i+1)+': '+d.serialNumber);
});
console.log('Please select a serial device 1-'+devices.length+': ');
process.stdin.resume();
process.stdin.setEncoding('utf8');
}).catch((err) => {
console.log(err);
});
}
function setCommandType(input){
commandType = parseInt(input) == 1 ? 'write' : parseInt(input) == 2 ? 'read' : false;
}
function selectCommandType(){
inputHandler = setCommandType;
process.stdin.resume();
process.stdin.setEncoding('utf8');
console.log('Select Command Type: ');
console.log(' 1: write');
console.log(' 2: read');
}
selectDevice();