Skip to content

Instantly share code, notes, and snippets.

@monteslu
Last active December 19, 2018 09:05
Show Gist options
  • Save monteslu/11151839 to your computer and use it in GitHub Desktop.
Save monteslu/11151839 to your computer and use it in GitHub Desktop.
chrome serial port implementation for node serialport
/*global chrome*/
var EventEmitter = require('./events.js').EventEmitter;
var util = require('./util.js');
function SerialPort(port, options) {
var self = this;
var id;
var bytesToRead = options.buffersize || 1;
var reading = false;
function onOpen (info) {
console.log('onOpen', info);
if(info){
id = self.id = info.connectionId;
if (id < 0) {
self.emit("error", new Error("Cannot connect to " + port));
return;
}
self.emit("open");
chrome.serial.onReceive.addListener(function(obj){
if(id == obj.connectionId){
var data = new Uint8Array(obj.data);
self.emit("data", data);
}
});
}else{
console.log('can\'t connect to ', port);
}
}
chrome.serial.connect(port, {
bitrate: options.baudrate || 9600
}, onOpen);
}
SerialPort.prototype.write = function (data) {
function onWrite() {
// log("onWrite", arguments);
}
data = new Uint8Array(data);
// console.log("OUT", data);
chrome.serial.send(this.id, data.buffer, onWrite);
};
util.inherits(SerialPort, EventEmitter);
exports.SerialPort = SerialPort;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment