mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
Clean up unit tests, add echo test
This commit is contained in:
parent
6066c1008f
commit
ddba26544f
12 changed files with 108 additions and 197 deletions
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
18
test/arduinoEcho.ino
Normal file
18
test/arduinoEcho.ino
Normal file
|
|
@ -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);
|
||||
}
|
||||
85
test/basic.js
Normal file
85
test/basic.js
Normal file
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -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");
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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( "=>" );
|
||||
|
||||
|
|
@ -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 <sensorValue> milliseconds:
|
||||
delay(sensorValue);
|
||||
// turn the ledPin off:
|
||||
digitalWrite(ledPin, LOW);
|
||||
// stop the program for for <sensorValue> milliseconds:
|
||||
delay(sensorValue);
|
||||
}
|
||||
|
||||
|
|
@ -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.');
|
||||
});
|
||||
|
||||
|
|
@ -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();
|
||||
|
|
@ -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();
|
||||
|
|
@ -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();
|
||||
|
|
@ -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();
|
||||
});
|
||||
*/
|
||||
Loading…
Add table
Reference in a new issue