Skip to content

Commit

Permalink
Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
reyiyo committed Jan 6, 2017
1 parent 81359cd commit c6b49ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var events = require('events');
var util = require('util');

Expand All @@ -7,10 +9,10 @@ var VirtualSerialPort = function(path, options, openImmediately, callback) {
var self = this;

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

if(openImmediately || openImmediately === undefined || openImmediately === null) {
if(openImmediately || typeof openImmediately === 'undefined' || openImmediately === null) {
process.nextTick(function() {
self.open(callback);
});
Expand All @@ -20,17 +22,18 @@ var VirtualSerialPort = function(path, options, openImmediately, callback) {
util.inherits(VirtualSerialPort, events.EventEmitter);

VirtualSerialPort.prototype.open = function open(callback) {
this.open = true;
this.opened = true;
process.nextTick(function() {
this.emit('open');
}.bind(this));

if(callback) {
return callback();
}
};

VirtualSerialPort.prototype.write = function write(buffer, callback) {
if(this.open) {
if(this.opened) {
process.nextTick(function() {
this.emit("dataToDevice", buffer);
}.bind(this));
Expand Down Expand Up @@ -60,13 +63,14 @@ VirtualSerialPort.prototype.drain = function drain(callback) {

VirtualSerialPort.prototype.close = function close(callback) {
this.removeAllListeners();
this.opened = false;
if(callback) {
return callback();
}
};

VirtualSerialPort.prototype.isOpen = function isOpen() {
return this.open ? true : false;
return this.opened;
};

function VirtualSerialPortFactory() {
Expand All @@ -82,7 +86,7 @@ function VirtualSerialPortFactory() {
return this;
}

VirtualSerialPort.prototype.parsers = SerialPort.parsers;
VirtualSerialPort.parsers = SerialPort.parsers;
return VirtualSerialPort;
} catch (error) {
console.warn('VirtualSerialPort - NO parsers available');
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.3.1",
"version": "0.3.2",
"description": "A drop-in virtual replacement for node-serialport's SerialPort object",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit c6b49ac

Please sign in to comment.