node-serialport-bindings-cpp/lib/bindings/linux.js
Jade Bilkey 910438c2ca feat(linux): Custom baud rates for linux (eg 250k baudrate) (#1464)
* 250k baudrate now works
* Generic linux non-standard baudrate setting using termios2
* Tested successfully with an FTDI usb chip with the tx and rx pins bridged. Previous solution using the B38400 hack returned gibberish in same setup.
* Test for linux and win32 baudrate setting. Darwin stubbed out.

The arbitrary baud interface has been working on darwin up to this point so it's
likely that it works properly. The GetBaudRate extension should still get
written to be thorough but for now, this will let the CI tests pass properly and
still throw a warning about the check being disabled.

That is, of course, if anyone's testing with the arduino hardware on darwin,
otherwise, these tests never run anyway.
2018-02-05 09:33:11 -05:00

101 lines
2.3 KiB
JavaScript

'use strict';
const binding = require('bindings')('serialport.node');
const BaseBinding = require('./base');
const linuxList = require('./linux-list');
const Poller = require('./poller');
const promisify = require('../util').promisify;
const unixRead = require('./unix-read');
const unixWrite = require('./unix-write');
const defaultBindingOptions = Object.freeze({
vmin: 1,
vtime: 0
});
class LinuxBinding extends BaseBinding {
static list() {
return linuxList();
}
constructor(opt) {
super(opt);
this.bindingOptions = Object.assign({}, defaultBindingOptions, opt.bindingOptions || {});
this.fd = null;
this.writeOperation = null;
}
get isOpen() {
return this.fd !== null;
}
open(path, options) {
return super.open(path, options)
.then(() => {
this.openOptions = Object.assign({}, this.bindingOptions, options);
return promisify(binding.open)(path, this.openOptions);
})
.then((fd) => {
this.fd = fd;
this.poller = new Poller(fd);
});
}
close() {
return super.close()
.then(() => {
const fd = this.fd;
this.poller.stop();
this.poller = null;
this.openOptions = null;
this.fd = null;
return promisify(binding.close)(fd);
});
}
read(buffer, offset, length) {
return super.read(buffer, offset, length)
.then(() => unixRead.call(this, buffer, offset, length));
}
write(buffer) {
this.writeOperation = super.write(buffer)
.then(() => unixWrite.call(this, buffer))
.then(() => {
this.writeOperation = null;
});
return this.writeOperation;
}
update(options) {
return super.update(options)
.then(() => promisify(binding.update)(this.fd, options));
}
set(options) {
return super.set(options)
.then(() => promisify(binding.set)(this.fd, options));
}
get() {
return super.get()
.then(() => promisify(binding.get)(this.fd));
}
getBaudRate() {
return super.getBaudRate()
.then(() => promisify(binding.getBaudRate)(this.fd));
}
drain() {
return super.drain()
.then(() => Promise.resolve(this.writeOperation))
.then(() => promisify(binding.drain)(this.fd));
}
flush() {
return super.flush()
.then(() => promisify(binding.flush)(this.fd));
}
}
module.exports = LinuxBinding;