-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rip out all the after open and watch port stuff, breaks reads but we're getting closer
- Loading branch information
Showing
6 changed files
with
221 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,130 @@ | ||
'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 (!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 (!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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.