-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialDevices.js
310 lines (282 loc) · 10.8 KB
/
serialDevices.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
var exports = module.exports = {};
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var parsers = serialport.parsers;
var atob = require('atob');
var btoa = require('btoa');
var when = require('when');
var debug = require('debug')('eve-connector:serial');
// polling USB interfaces
var _pollingUsb = [];
var checkDeviceType = function(device)
{
if (!device)
throw ('device is undefined')
if ( device.type != 'serial' )
throw (['Device type should be "serial"', device])
if ( ! device.params )
throw (['Device params is empty', device])
if ( !device.params.pnpId && !device.params.comName )
throw (['You must provide pnpId or comName parameters for serial devices', device.params]);
}
var listDevices = function(type)
{
return when.promise(function(resolve, reject){
serialport.list(function (err, ports) {
if (err) reject(err);
else resolve(ports);
});
});
};
var isDeviceAvailable = function(device)
{
return when.promise(function(resolve, reject){
debug('isDeviceAvailable()');
checkDeviceType(device);
serialport.list(function (err, ports) {
if (err)
reject(err);
else {
var comName = device.params.comName;
var pnpId = device.params.pnpId;
var available = ports.find(function(port){
if (comName && pnpId && comName === port.comName && port.pnpId.includes(pnpId))
return true;
if (comName && comName === port.comName)
return true;
if (pnpId &&port.pnpId.includes(pnpId))
return true;
return false;
});
resolve({available: available, device: device});
}
});
});
}
var areDevicesAvailable = function(type, devicesList)
{
debug('areDevicesAvailable()');
var available = { type: type, params: []};
var checks = [];
devicesList.forEach(function(device){
var check = when.promise(function(resolve, reject){
serialport.list(function (err, ports) {
if (err)
reject(err);
else {
var comName = device.comName;
var pnpId = device.pnpId;
var found = ports.find(function(port){
if (comName && pnpId && comName === port.comName && port.pnpId && port.pnpId.includes(pnpId)) {
return true;
}
if (comName && comName === port.comName) {
return true;
}
if (pnpId && port.pnpId && port.pnpId.includes(pnpId)) {
return true;
}
return false;
});
if (found) {
available.params.push(found);
}
resolve(available);
}
});
});
checks.push(check);
});
return when.all(checks);
}
var resetData = function(device, socket)
{
//usb.setDebugLevel(4);
return when.promise(function(resolve, reject){
debug('Resetting device (TODO)...');
resolve('device reset (fake)');
});
};
var sendData = function(device, data, socket)
{
debug('sendData()');
if ( Array.isArray(data.writes) && Array.isArray(data.reads) )
return doTransaction(device, data);
return getComName(device)
.then(function(comName){
return when.promise(function(resolve, reject){
debug('sending data to comName=' + comName);
var port = new SerialPort(comName, {
baudrate: device.params.baudrate ? device.params.baudrate : 9600,
//parser: serialport.parsers.byteLength(42), // TODO: this is specific to SCD122U
dataBits: device.params.databits ? device.params.databits : 8,
parity: device.params.parity ? device.params.parity : 'none',
stopBits: device.params.stopbits ? device.params.stopbits : 1
});
var readAfterSend = device.params.readAfterSend;
port.on('open', function() {
data = new Buffer(atob(data.toString()));
port.write(data, function(err) {
if (err) {
debug('Error on write: ', err.message);
port.close();
reject(err);
}
debug('Message written: ' + data);
if ( !readAfterSend ) {
port.close();
// we send back base64 encoded data
resolve(data != undefined ? btoa(data) : '');
}
});
});
port.on('data', function(data) {
debug('got data after send:' + data , data.toString().charCodeAt(0));
if ( readAfterSend ) {
port.close();
// we send back base64 encoded data
resolve(data != undefined ? btoa(data) : '');
}
});
// open errors will be emitted as an error event
port.on('error', function(err) {
debug('serial port Error: ', err.message);
reject(err);
});
});
});
};
var readData = function(device, length)
{
debug('readData()');
return getComName(device)
.then(function(comName){
return when.promise(function(resolve, reject){
debug('reading data from comName=' + comName);
var port = new SerialPort(comName, {
baudrate: device.params.baudrate ? device.params.baudrate : 9600,
//parser: serialport.parsers.byteLength(42), // TODO: this is specific to SCD122U
dataBits: device.params.databits ? device.params.databits : 8,
parity: device.params.parity ? device.params.parity : 'none',
stopBits: device.params.stopbits ? device.params.stopbits : 1
});
port.on('data', function(data) {
debug('got data:' + data , data.toString().charCodeAt(0));
port.close();
// we send back base64 encoded data
resolve(data != undefined ? btoa(data) : '');
});
port.on('error', function(err) {
debug('serial port Error: ', err.message);
reject(err);
});
});
})
}
var doTransaction = function(device, data)
{
debug('doTransaction()');
var writes = data.writes || [];
var reads = data.reads || [];
var result = '';
return getComName(device).then(function(comName){
return when.promise(function(resolve, reject){
debug('Start transaction with comName=' + comName);
debug('writes = ' + writes);
debug('reads = ' + reads);
var port = new SerialPort(comName, {
baudrate: device.params.baudrate ? device.params.baudrate : 9600,
//parser: serialport.parsers.byteLength(42), // TODO: this is specific to SCD122U
dataBits: device.params.databits ? device.params.databits : 8,
parity: device.params.parity ? device.params.parity : 'none',
stopBits: device.params.stopbits ? device.params.stopbits : 1
}, false);
var done = function() {
if ( port.isOpen() ) port.close();
// we send back base64 encoded data
resolve(result != undefined ? btoa(result) : '');
}
var write = function() {
if ( writes.length > 0 ) {
var data = writes.shift();
data = new Buffer(atob(data.toString()));
port.write(data, function(err) {
if (err) {
debug('Error on write: ', err.message);
if ( port.isOpen() ) port.close();
reject(err);
}
debug('Message written: ' + data);
if ( reads.length == 0 ) {
if ( writes.length == 0 ) done();
else write();
}
});
}
else if ( reads.length == 0 ) done();
}
if ( port.isOpen() )
reject('Port is already open');
else port.open(function(err){
if ( err ) {
debug('serial port open Error: ', err.message);
reject(err);
}
else write();
});
port.on('data', function(data) {
debug('got data:' + data , data.toString().charCodeAt(0));
var found = false;
if ( reads.length > 0 ) {
var next = atob(reads[0]);
debug('next:' + next , next.toString().charCodeAt(0));
if ( next == '*' ) {
result = data;
found = true;
}
if ( next == data )
found = true;
}
if ( found ) {
reads.shift();
write();
}
if ( reads.length == 0 && writes.length == 0 ) done();
});
// open errors will be emitted as an error event
port.on('error', function(err) {
debug('serial port Error: ', err.message);
if ( port.isOpen() ) port.close();
reject(err);
});
});
});
};
/**
* returns the comName of the first device having a pnpId containing pnpId
*/
var getComName = function(device)
{
return when.promise(function(resolve, reject) {
if (device.params.comName)
resolve(device.params.comName)
else serialport.list(function (err, ports) {
if (err)
reject(err.msg);
else {
var pnpId = device.params.pnpId;
var found = ports.find(function(port){
return pnpId && port.pnpId.includes(pnpId);
});
if (found) resolve(found.comName);
else reject('Device not found for pnpId ' + pnpId);
}
});
});
}
exports.isDeviceAvailable = isDeviceAvailable;
exports.areDevicesAvailable = areDevicesAvailable;
exports.listDevices = listDevices;
exports.sendData = sendData;
exports.readData = readData;
//exports.test = test;