diff --git a/package.json b/package.json index 5ac64bd..bc6bf56 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,6 @@ "serialportterm": "./bin/serialportTerminal.js" }, "scripts": { - "test": "mocha -R spec -t 60s -s 60s tests/test_parsers.js" + "test": "mocha -R spec -t 60s -s 60s" } } diff --git a/test/arduinoEcho.ino b/test/arduinoEcho.ino new file mode 100644 index 0000000..177a327 --- /dev/null +++ b/test/arduinoEcho.ino @@ -0,0 +1,18 @@ +const int ledPin = 11; + +void setup() { + pinMode(ledPin, OUTPUT); + Serial.begin(9600); + Serial.write("READY"); +} + +void loop() { + + while (Serial.available()) { + digitalWrite(ledPin, HIGH); + Serial.write(Serial.read()); + } + + delay(100); + digitalWrite(ledPin, LOW); +} diff --git a/test/basic.js b/test/basic.js new file mode 100644 index 0000000..5af7a5f --- /dev/null +++ b/test/basic.js @@ -0,0 +1,85 @@ +/*jslint node: true */ +/*global describe, it */ +"use strict"; + +var chai = require('chai'); +var serialPort = require('../serialport'); + +describe ('basics', function() { + + describe('echo hello', function() { + it('sends hello to the last port and validates that it is received back (see arduinoEcho.ino for echo sketch)', function(done) { + serialPort.list(function(err, ports) { + + chai.assert.isUndefined(err); + chai.assert.isDefined(ports); + chai.assert.isTrue(ports.length > 0); + + var data = new Buffer("hello"); + + var port = new serialPort.SerialPort(ports.slice(-1)[0].comName, null, false); + port.on('error', function(err) { chai.assert.fail(err); }); + + port.on('data', function(d) { + chai.assert.equal(data.toString(), d.toString()); + port.close(function(err) { + chai.assert.isUndefined(err); + done(); + }); + }); + + port.open(function(err) { + chai.assert.isUndefined(err); + port.write(data); + }); + }); + }); + }); + + describe('relaxed baud rate', function() { + it('opens a port with a non-standard baud rate', function(done) { + serialPort.list(function(err, ports) { + + chai.assert.isUndefined(err); + chai.assert.isDefined(ports); + chai.assert.isTrue(ports.length > 0); + + var port = new serialPort.SerialPort(ports.slice(-1)[0].comName, {baudrate: 5}, false); + port.on('error', function(err) { chai.assert.fail(err); }); + + port.open(function(err) { + chai.assert.isUndefined(err); + port.close(function(err) { + chai.assert.isUndefined(err); + done(); + }); + }); + }); + }); + }); + + describe('simple write', function() { + it('opens a port and sends data without encountering error', function(done) { + serialPort.list(function(err, ports) { + + chai.assert.isUndefined(err); + chai.assert.isDefined(ports); + chai.assert.isTrue(ports.length > 0); + + var data = new Buffer("hello"); + + var port = new serialPort.SerialPort(ports.slice(-1)[0].comName, null, false); + port.on('error', function(err) { chai.assert.fail(err); }); + + port.open(function(err) { + chai.assert.isUndefined(err); + port.write(data); + port.close(function(err) { + chai.assert.isUndefined(err); + done(); + }); + }); + }); + }); + }); +}); \ No newline at end of file diff --git a/tests/test_parsers.js b/test/test_parsers.js similarity index 85% rename from tests/test_parsers.js rename to test/test_parsers.js index 720f1b0..050c4b3 100644 --- a/tests/test_parsers.js +++ b/test/test_parsers.js @@ -1,5 +1,6 @@ +/*jslint node: true */ +/*global describe, it */ "use strict"; -/*global describe it require */ var parsers = require("../serialport").parsers; var chai = require("chai"); @@ -14,7 +15,7 @@ describe("parsers", function () { describe("#raw", function () { it("emits data exactly as it's written", function () { var mockEmitter = { emit: sinon.spy() }; - var data = Buffer("BOGUS"); + var data = new Buffer("BOGUS"); parsers.raw(mockEmitter, data); mockEmitter.emit.should.have.been.calledWith("data", data); }); @@ -23,7 +24,7 @@ describe("parsers", function () { describe("#readline", function () { it("emits data events split on a delimiter", function () { var parser = parsers.readline(); - var data = Buffer("I love robots\rEach and Every One\r"); + var data = new Buffer("I love robots\rEach and Every One\r"); var mockEmitter = { emit: sinon.spy() }; parser(mockEmitter, data); mockEmitter.emit.should.have.been.calledWith("data", "I love robots"); diff --git a/tests/arduinioEcho/arduinioEcho.ino b/tests/arduinioEcho/arduinioEcho.ino deleted file mode 100644 index 3bb040e..0000000 --- a/tests/arduinioEcho/arduinioEcho.ino +++ /dev/null @@ -1,15 +0,0 @@ - -void setup() -{ - Serial.begin(9600); - Serial.write("READY"); -} - -void loop() -{ - if (Serial.available() > 0) { - char inByte = Serial.read(); - Serial.write(toupper(inByte)); - } -} - diff --git a/tests/arduino-ldr-read.js b/tests/arduino-ldr-read.js deleted file mode 100644 index 3cc8ec6..0000000 --- a/tests/arduino-ldr-read.js +++ /dev/null @@ -1,45 +0,0 @@ - -// For use with Arduino LDR -// Upload arduino-ldr-read.pde to Arduino apparatus via Arduino IDE - -var sys = require("sys"), - repl = require("repl"), - serialPort = require("serialport").SerialPort, - - // Required - defaults = { - baudrate: 9600 - }, - // Create new serialport pointer - serial = new serialPort("/dev/ttyACM0" , defaults); - - -// Add data read event listener -serial.on( "data", function( data ) { - - var output; - - // Coerce data into a number - data = +data; - - // If data is worth reading and processing - if ( data && data > 1 ) { - - // Create a new Array() whose length equals data - // then join to create output visualization - output = ( new Array(data) ).join( ">" ); - - // Print the data value along with visualization of data - sys.puts( data + " " + output ); - } - -}); - - -serial.on( "error", function( msg ) { - sys.puts("error: " + msg ); -}); - - -repl.start( "=>" ); - diff --git a/tests/arduino-ldr-read.pde b/tests/arduino-ldr-read.pde deleted file mode 100644 index 9561863..0000000 --- a/tests/arduino-ldr-read.pde +++ /dev/null @@ -1,30 +0,0 @@ -/* - - */ - -int sensorPin = A0; // select the input pin for the potentiometer -int ledPin = 13; // select the pin for the LED -int sensorValue = 0; // variable to store the value coming from the sensor - -void setup() { - // declare the ledPin as an OUTPUT: - pinMode(ledPin, OUTPUT); - - Serial.begin(9600); -} - -void loop() { - // read the value from the sensor: - sensorValue = analogRead(sensorPin); - - Serial.println(sensorValue); - // turn the ledPin on - digitalWrite(ledPin, HIGH); - // stop the program for milliseconds: - delay(sensorValue); - // turn the ledPin off: - digitalWrite(ledPin, LOW); - // stop the program for for milliseconds: - delay(sensorValue); -} - diff --git a/tests/echoTest.js b/tests/echoTest.js deleted file mode 100644 index 1b8ea31..0000000 --- a/tests/echoTest.js +++ /dev/null @@ -1,56 +0,0 @@ -// Test with the epic VirtualSerialPortApp - http://code.google.com/p/macosxvirtualserialport/ - -var SerialPort = require("../serialport").SerialPort; -var assert = require('assert'); - -var keepAlive = setTimeout(function () { - console.log('timeout'); - process.exit(); -}, 10000); - -var portName; - -if (process.platform == 'win32') { - portName = 'COM4'; -} else if (process.platform == 'darwin') { - portName = '/dev/cu.usbserial-A800eFN5'; -} else { - portName = '/dev/ttyUSB0'; -} - -var readData = ''; -var sp = new SerialPort(portName, { - baudRate: 9600, - dataBits: 8, - parity: 'none', - stopBits: 1, - flowControl: false -}); -sp.on('data', function (data) { - readData += data.toString(); - console.log('read buffer:', readData); - if (readData.indexOf('READY') >= 0) { - readData = ''; - sp.write("hello world", function (err, bytesWritten) { - console.log('bytes written:', bytesWritten); - }); - } - if (readData === "HELLO WORLD") { - sp.close(); - clearTimeout(keepAlive); - console.log('done'); - } -}); - -sp.on('close', function (err) { - console.log('port closed'); -}); - -sp.on('error', function (err) { - console.error("error", err); -}); - -sp.on('open', function () { - console.log('port opened... Press reset on the Arduino.'); -}); - diff --git a/tests/test_read.js b/tests/test_read.js deleted file mode 100644 index 126fcae..0000000 --- a/tests/test_read.js +++ /dev/null @@ -1,16 +0,0 @@ -// Test with the epic VirtualSerialPortApp - http://code.google.com/p/macosxvirtualserialport/ - -var SerialPort = require("../serialport").SerialPort; -var util = require("util"), repl = require("repl"); - -var serial_port = new SerialPort("/dev/master", {baudrate: 9600}); - -serial_port.on("data", function (data) { - util.puts("here: "+data); -}) -serial_port.on("error", function (msg) { - util.puts("error: "+msg); -}) -repl.start("=>") - -//serial_port.close(); diff --git a/tests/test_relaxedBaudRate.js b/tests/test_relaxedBaudRate.js deleted file mode 100644 index 4954006..0000000 --- a/tests/test_relaxedBaudRate.js +++ /dev/null @@ -1,7 +0,0 @@ -var SerialPort = require("../serialport").SerialPort; -var util = require('util'); - -var serial_port = new SerialPort("/dev/slave", {baudrate: 5}); -serial_port.write("This is a test"); -util.puts("wrote buffer"); -serial_port.close(); diff --git a/tests/test_write.js b/tests/test_write.js deleted file mode 100644 index 525d519..0000000 --- a/tests/test_write.js +++ /dev/null @@ -1,8 +0,0 @@ -// Test with the epic VirtualSerialPortApp - http://code.google.com/p/macosxvirtualserialport/ - -var SerialPort = require("../serialport").SerialPort; - -var serial_port = new SerialPort("/dev/master", {baudrate: 9600}); - -serial_port.write("It worked!\r"); -serial_port.close(); diff --git a/tests/test_zwave.js b/tests/test_zwave.js deleted file mode 100644 index d580f2b..0000000 --- a/tests/test_zwave.js +++ /dev/null @@ -1,16 +0,0 @@ -// To test with a z-wave device, I recommend: http://www.aeon-labs.com/site/products/view/2/ - -var SerialPort = require("../serialport").SerialPort; -var util = require('util'); - -var serial_port = new SerialPort("/dev/ttyUSB0"); -serial_port.write(new Buffer([0x01, 0x03, 0x00, 0x20,220])); -util.puts("write"); -serial_port.read(); -/* -serial_port.on("data", function(d){ - util.puts("here"); - util.puts(d); - serial_port.close(); -}); -*/ \ No newline at end of file