-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerialReader.js
174 lines (156 loc) · 5.49 KB
/
SerialReader.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
"use strict";
/**
* Read Serial port, parse NMEA data
*
* Doc at https://www.npmjs.com/package/serialport
*/
let SerialPort = require('serialport');
let NMEAParser = require('./NMEAParser.js');
let fullContext = {};
function NMEA(serial, br) {
let instance = this;
if (serial === undefined) {
serial = '/dev/ttyUSB0'; // Linux
}
if (br === undefined) {
br = 4800;
}
console.log(">>> Reading Serial", serial, "br", br);
let port = new SerialPort(serial, {
baudRate: br,
parser: SerialPort.parsers.raw
});
port.on('open', function() {
console.log('Port open');
});
let dataBuffer = Buffer.from("");
let EOS = "\r\n";
function isSentenceCompleted(str) {
let ok = false;
if (str.charAt(0) === '$') {
if (str.charAt(str.length - 3) === '*') {
ok = true;
}
}
return ok;
}
port.on('data', (data) => {
try {
dataBuffer = Buffer.concat([dataBuffer, data]);
let stringBuffer = dataBuffer.toString();
let sentences = stringBuffer.split(EOS);
for (let i=0; i<sentences.length; i++) {
if (sentences[i].charAt(0) === '$') {
if (isSentenceCompleted(sentences[i])) {
try {
let id = NMEAParser.validate(sentences[i]); // Validation!
if (id !== undefined) {
if (global.displayMode === 'fmt') {
switch (id.id) {
case 'RMC':
let rmc = NMEAParser.parseRMC(sentences[i]);
if (rmc !== undefined) {
// console.log("RMC:", rmc);
if (instance.onPosition !== undefined) {
if (rmc.pos !== undefined) {
instance.onPosition(rmc.pos);
}
}
if (instance.onTime !== undefined) {
if (rmc.epoch !== undefined) {
instance.onTime(rmc.epoch);
}
}
if (instance.onCOG !== undefined) {
if (rmc.cog !== undefined) {
instance.onCOG(rmc.cog);
}
}
if (instance.onSOG !== undefined) {
if (rmc.sog !== undefined) {
instance.onSOG(rmc.sog);
}
}
}
break;
default:
// console.log(">> Sentence: " + sentences[i] + " not managed yet");
break;
}
} else if (global.displayMode === 'auto') {
try {
let str = sentences[i];
let auto = NMEAParser.autoparse(str);
try {
if (auto !== undefined && auto.type !== undefined) {
// console.log(">> Autoparsed:" + auto.type);
fullContext.lastID = auto.type;
switch (auto.type) {
case "GSV":
if (auto.satData !== undefined) {
fullContext.nbSat = auto.satData.length;
fullContext.satellites = auto.satData;
}
break;
case "RMC":
fullContext.date = new Date(auto.epoch);
fullContext.latitude = auto.pos.lat;
fullContext.longitude = auto.pos.lon;
fullContext.cog = auto.cog;
fullContext.sog = auto.sog;
break;
case "GGA":
fullContext.date = new Date(auto.epoch);
fullContext.latitude = auto.position.latitude;
fullContext.longitude = auto.position.longitude;
fullContext.altitude = auto.antenna.altitude;
break;
}
if (instance.onFullGPSData !== undefined) {
// console.log(">>> GPS Data sent to client:", fullContext);
instance.onFullGPSData(fullContext);
} else {
console.log(">>> NMEA:", JSON.stringify(fullContext));
}
}
} catch (err) {
console.log(err);
console.log("AutoParsed:", auto);
}
} catch (err) {
console.log(">> Error:", err);
}
} else {
console.log(sentences[i]);
}
}
} catch (err) {
console.log("Argh!", err);
}
} else {
dataBuffer = Buffer.from(sentences[i]);
}
}
}
} catch (err) {
console.log('Oops');
}
});
// open errors will be emitted as an error event
port.on('error', (err) => {
console.log('Error: ', err.message);
});
port.on('close', () => {
console.log('Bye');
});
this.exit = () => {
port.close();
};
this.onPosition;
this.onTime;
this.onCOG;
this.onSOG;
this.onFullGPSData;
}
// Made public.
exports.NMEA = NMEA;