From 623cbf2bb76ec62572a8995bdc18a03a8277db67 Mon Sep 17 00:00:00 2001 From: AJ Keller Date: Sat, 9 Jul 2016 22:48:29 -0400 Subject: [PATCH] Saving for the night --- openBCIBoard.js | 105 +++++++++++++----- openBCISimulator.js | 26 ++++- test/openBCIBoard-test.js | 203 ++++++++++++++++++++++++++++++++-- test/openBCISimulator-test.js | 86 +++++++++++++- 4 files changed, 378 insertions(+), 42 deletions(-) diff --git a/openBCIBoard.js b/openBCIBoard.js index 8dcbcea..cbe968f 100644 --- a/openBCIBoard.js +++ b/openBCIBoard.js @@ -251,11 +251,11 @@ function OpenBCIFactory() { if(this.options.verbose) console.log('Serial port connected'); - boardSerial.on('data',(data) => { + boardSerial.on('data',data => { this._processBytes(data); }); this.connected = true; - boardSerial.on('open',() => { + boardSerial.once('open',() => { var timeoutLength = this.options.simulate ? 50 : 300; if(this.options.verbose) console.log('Serial port open'); setTimeout(() => { @@ -271,12 +271,12 @@ function OpenBCIFactory() { },timeoutLength + 250); }); - boardSerial.on('close',() => { + boardSerial.once('close',() => { if (this.options.verbose) console.log('Serial Port Closed'); this.emit('close') }); /* istanbul ignore next */ - boardSerial.on('error',(err) => { + boardSerial.once('error',(err) => { if (this.options.verbose) console.log('Serial Port Error'); this.emit('error',err); }); @@ -545,7 +545,7 @@ function OpenBCIFactory() { * exist and thus this method will reject. If the board is using firmware 2+ then this function should resolve. * **Note**: This functionality requires OpenBCI Firmware Version 2.0 * @since 1.0.0 - * @returns {Number} - The new channel number. + * @returns {Promise} - Resolves with the new channel number, rejects with err. * @author AJ Keller (@pushtheworldllc) */ OpenBCIBoard.prototype.radioChannelSet = function(channelNumber) { @@ -567,6 +567,7 @@ function OpenBCIFactory() { // Subscribe to the EOT event this.once('eot',data => { + if (this.options.verbose) console.log(data.toString()); // Remove the timeout! clearTimeout(badCommsTimeout); badCommsTimeout = null; @@ -585,6 +586,53 @@ function OpenBCIFactory() { }); }; + /** + * @description Used to query the OpenBCI system for its radio channel number. The function will reject if not + * connected to the serial port of the dongle. Further the function should reject if currently streaming. + * Lastly and more important, if the board is not running the new firmware then this functionality does not + * exist and thus this method will reject. If the board is using firmware 2+ then this function should resolve. + * **Note**: This functionality requires OpenBCI Firmware Version 2.0 + * @since 1.0.0 + * @returns {Promise} - Resolves with the new channel number, rejects with err. + * @author AJ Keller (@pushtheworldllc) + */ + OpenBCIBoard.prototype.radioChannelSetHostOverride = function(channelNumber) { + var badCommsTimeout; + return new Promise((resolve,reject) => { + if (!this.connected) return reject("Must be connected to Dongle. Pro tip: Call .connect()"); + if (this.streaming) return reject("Don't query for the radio while streaming"); + if (channelNumber === undefined || channelNumber === null) return reject("Must input a new channel number to switch too!"); + if (!k.isNumber(channelNumber)) return reject("Must input type Number"); + if (channelNumber > k.OBCIRadioChannelMax) return reject(`New channel number must be less than ${k.OBCIRadioChannelMax}`); + if (channelNumber < k.OBCIRadioChannelMin) return reject(`New channel number must be greater than ${k.OBCIRadioChannelMin}`); + + // Set a timeout. Since poll times can be max of 255 seconds, we should set that as our timeout. This is + // important if the module was connected, not streaming and using the old firmware + badCommsTimeout = setTimeout(() => { + reject("Please make sure your dongle is using firmware v2"); + }, 1000); + + // Subscribe to the EOT event + this.once('eot',data => { + if (this.options.verbose) console.log(`${data.toString()}`); + // Remove the timeout! + clearTimeout(badCommsTimeout); + badCommsTimeout = null; + + if (openBCISample.isSuccessInBuffer(data)) { + resolve(data[data.length - 4]); + } else { + reject(`Error [radioChannelSet]: ${data}`); // The channel number is in the first byte + } + }); + + this.curParsingMode = k.OBCIParsingEOT; + + // Send the radio channel query command + this._writeAndDrain(new Buffer([k.OBCIRadioKey,k.OBCIRadioCmdChannelSetOverride,channelNumber])); + }); + }; + /** * @description Used to query the OpenBCI system for it's radio channel number. The function will reject if not * connected to the serial port of the dongle. Further the function should reject if currently streaming. @@ -613,6 +661,7 @@ function OpenBCIFactory() { // Subscribe to the EOT event this.once('eot',data => { + if (this.options.verbose) console.log(data.toString()); // Remove the timeout! clearTimeout(badCommsTimeout); badCommsTimeout = null; @@ -659,13 +708,13 @@ function OpenBCIFactory() { // Subscribe to the EOT event this.once('eot',data => { + if (this.options.verbose) console.log(data.toString()); // Remove the timeout! clearTimeout(badCommsTimeout); badCommsTimeout = null; if (openBCISample.isSuccessInBuffer(data)) { - var pollTime = data[data.length - 3]; - console.log(`pollTime is ${pollTime}`); + var pollTime = data[data.length - 4]; resolve(pollTime); } else { reject(`Error [radioPollTimeGet]: ${data}`); // The channel number is in the first byte @@ -688,7 +737,7 @@ function OpenBCIFactory() { * function should resolve. * **Note**: This functionality requires OpenBCI Firmware Version 2.0 * @since 1.0.0 - * @returns {Promise} - With a {Buffer} that contains the success message. + * @returns {Promise} - Resolves with new poll time, rejects with error message. * @author AJ Keller (@pushtheworldllc) */ OpenBCIBoard.prototype.radioPollTimeSet = function (pollTime) { @@ -710,12 +759,13 @@ function OpenBCIFactory() { // Subscribe to the EOT event this.once('eot',data => { + if (this.options.verbose) console.log(data.toString()); // Remove the timeout! clearTimeout(badCommsTimeout); badCommsTimeout = null; if (openBCISample.isSuccessInBuffer(data)) { - resolve(data); + resolve(data[data.length - 4]); // Ditch the eot $$$ } else { reject(`Error [radioPollTimeSet]: ${data}`); // The channel number is in the first byte } @@ -759,10 +809,18 @@ function OpenBCIFactory() { // Subscribe to the EOT event this.once('eot',data => { + if (this.options.verbose) console.log(data.toString()); // Remove the timeout! clearTimeout(badCommsTimeout); badCommsTimeout = null; - var newBaudRateBuf = data.slice(data.length - 9, data.length - 3); + var eotBuf = new Buffer('$$$'); + var newBaudRateBuf; + for (var i = data.length; i > 3; i--) { + if (bufferEqual(data.slice(i - 3, i),eotBuf)) { + newBaudRateBuf = data.slice(i - 9, i - 3); + break; + } + } var newBaudRateNum = Number(newBaudRateBuf.toString()); if (newBaudRateNum !== k.OBCIRadioBaudRateDefault && newBaudRateNum !== k.OBCIRadioBaudRateFast) { return reject("Error parse mismatch, restart your system!"); @@ -770,22 +828,15 @@ function OpenBCIFactory() { if (openBCISample.isSuccessInBuffer(data)) { // Change the sample rate here if (this.options.simulate === false) { - if (this.serial.isOpen()) { - this.serial.close(() => { - this.serial = new serialPort.SerialPort(this.portName, { - baudRate: newBaudRateNum - },(err) => { - if (err) { - this.connected = false; - reject(err); - } else { - this.connected = true; - this.options.baudRate = newBaudRateNum; - resolve(newBaudRateNum); - } - }); - }); - } + this.serial.update({ + baudRate: newBaudRateNum + }, err => { + if (err) { + reject(err); + } else { + resolve(newBaudRateNum); + } + }); } else { resolve(newBaudRateNum); } @@ -835,6 +886,8 @@ function OpenBCIFactory() { clearTimeout(badCommsTimeout); badCommsTimeout = null; + if (this.options.verbose) console.log(data.toString()); + if (openBCISample.isSuccessInBuffer(data)) { resolve(true); } else { diff --git a/openBCISimulator.js b/openBCISimulator.js index 5f85c1f..ae10d39 100644 --- a/openBCISimulator.js +++ b/openBCISimulator.js @@ -73,6 +73,7 @@ function OpenBCISimulatorFactory() { this.buffer = new Buffer(500); // Numbers this.channelNumber = 1; + this.hostChannelNumber = this.channelNumber; this.pollTime = 80; this.sampleNumber = -1; // So the first sample is 0 // Objects @@ -260,6 +261,7 @@ function OpenBCISimulatorFactory() { if (!this.options.boardFailure) { if (dataBuffer[2] < k.OBCIRadioChannelMax) { this.channelNumber = dataBuffer[2]; + this.hostChannelNumber = this.channelNumber; this._printSuccess(); this.emit('data', new Buffer(`Channel Number ${this.channelNumber}`)); this.emit('data', new Buffer([this.channelNumber])); @@ -274,6 +276,26 @@ function OpenBCISimulatorFactory() { } } break; + case k.OBCIRadioCmdChannelSetOverride: + if (this.options.firmwareVersion === k.OBCIFirmwareV2) { + if (dataBuffer[2] < k.OBCIRadioChannelMax) { + if (dataBuffer[2] === this.channelNumber) { + this.options.boardFailure = false; + } else { + this.options.boardFailure = true; + } + this.hostChannelNumber = dataBuffer[2]; + this._printSuccess(); + this.emit('data', new Buffer(`Host override - Channel Number ${this.hostChannelNumber}`)); + this.emit('data', new Buffer([this.hostChannelNumber])); + this._printEOT(); + } else { + this._printFailure(); + this.emit('data', new Buffer("Verify channel number is less than 25")); + this._printEOT(); + } + } + break; case k.OBCIRadioCmdPollTimeGet: if (this.options.firmwareVersion === k.OBCIFirmwareV2) { if (!this.options.boardFailure) { @@ -303,14 +325,14 @@ function OpenBCISimulatorFactory() { if (this.options.firmwareVersion === k.OBCIFirmwareV2) { this._printSuccess(); this.emit('data', new Buffer("Switch your baud rate to 115200")); - this._printEOT(); + this.emit('data', new Buffer([0x24,0x24,0x24,0xFF])); // The board really does this } break; case k.OBCIRadioCmdBaudRateSetFast: if (this.options.firmwareVersion === k.OBCIFirmwareV2) { this._printSuccess(); this.emit('data', new Buffer("Switch your baud rate to 230400")); - this._printEOT(); + this.emit('data', new Buffer([0x24,0x24,0x24,0xFF])); // The board really does this } break; case k.OBCIRadioCmdSystemStatus: diff --git a/test/openBCIBoard-test.js b/test/openBCIBoard-test.js index 4a767c3..a2d1897 100644 --- a/test/openBCIBoard-test.js +++ b/test/openBCIBoard-test.js @@ -1552,7 +1552,6 @@ describe('openbci-sdk',function() { }); }).catch(err => done(err)); }); - it("should reject if not firmware version 2", done => { ourBoard = new openBCIBoard.OpenBCIBoard({ verbose : true, @@ -1565,7 +1564,6 @@ describe('openbci-sdk',function() { }); }).catch(err => done(err)); }); - it("should reject if a number is not sent as input", done => { ourBoard = new openBCIBoard.OpenBCIBoard({ verbose : true, @@ -1578,7 +1576,6 @@ describe('openbci-sdk',function() { ourBoard.radioChannelSet('1').should.be.rejected.and.notify(done); }); }).catch(err => done(err)); - }); it("should reject if no channel number is presented as arg", done => { @@ -1656,7 +1653,114 @@ describe('openbci-sdk',function() { }).catch(err => done(err)); }); }); - + describe('#radioChannelSetHostOverride', function() { + afterEach(done => { + if (ourBoard.connected) { + ourBoard.disconnect().then(() => { + done(); + }).catch(() => done); + } else { + done() + } + }); + it("should not change the channel number if not connected", done => { + ourBoard = new openBCIBoard.OpenBCIBoard({ + verbose : true, + simulate : true, + simulatorFirmwareVersion: 'v2' + }); + ourBoard.radioChannelSetHostOverride().should.be.rejected.and.notify(done); + }); + it("should reject if streaming", done => { + ourBoard = new openBCIBoard.OpenBCIBoard({ + verbose : true, + simulate : true, + simulatorFirmwareVersion: 'v2' + }); + ourBoard.connect(k.OBCISimulatorPortName) + .then(() => { + ourBoard.once('ready', () => { + ourBoard.streamStart() + .then(() => { + ourBoard.radioChannelSetHostOverride(1).then(() => { + done("should have rejected"); + }).catch(() => { + done(); // Test pass + }) + }).catch(err => done(err)); + }); + }).catch(err => done(err)); + }); + it("should reject if a number is not sent as input", done => { + ourBoard = new openBCIBoard.OpenBCIBoard({ + verbose : true, + simulate : true, + simulatorFirmwareVersion: 'v2' + }); + ourBoard.connect(k.OBCISimulatorPortName) + .then(() => { + ourBoard.once('ready', () => { + ourBoard.radioChannelSetHostOverride('1').should.be.rejected.and.notify(done); + }); + }).catch(err => done(err)); + }); + it("should reject if no channel number is presented as arg", done => { + ourBoard = new openBCIBoard.OpenBCIBoard({ + verbose : true, + simulate : true, + simulatorFirmwareVersion: 'v2' + }); + ourBoard.connect(k.OBCISimulatorPortName) + .then(() => { + ourBoard.once('ready', () => { + ourBoard.radioChannelSetHostOverride().should.be.rejected.and.notify(done); + }); + }).catch(err => done(err)); + }); + it("should reject if the requested new channel number is lower than 0", done => { + ourBoard = new openBCIBoard.OpenBCIBoard({ + verbose : true, + simulate : true, + simulatorFirmwareVersion: 'v2' + }); + ourBoard.connect(k.OBCISimulatorPortName) + .then(() => { + ourBoard.once('ready', () => { + ourBoard.radioChannelSetHostOverride(-1).should.be.rejected.and.notify(done); + }); + }).catch(err => done(err)); + }); + it("should reject if the requested new channel number is higher than 25", done => { + ourBoard = new openBCIBoard.OpenBCIBoard({ + verbose : true, + simulate : true, + simulatorFirmwareVersion: 'v2' + }); + ourBoard.connect(k.OBCISimulatorPortName) + .then(() => { + ourBoard.once('ready', () => { + ourBoard.radioChannelSetHostOverride(26).should.be.rejected.and.notify(done); + }); + }).catch(err => done(err)); + }); + it("should change the channel if connected, not steaming, and using firmware version 2+",done => { + var newChannelNumber = 2; + ourBoard = new openBCIBoard.OpenBCIBoard({ + verbose : true, + simulate : true, + simulatorFirmwareVersion: 'v2' + }); + ourBoard.connect(k.OBCISimulatorPortName) + .then(() => { + ourBoard.once('ready', () => { + ourBoard.radioChannelSetHostOverride(newChannelNumber).then(channelNumber => { + expect(channelNumber).to.be.equal(newChannelNumber); + done(); + }).catch(err => done(err)); + }); + }).catch(err => done(err)); + }); + }); describe('#radioChannelGet', function() { afterEach(done => { if (ourBoard.connected) { @@ -2150,10 +2254,9 @@ describe('openbci-sdk',function() { }); describe('#radioTestsWithBoard',function() { - this.timeout(2000); + this.timeout(3000); before(done => { ourBoard = new openBCIBoard.OpenBCIBoard({ - simulate: !realBoard, verbose: true }); ourBoard.connect(masterPortName).catch(err => done(err)); @@ -2175,28 +2278,104 @@ describe('openbci-sdk',function() { // Don't test if not using real board if (!realBoard) return done(); // The channel number should be between 0 and 25. Those are hard limits. - ourBoard.radioChannelGet().should.eventually.be.between(0,25); + ourBoard.radioChannelGet().then(res => { + expect(res.channelNumber).to.be.within(0,25); + done(); + }).catch(err => done(err)); + }); + it("should be able to set the channel to 1", done => { + // Don't test if not using real board + if (!realBoard) return done(); + ourBoard.radioChannelSet(1).then(channelNumber => { + expect(channelNumber).to.equal(1); + done(); + }).catch(err => done(err)); + }); + it("should be able to set the channel to 2", done => { + // Don't test if not using real board + if (!realBoard) return done(); + ourBoard.radioChannelSet(2).then(channelNumber => { + expect(channelNumber).to.equal(2); + done(); + }).catch(err => done(err)); + }); + it("should be able to set the channel to 25", done => { + // Don't test if not using real board + if (!realBoard) return done(); + ourBoard.radioChannelSet(25).then(channelNumber => { + expect(channelNumber).to.equal(25); + done(); + }).catch(err => done(err)); + }); + it("should be able to set the channel to 0", done => { + // Don't test if not using real board + if (!realBoard) return done(); + ourBoard.radioChannelSet(0).then(channelNumber => { + expect(channelNumber).to.equal(0); + done(); + }).catch(err => done(err)); + }); + it("should be able to override host channel number, verify system is down, set the host back and verify system is up", done => { + // Don't test if not using real board + if (!realBoard) return done(); + var systemChanNumber = 0; + var newChanNum = 0; + // Get the current system channel + ourBoard.radioChannelGet() + .then(res => { + // Store it + systemChanNumber = res.channelNumber; + if (systemChanNumber == 25) { + newChanNum = 24; + } else { + newChanNum = systemChanNumber + 1; + } + // Call to change just the host + return ourBoard.radioChannelSetHostOverride(newChanNum); + }) + .then(newChanNumActual => { + expect(newChanNumActual).to.equal(newChanNum); + return ourBoard.radioSystemStatusGet(); + }) + .then(isUp => { + expect(isUp).to.be.false; + return ourBoard.radioChannelSetHostOverride(systemChanNumber); // Set back to good + }) + .then(newChanNumActual => { + // Verify we set it back to normal + expect(newChanNumActual).to.equal(systemChanNumber); + return ourBoard.radioSystemStatusGet(); + }) + .then(isUp => { + expect(isUp).to.be.true; + done(); + }) + .catch(err => done(err)); }); it("should be able to get the poll time", done => { // Don't test if not using real board if (!realBoard) return done(); - ourBoard.radioPollTimeGet().should.eventually.be.greaterThan(0); + ourBoard.radioPollTimeGet().should.eventually.be.greaterThan(0).and.notify(done); }); it("should be able to set the poll time", done => { // Don't test if not using real board if (!realBoard) return done(); - ourBoard.radioPollTimeSet(80).should.become(80); + ourBoard.radioPollTimeSet(80).should.become(80).and.notify(done); }); it("should be able to change to default baud rate", done => { // Don't test if not using real board if (!realBoard) return done(); - ourBoard.radioBaudRateSet('default').should.become(115200); + ourBoard.radioBaudRateSet('default').should.become(115200).and.notify(done); }); it("should be able to change to fast baud rate", done => { // Don't test if not using real board if (!realBoard) return done(); - ourBoard.radioBaudRateSet('fast').should.become(230400); - + ourBoard.radioBaudRateSet('fast').then(newBaudRate => { + expect(newBaudRate).to.equal(230400); + return ourBoard.radioBaudRateSet('default'); + }).then(() => { + done(); + }).catch(err => done(err)); }); it("should be able to set the system status", done => { // Don't test if not using real board diff --git a/test/openBCISimulator-test.js b/test/openBCISimulator-test.js index 1c9c731..23461f1 100644 --- a/test/openBCISimulator-test.js +++ b/test/openBCISimulator-test.js @@ -179,6 +179,8 @@ describe('openBCISimulator',function() { expect(openBCISample.isSuccessInBuffer(buf)).to.be.true; expect(openBCISample.isFailureInBuffer(buf)).to.be.false; expect(buf[buf.length - 4]).to.equal(newChanNum); + expect(simulator.channelNumber).to.equal(newChanNum); + expect(simulator.hostChannelNumber).to.equal(newChanNum); simulator.removeListener('data',dataEmit); done(); } @@ -223,6 +225,72 @@ describe('openBCISimulator',function() { simulator._processPrivateRadioMessage(new Buffer([k.OBCIRadioKey,k.OBCIRadioCmdChannelSet,7])); }); }); + describe('OBCIRadioCmdChannelSetOverride',function () { + it('should change just the hosts channel number and not the systems channel number and force a board comms failure', done => { + var systemChannelNumber = 0; + var newHostChannelNumber = 1; + simulator.channelNumber = systemChannelNumber; + var buf = new Buffer(0); + var dataEmit = data => { + buf = Buffer.concat([buf,data]); + if (openBCISample.doesBufferHaveEOT(buf)) { + expect(openBCISample.isSuccessInBuffer(buf)).to.be.true; + expect(openBCISample.isFailureInBuffer(buf)).to.be.false; + expect(buf[buf.length - 4]).to.equal(newHostChannelNumber); + expect(simulator.options.boardFailure).to.be.true; + expect(simulator.channelNumber).to.equal(systemChannelNumber); + expect(simulator.hostChannelNumber).to.equal(newHostChannelNumber); + simulator.removeListener('data',dataEmit); + done(); + } + }; + + simulator.on('data',dataEmit); + + simulator._processPrivateRadioMessage(new Buffer([k.OBCIRadioKey,k.OBCIRadioCmdChannelSetOverride,newHostChannelNumber])); + }); + it('should change just the hosts channel number and not the systems channel number and fix a board failure', done => { + var systemChannelNumber = 0; + var oldHostChannelNumber = 1; + simulator.channelNumber = systemChannelNumber; + simulator.hostChannelNumber = oldHostChannelNumber; + var buf = new Buffer(0); + var dataEmit = data => { + buf = Buffer.concat([buf,data]); + if (openBCISample.doesBufferHaveEOT(buf)) { + expect(openBCISample.isSuccessInBuffer(buf)).to.be.true; + expect(openBCISample.isFailureInBuffer(buf)).to.be.false; + expect(buf[buf.length - 4]).to.equal(systemChannelNumber); + expect(simulator.options.boardFailure).to.be.false; + expect(simulator.channelNumber).to.equal(systemChannelNumber); + expect(simulator.hostChannelNumber).to.equal(systemChannelNumber); + simulator.removeListener('data',dataEmit); + done(); + } + }; + + simulator.on('data',dataEmit); + + simulator._processPrivateRadioMessage(new Buffer([k.OBCIRadioKey,k.OBCIRadioCmdChannelSetOverride,systemChannelNumber])); + }); + it('should not set the channel number if out of bounds', done => { + var newChanNum = 26; + var buf = new Buffer(0); + var dataEmit = data => { + buf = Buffer.concat([buf,data]); + if (openBCISample.doesBufferHaveEOT(buf)) { + expect(openBCISample.isSuccessInBuffer(buf)).to.be.false; + expect(openBCISample.isFailureInBuffer(buf)).to.be.true; + simulator.removeListener('data',dataEmit); + done(); + } + }; + + simulator.on('data',dataEmit); + + simulator._processPrivateRadioMessage(new Buffer([k.OBCIRadioKey,k.OBCIRadioCmdChannelSetOverride,newChanNum])); + }); + }); describe('OBCIRadioCmdPollTimeGet',function () { it('should emit success if firmware version 2 with poll time', done => { var expectedPollTime = 80; @@ -307,7 +375,14 @@ describe('openBCISimulator',function() { if (openBCISample.doesBufferHaveEOT(buf)) { expect(openBCISample.isSuccessInBuffer(buf)).to.be.true; expect(openBCISample.isFailureInBuffer(buf)).to.be.false; - var newBaudRateBuf = buf.slice(buf.length - 9, buf.length - 3); + var eotBuf = new Buffer('$$$'); + var newBaudRateBuf; + for (var i = buf.length; i > 3; i--) { + if (bufferEqual(buf.slice(i - 3, i),eotBuf)) { + newBaudRateBuf = buf.slice(i - 9, i - 3); + break; + } + } var newBaudRateNum = Number(newBaudRateBuf.toString()); expect(newBaudRateNum).to.equal(k.OBCIRadioBaudRateDefault); simulator.removeListener('data',dataEmit); @@ -346,7 +421,14 @@ describe('openBCISimulator',function() { if (openBCISample.doesBufferHaveEOT(buf)) { expect(openBCISample.isSuccessInBuffer(buf)).to.be.true; expect(openBCISample.isFailureInBuffer(buf)).to.be.false; - var newBaudRateBuf = buf.slice(buf.length - 9, buf.length - 3); + var eotBuf = new Buffer("$$$"); + var newBaudRateBuf; + for (var i = buf.length; i > 3; i--) { + if (bufferEqual(buf.slice(i - 3, i),eotBuf)) { + newBaudRateBuf = buf.slice(i - 9, i - 3); + break; + } + } var newBaudRateNum = Number(newBaudRateBuf.toString()); expect(newBaudRateNum).to.equal(k.OBCIRadioBaudRateFast); simulator.removeListener('data',dataEmit);