Skip to content

Commit

Permalink
Bump new npm version
Browse files Browse the repository at this point in the history
  • Loading branch information
reyiyo committed Jan 22, 2016
1 parent 181c613 commit 032a6e0
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 50 deletions.
2 changes: 0 additions & 2 deletions .gitiginore

This file was deleted.

79 changes: 78 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,79 @@
node_modules/
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
### Node template
# Logs
logs
*.log
npm-debug.log*
*.swp

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#About this repo:
# About this repo

This is an exact copy of the last code in https://github.com/freshdried/virtual-serialport.git, which is now deleted and we don't know why.
We made this copy and also published the npm package because we were using this module in several projects.


# virtual-serialport

Do you use [node-serialport](https://github.com/voodootikigod/node-serialport), but don't have your device connected for development or testing?

virtual-serialport provides a virtual drop-in replacement for an actual SerialPort object.

<br><br><br>
## examples
## Examples

```javascript
var SerialPort = require('node-serialport').SerialPort;
if (process.env.NODE_ENV == 'development') {
Expand All @@ -20,38 +21,36 @@ if (process.env.NODE_ENV == 'development') {
var sp = new SerialPort('/dev/ttyUSB0', { baudrate: 57600 }); // still works if NODE_ENV is set to development!

sp.on('open', function (err) {

sp.on("data", function(data) {
console.log("From Arduino: " + data);
});


if (process.env.NODE_ENV == 'development') {
sp.on("dataToDevice", function(data) {
sp.writeToComputer(data + " " + data + "!");
});
}

sp.write("BLOOP"); // "From Arduino: BLOOP BLOOP!"
}
});
```

<br><br><br>
## Usage

## usage
```javascript
var VirtualSerialPort = require('virtual-serialport');
var sp = new VirtualSerialPort(path, [opts={}]);
```

#### sp = new VirtualSerialPort(path, [opts={}])
instantiates a virtual SerialPort object. Currently does nothing with the parameters.

```javascript
sp = new VirtualSerialPort("/dev/ttyUSB0");
var sp = new VirtualSerialPort("/dev/ttyUSB0");
// No device has to actually exist at /dev/ttyUSB0 :)
```

<br><br><br>
### computer-to-device communication
### Computer to device communication

```javascript
sp.on("data", function(data) {
Expand All @@ -61,7 +60,7 @@ sp.on("data", function(data) {
sp.writeToComputer("BLEEP!"); // "Computer says, BLEEP!"
```

### device-to-computer communication
### Device to computer communication

```javascript
sp.on("dataToDevice", function(data) {
Expand All @@ -71,30 +70,29 @@ sp.on("dataToDevice", function(data) {
sp.write("BLOOP!"); // "Arduino says, BLOOP!"
```

<br><br><br>
### node-serialport methods/events:

#### sp.write(data)
Writes data to the virtual device.
Equivalent to `sp.emit("dataToDevice", data)`.

Writes data to the virtual device. Equivalent to `sp.emit("dataToDevice", data)`.

#### sp.on("open", function(err) { ... } )
Runs function once SerialPort is ready, as you would with an actual SerialPort instance.

Runs function once `SerialPort` is ready, as you would with an actual `SerialPort` instance.

#### sp.on("data", function(data) { ... })
Act on data sent to the computer, as you would with an actual SerialPort instance.

<br><br><br>
### non node-serialport methods/events:
Act on data sent to the computer, as you would with an actual `SerialPort` instance.

### Non node-serialport methods/events:

#### sp.writeToComputer(data);
Writes data to computer.
Equivalent to `sp.emit("data", data)`

Writes data to computer. Equivalent to `sp.emit("data", data)`

#### sp.on("dataToDevice", function(data) { ... })
Act on data sent to the device.

<br><br><br>
## todo
## TODO
- move to automated testing (assertions and more)
- better match voodootikigod's node-serialport api
29 changes: 15 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var events = require('events');
var util = require('util');
var util = require('util');

var VirtualSerialPort = function(path, options, openImmediately, callback) {
events.EventEmitter.call(this);
events.EventEmitter.call(this);

var self = this;
var open = false;
var self = this;

this.writeToComputer = function(data) {
self.emit("data", data);
};
this.writeToComputer = function(data) {
self.emit("data", data);
};

if (openImmediately || openImmediately === undefined || openImmediately === null) {
if(openImmediately || openImmediately === undefined || openImmediately === null) {
process.nextTick(function() {
self.open(callback);
});
Expand All @@ -23,16 +22,18 @@ util.inherits(VirtualSerialPort, events.EventEmitter);
VirtualSerialPort.prototype.open = function open(callback) {
this.open = true;
this.emit('open');
if (callback) {
if(callback) {
callback();
}
};

VirtualSerialPort.prototype.write = function write(buffer, callback) {
if (this.open) this.emit("dataToDevice", buffer);
if(this.open) {
this.emit("dataToDevice", buffer);
}
// This callback should receive both an error and result, however result is
// undocumented so I do not know what it should contain
if (callback) {
if(callback) {
callback();
}
};
Expand All @@ -44,20 +45,20 @@ VirtualSerialPort.prototype.resume = function resume() {
};

VirtualSerialPort.prototype.flush = function flush(callback) {
if (callback) {
if(callback) {
callback();
}
};

VirtualSerialPort.prototype.drain = function drain(callback) {
if (callback) {
if(callback) {
callback();
}
};

VirtualSerialPort.prototype.close = function close(callback) {
this.removeAllListeners();
if (callback) {
if(callback) {
callback();
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "virtual-serialport",
"version": "0.1.3",
"version": "0.2.0",
"description": "A drop-in virtual replacement for node-serialport's SerialPort object",
"main": "index.js",
"directories": {
Expand Down
18 changes: 9 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ var sp = new VirtualSerialPort('/dev/null');

// Simple echo function for fake Arduino
sp.on('dataToDevice', function(data) {
console.log("Arduino: " + (data == 1 ? "BLEEP" : "BLOOP"));
sp.writeToComputer(data^1);
console.log("Arduino: " + (data == 1 ? "BLEEP" : "BLOOP"));
sp.writeToComputer(data ^ 1);
});

sp.on('open', function() {
console.log("Initialized virtual serial port!");
console.log("Initialized virtual serial port!");

setInterval(function() {
sp.write(Math.floor(Math.random() * 2));
}, 1000);
setInterval(function() {
sp.write(Math.floor(Math.random() * 2));
}, 1000);

sp.on("data", function(data) {
console.log("Computer: " + (data == 1 ? "BLEEP" : "BLOOP"));
});
sp.on("data", function(data) {
console.log("Computer: " + (data == 1 ? "BLEEP" : "BLOOP"));
});
});

0 comments on commit 032a6e0

Please sign in to comment.