This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
port.js
96 lines (91 loc) · 2.54 KB
/
port.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
module.exports = function(app) {
var event = app.event, com = require("serialport"), utils = require('./utils.js'), config = require('./config.js'), port = {};
// begin port setup
var portRetry = setTimeout(portRetryFunc, 5000);
function portRetryFunc() {
utils.logText('Serial port unavailable', 'ERR');
if (config.dev) {
com.list(function(err, ports) {
if (config.dev)
utils.logText('List of available ports:');
ports.forEach(function(testport) {
console.log('Port name: ' + testport.comName);
console.log('Port pnpid: ' + testport.pnpId);
console.log('Port mfgr: ' + testport.manufacturer);
console.log();
});
});
}
portRetry = setInterval(portConnect, 5000);
portConnect();
}
function portConnect() {
com
.list(function(err, ports) {
ports
.forEach(function(testport) {
if (testport.comName == config.port.name
|| (testport.pnpId
.indexOf(config.port.pid) >= 0 && testport.pnpId
.indexOf(config.port.vid) >= 0)) {
port = new com.SerialPort(testport.comName,
{
baudrate : config.baud,
parser : com.parsers.raw
}, false)
.on(
'open',
function() {
clearInterval(portRetry);
clearTimeout(portRetry);
event.emit(
'port-start',
port);
utils
.logText('Serial port '
+ testport.comName
+ ' opened');
})
.on(
'close',
function(data) {
event.emit('port-stop');
portRetry = setTimeout(
portRetryFunc,
5000);
utils
.logText(
'Serial port '
+ testport.comName
+ ' closed',
'ERR');
})
.on(
'error',
function(data) {
utils
.logText(
'Serial port '
+ testport.comName
+ ' error: '
+ data,
'ERR');
})
.on(
'data',
function(buf) {
utils
.logText('Serial data read - '
+ buf.length
+ ' bytes');
event.emit('port-read',
buf);
});
port.open();
}
});
});
}
portConnect();
return port;
}