Skip to content

Commit

Permalink
first draft of windows support
Browse files Browse the repository at this point in the history
rip out all the after open and watch port stuff, breaks reads but we're
getting closer
  • Loading branch information
reconbot committed Nov 2, 2016
1 parent b312c1c commit bbd7186
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 182 deletions.
45 changes: 30 additions & 15 deletions lib/bindings-darwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

var binding = require('bindings')('serialport.node');

function asyncError(cb, err) {
process.nextTick(function() {
cb(err);
});
}

function DarwinBinding(opt) {
if (typeof opt !== 'object') {
throw new TypeError('options is not an object');
Expand Down Expand Up @@ -29,11 +35,9 @@ DarwinBinding.prototype.open = function(path, options, cb) {
}

if (this.isOpen) {
process.nextTick(function() {
cb(new Error('Already open'));
});
return;
return asyncError(cb, new Error('Already open'));
}

binding.open(path, options, function(err, fd) {
if (err) {
return cb(err);
Expand All @@ -44,11 +48,12 @@ DarwinBinding.prototype.open = function(path, options, cb) {
};

DarwinBinding.prototype.close = function(cb) {
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
process.nextTick(function() {
cb(new Error('Already closed'));
});
return;
return asyncError(cb, new Error('Port is not open'));
}

binding.close(this.fd, function(err) {
Expand All @@ -71,8 +76,12 @@ DarwinBinding.prototype.set = function(options, cb) {
throw new TypeError('options is not an object');
}

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return cb(new Error('Port is not open'));
return asyncError(cb, new Error('Port is not open'));
}
binding.set(this.fd, options, cb);
};
Expand All @@ -82,8 +91,12 @@ DarwinBinding.prototype.write = function(buffer, cb) {
throw new TypeError('buffer is not a Buffer');
}

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return cb(new Error('Port is not open'));
return asyncError(cb, new Error('Port is not open'));
}

binding.write(this.fd, buffer, cb);
Expand All @@ -99,12 +112,14 @@ var commonMethods = [
commonMethods.map(function(methodName) {
DarwinBinding.prototype[methodName] = function() {
var args = Array.prototype.slice.apply(arguments);
var cb = args[args.length - 1];

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
var cb = args.pop();
process.nextTick(function() {
cb(new Error('Port is not open'));
});
return;
return asyncError(cb, new Error('Port is not open'));
}
args.unshift(this.fd);
binding[methodName].apply(binding, args);
Expand Down
46 changes: 30 additions & 16 deletions lib/bindings-linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
var binding = require('bindings')('serialport.node');
var listLinux = require('./list-linux');

function asyncError(cb, err) {
process.nextTick(function() {
cb(err);
});
}

function LinuxBinding(opt) {
if (typeof opt !== 'object') {
throw new TypeError('options is not an object');
Expand Down Expand Up @@ -30,11 +36,9 @@ LinuxBinding.prototype.open = function(path, options, cb) {
}

if (this.isOpen) {
process.nextTick(function() {
cb(new Error('Already open'));
});
return;
return asyncError(cb, new Error('Already open'));
}

binding.open(path, options, function(err, fd) {
if (err) {
return cb(err);
Expand All @@ -45,11 +49,12 @@ LinuxBinding.prototype.open = function(path, options, cb) {
};

LinuxBinding.prototype.close = function(cb) {
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
process.nextTick(function() {
cb(new Error('Already closed'));
});
return;
return asyncError(cb, new Error('Port is not open'));
}

binding.close(this.fd, function(err) {
Expand All @@ -66,8 +71,12 @@ LinuxBinding.prototype.set = function(options, cb) {
throw new TypeError('options is not an object');
}

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return cb(new Error('Port is not open'));
return asyncError(cb, new Error('Port is not open'));
}
binding.set(this.fd, options, cb);
};
Expand All @@ -77,10 +86,13 @@ LinuxBinding.prototype.write = function(buffer, cb) {
throw new TypeError('buffer is not a Buffer');
}

if (!this.isOpen) {
return cb(new Error('Port is not open'));
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
binding.write(this.fd, buffer, cb);
};

Expand All @@ -94,12 +106,14 @@ var commonMethods = [
commonMethods.map(function(methodName) {
LinuxBinding.prototype[methodName] = function() {
var args = Array.prototype.slice.apply(arguments);
var cb = args[args.length - 1];

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
var cb = args.pop();
process.nextTick(function() {
cb(new Error('Port is not open'));
});
return;
return asyncError(cb, new Error('Port is not open'));
}
args.unshift(this.fd);
binding[methodName].apply(binding, args);
Expand Down
148 changes: 135 additions & 13 deletions lib/bindings-win32.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,138 @@
'use strict';

var bindings = require('bindings')('serialport.node');

module.exports = {
close: bindings.close,
drain: bindings.drain,
flush: bindings.flush,
list: bindings.list,
open: bindings.open,
set: bindings.set,
update: bindings.update,
write: bindings.write,
read: bindings.read,
dataAvailable: bindings.dataAvailable
var binding = require('bindings')('serialport.node');

function asyncError(cb, err) {
process.nextTick(function() {
cb(err);
});
}

function WindowsBinding(opt) {
if (typeof opt !== 'object') {
throw new TypeError('options is not an object');
}
if (typeof opt.disconnect !== 'function') {
throw new TypeError('options.disconnect is not a function');
}
this.disconnect = opt.disconnect;
this.fd = null;
};

WindowsBinding.prototype.read = require('./read-unix');

WindowsBinding.prototype.open = function(path, options, cb) {
if (typeof path !== 'string') {
throw new TypeError('path is not a string');
}

if (typeof options !== 'object') {
throw new TypeError('options is not an object');
}

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (this.isOpen) {
return asyncError(cb, new Error('Already open'));
}

binding.open(path, options, function(err, fd) {
if (err) {
return cb(err);
}
this.fd = fd;
cb(null);
}.bind(this));
};

WindowsBinding.prototype.close = function(cb) {
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}

binding.close(this.fd, function(err) {
if (err) {
return cb(err);
}
this.fd = null;

if (this.poller) {
this.poller.stop();
this.poller = null;
}

cb(null);
}.bind(this));
};

WindowsBinding.prototype.set = function(options, cb) {
if (typeof options !== 'object') {
throw new TypeError('options is not an object');
}

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}

binding.set(this.fd, options, cb);
};

WindowsBinding.prototype.write = function(buffer, cb) {
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('buffer is not a Buffer');
}

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}

binding.write(this.fd, buffer, cb);
};

var commonMethods = [
'drain',
'flush',
'update',
'get'
];

commonMethods.map(function(methodName) {
WindowsBinding.prototype[methodName] = function() {
var args = Array.prototype.slice.apply(arguments);
var cb = args[args.length - 1];

if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}

if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
args.unshift(this.fd);
binding[methodName].apply(binding, args);
};
});

Object.defineProperty(WindowsBinding.prototype, 'isOpen', {
enumerable: true,
get: function() {
return this.fd !== null;
}
});

WindowsBinding.list = binding.list;
module.exports = WindowsBinding;
21 changes: 11 additions & 10 deletions lib/serialport.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ function SerialPort(path, options, callback) {
readBufferSize: settings.highWaterMark
});

Object.defineProperty(this, 'binding', {
enumerable: true,
writable: false,
value: binding
});

Object.defineProperty(this, 'path', {
enumerable: true,
writable: false,
value: path
Object.defineProperties(this, {
'binding': {
enumerable: true,
writable: false,
value: binding
},
'path': {
enumerable: true,
writable: false,
value: path
}
});

this.opening = false;
Expand Down
Loading

0 comments on commit bbd7186

Please sign in to comment.