From a3b8d3559771eafcc2f11ca58618bd68fb7344a4 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Sun, 24 Sep 2017 17:14:58 -0400 Subject: [PATCH] feat(open): Throw on incorrect baudrate option (#1347) This was causing a lot of issues, lets make it easier on people. --- examples/socketio/index.js | 2 +- lib/serialport.js | 4 ++++ test/serialport.js | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/socketio/index.js b/examples/socketio/index.js index f6204c74c..8de7b88ed 100644 --- a/examples/socketio/index.js +++ b/examples/socketio/index.js @@ -18,7 +18,7 @@ const serialport = require('serialport'); const SerialPort = serialport.SerialPort; const serial = new SerialPort('/dev/cu.usbmodem1411', { - baudrate: 9600, + baudRate: 9600, parser: SerialPort.parsers.byteLength(1) }); diff --git a/lib/serialport.js b/lib/serialport.js index bfed7b66d..c090d9525 100644 --- a/lib/serialport.js +++ b/lib/serialport.js @@ -123,6 +123,10 @@ function SerialPort(path, options, callback) { throw new TypeError(`"path" is not defined: ${path}`); } + if (settings.baudrate) { + throw new TypeError(`"baudrate" is an unknown option, did you mean "baudRate"?`); + } + if (typeof settings.baudRate !== 'number') { throw new TypeError(`"baudRate" must be a number: ${settings.baudRate}`); } diff --git a/test/serialport.js b/test/serialport.js index 45d29e494..2079f5121 100644 --- a/test/serialport.js +++ b/test/serialport.js @@ -95,6 +95,12 @@ describe('SerialPort', () => { } }); + it('throws an error when given bad baudrate even with a callback', function() { + assert.throws(() => { + this.port = new SerialPort('/dev/exists', { baudrate: 9600 }, () => {}); + }); + }); + it('errors with a non number baudRate', function(done) { try { this.port = new SerialPort('/bad/port', { baudRate: 'whatever' });