Skip to content

Commit 0bfa54f

Browse files
committed
Add better error handling (clear message) when opening port fails
1 parent 960b379 commit 0bfa54f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

lib/Controller.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ function Controller(settings) {
3333
parser : SerialPort.parsers.readline('\n')
3434
});
3535

36-
self.port.on('open', self._onPortOpen.bind(self));
37-
self.port.on('data', self._onPortData.bind(self));
36+
self.port.on('open' , self._onPortOpen.bind(self));
37+
self.port.on('error', self._onPortError.bind(self));
38+
self.port.on('data' , self._onPortData.bind(self));
3839

3940
self._isFirstLine = true;
4041

@@ -216,6 +217,20 @@ Controller.prototype._onPortOpen = function() {
216217
});
217218
};
218219

220+
Controller.prototype._onPortError = function(err) {
221+
var self = this;
222+
223+
console.error(
224+
'Serial port error: %s',
225+
err.message
226+
);
227+
console.error(
228+
'Check the serial device name and make sure it\'s connected.'
229+
);
230+
console.error();
231+
throw err;
232+
};
233+
219234
Controller.prototype._onPortData = function(line) {
220235
var self = this;
221236

0 commit comments

Comments
 (0)