-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtellstick.js
118 lines (97 loc) · 2.9 KB
/
tellstick.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
// var ftdi = require('../ftdi');
//var ftdi = require('../index');
var ftdi = require('ftdi');
var fs = require('fs'), protocols = {};
//var dataToWrite = [0x04, 0x00, 0x02, 0x79, 0x40];
fs.readdirSync('./protocols/').forEach((protocolFile) => {
console.log('Protocol loaded:', protocolFile.substr(0, protocolFile.lastIndexOf('.')));
protocols[protocolFile.substr(0, protocolFile.lastIndexOf('.'))] = require('./protocols/' + protocolFile);
});
ftdi.find(0x1781, 0x0c31, function(status, devices) {
if(devices.length > 0) {
var device = new ftdi.FtdiDevice(devices[0]);
loop(device);
}
else {
console.log("No Device found");
}
});
var buf = Buffer.alloc(0);
var loop = function(device) {
device.on('error', function(error) {
console.log("Error: " + error);
});
device.on('data', function(data) {
console.log('Output: ', data.length, data.toString('hex'));
buf = Buffer.concat([buf, data]);
console.log('Buffer:', buf.toString('hex'));
if(buf.indexOf(10) > -1) {
var offset = 0;
console.log('test', buf.indexOf(10));
// buf = Buffer.from(buf.slice(buf.indexOf(10) + 1));
while(buf.indexOf(0x2b, offset) > -1 && offset < 5) {
var msg = Buffer.from(buf.slice(buf.indexOf(0x2b, offset), buf.indexOf(13)));
console.log('Fragment:', msg.toString('utf8'));
switch(msg.slice(0, 2).toString()) {
case '+V':
console.log('Firmware version:', msg.slice(2).toString());
buf = Buffer.from(buf.slice(buf.indexOf(10) + 1))
break;
case '+W':
console.log('data', msg.toString());
var params = {};
msg.slice(2).toString().split(';')
.filter((keyval) => keyval.length != '')
.forEach(function(keyval) {
console.log(keyval);
var tmp = keyval.split(':');
params[tmp[0]] = tmp[1];
});
if(protocols[params.protocol]) {
console.log(params.protocol, protocols[params.protocol].decodeData(params));
}
buf = Buffer.from(buf.slice(buf.indexOf(10) + 1))
break;
default:
console.log('Default, data', msg.toString(), msg.toString('hex'));
offset++;
break;
}
}
}
device.close(function(status) {
console.log("JS Close Device");
//setTimeout(function() {loop(device);}, 5000);
loop(device);
});
});
device.open({
baudrate: device.deviceSettings.productId == 0x0C31 ? 9600 : 4800,
databits: 8,
stopbits: 1,
parity: 'none'
}, function(status) {
console.log("Connected..");
// device.write(dataToWrite);
});
}
// var device = ftdi.Ftdi();
// var devices = ftdi.find(0x18d9, 0x01a0, function() {});
// console.log( devices );
// device.on('data', function(data)
// {
// console.log('Output:');
// console.log( data );
// });
// if(devices.length > 0)
// {
// device.open(devices[0].serial);
// setInterval(function()
// {
// device.write(dataToWrite);
// }, 2000);
// }
// else
// {
// console.log("No Device found");
// }