Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More browser support & port forwarding #227

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions avrgirl-arduino.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var injectDependencies = function(boards, Connection, protocols) {
megaDebug: opts.megaDebug || false,
board: opts.board || 'uno',
port: opts.port || '',
serialPort: opts.serialPort || null,
manualReset: opts.manualReset || false,
disableVerify: opts.disableVerify || false
};
Expand Down
139 changes: 114 additions & 25 deletions lib/browser-serialport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ class SerialPort extends EventEmitter {
this.browser = true;
this.path = this.options.path;
this.isOpen = false;
this.port = null;
this.port = port || null;
this.writer = null;
this.reader = null;
this.isUpdating = false;
this.pausePromise = Promise.resolve();
this.pauseResolve = () => {};
this.closeReadPromise = null;
this.closeReadResolve = () => {};
this.baudRate = this.options.baudRate;
this.requestOptions = this.options.requestOptions || {};

Expand All @@ -24,45 +29,74 @@ class SerialPort extends EventEmitter {
.catch((error) => {if (callback) {return callback(error)}});
}

async _getPort() {
if (this.port && !this.options.forceRequest) return this.port;
return window.navigator.serial.requestPort(this.requestOptions)
}

open(callback) {
window.navigator.serial.requestPort(this.requestOptions)
.then(serialPort => {
this.port = serialPort;
if (this.isOpen) return;
return this.port.open({ baudRate: this.baudRate || 57600 });
})
.then(() => this.writer = this.port.writable.getWriter())
.then(() => this.reader = this.port.readable.getReader())
.then(async () => {
this.emit('open');
this.isOpen = true;
callback(null);
while (this.port.readable.locked) {
try {
const { value, done } = await this.reader.read();
if (done) {
break;
return new Promise((resolve, reject) => {
this._getPort()
.then(serialPort => {
this.port = serialPort;
if (this.isOpen) return;
return this.port.open({ baudRate: this.baudRate || 57600 });
})
.then(() => this.writer = this.port.writable.getWriter())
.then(() => this.reader = this.port.readable.getReader())
.then(async () => {
if (!this.isUpdating) this.emit('open');
this.isOpen = true;
if (callback) callback(null);
resolve(null);
while (this.port.readable.locked) {
try {
await this.pausePromise;
const { value, done } = await this.reader.read();
if (done || this.closeReadPromise) {
break;
}
this.emit('data', Buffer.from(value));
} catch (e) {
console.error(e);
}
this.emit('data', Buffer.from(value));
} catch (e) {
console.error(e);
}
}
})
.catch(error => {callback(error)});
this.closeReadResolve();
})
.catch(error => {
if (callback) callback(error);
reject(error);
});
});
}

async close(callback) {
if (!this.isOpen) {
if (callback) callback(null);
return;
}
if (this.port.readable.locked) {
this.closeReadPromise = new Promise((resolve) => {
this.closeReadResolve = resolve;
});
this.pauseResolve();
try {
await this.reader.cancel();
} catch (err) { console.error(err); }
await this.closeReadPromise;
this.closeReadPromise = null;
}
try {
await this.reader.releaseLock();
await this.writer.releaseLock();
await this.port.close();
if (!this.isUpdating) this.emit('close');
this.isOpen = false;
} catch (error) {
if (callback) return callback(error);
throw error;
}
callback && callback(null);
if (callback) callback(null);
}

async set(props = {}, callback) {
Expand All @@ -87,6 +121,27 @@ class SerialPort extends EventEmitter {
if (callback) return callback(null);
}

async get(callback) {
const props = {};
try {
const signals = await this.port.getSignals();
if (Object.prototype.hasOwnProperty.call(signals, 'dataCarrierDetect')) {
props.dcd = signals.dataCarrierDetect;
}
if (Object.prototype.hasOwnProperty.call(signals, 'clearToSend')) {
props.cts = signals.clearToSend;
}
if (Object.prototype.hasOwnProperty.call(signals, 'dataSetReady')) {
props.dsr = signals.dataSetReady;
}
} catch (error) {
if (callback) return callback(error);
throw error;
}
if (callback) return callback(props);
return props;
}

write(buffer, callback) {
this.writer.write(buffer);
if (callback) return callback(null);
Expand All @@ -103,6 +158,40 @@ class SerialPort extends EventEmitter {
if (callback) callback(null, buffer);
}

async update (options, callback) {
try {
if (
Object.prototype.hasOwnProperty.call(options, 'baudRate')
&& this.baudRate !== options.baudRate
) {
if (this.isOpen) {
// try to quietly close then open the port with the new baudRate
this.isUpdating = true;
await this.close();
this.baudRate = options.baudRate;
await this.open();
this.isUpdating = false;
} else {
this.baudRate = options.baudRate;
}
}
} catch (error) {
if (callback) return callback(error);
throw error;
}
if (callback) callback(null);
}

pause() {
this.pausePromise = new Promise((resolve) => {
this.pauseResolve = resolve;
});
}

resume() {
this.pauseResolve();
}

// TODO: is this correct?
flush(callback) {
//this.port.flush(); // is this sync or a promise?
Expand Down
23 changes: 15 additions & 8 deletions lib/connection-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ Connection.prototype._init = function(callback) {
* Create new serialport instance for the Arduino board, but do not immediately connect.
*/
Connection.prototype._setUpSerial = function(callback) {
this.serialPort = new Serialport('', {
baudRate: this.board.baud,
autoOpen: false
});
this.serialPort.on('open', function() {
// _this.emit('connection:open');
})
return callback(null);
if (this.options.serialPort) {
this.serialPort = this.options.serialPort;
this.serialPort.update({ baudRate: this.board.baud }, function(error) {
return callback(error);
});
} else {
this.serialPort = new Serialport(this.options.port, {
baudRate: this.board.baud,
autoOpen: false
});
this.serialPort.on('open', function() {
// _this.emit('connection:open');
})
return callback(null);
}
};

/**
Expand Down