2014-01-18 10:44:43 +00:00
|
|
|
var serial = {
|
|
|
|
connectionId: -1,
|
|
|
|
|
|
|
|
connect: function(path, options, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
chrome.serial.connect(path, options, function(connectionInfo) {
|
|
|
|
self.connectionId = connectionInfo.connectionId;
|
2014-01-25 12:48:31 +00:00
|
|
|
|
|
|
|
if (connectionInfo.connectionId > 0) {
|
|
|
|
console.log('SERIAL: Connection opened with ID: ' + connectionInfo.connectionId + ', Baud: ' + connectionInfo.bitrate);
|
|
|
|
}
|
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
callback(connectionInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
disconnect: function(callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
chrome.serial.disconnect(this.connectionId, function(result) {
|
2014-01-25 12:48:31 +00:00
|
|
|
if (result) {
|
|
|
|
console.log('SERIAL: Connection with ID: ' + self.connectionId + ' closed');
|
|
|
|
} else {
|
|
|
|
console.log('SERIAL: Failed to close connection with ID: ' + self.connectionId + ' closed');
|
|
|
|
}
|
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
self.connectionId = -1;
|
2014-01-25 12:48:31 +00:00
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
callback(result);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getDevices: function(callback) {
|
|
|
|
chrome.serial.getDevices(function(devices_array) {
|
|
|
|
var devices = [];
|
|
|
|
devices_array.forEach(function(device) {
|
|
|
|
devices.push(device.path);
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(devices);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setControlSignals: function(signals, callback) {
|
|
|
|
chrome.serial.setControlSignals(this.connectionId, signals, callback);
|
|
|
|
},
|
|
|
|
send: function(data, callback) {
|
|
|
|
chrome.serial.send(this.connectionId, data, callback);
|
|
|
|
},
|
|
|
|
onReceive: {
|
|
|
|
listeners_: chrome.serial.onReceive.listeners_,
|
|
|
|
|
|
|
|
addListener: function(function_reference) {
|
|
|
|
chrome.serial.onReceive.addListener(function_reference);
|
|
|
|
},
|
|
|
|
removeListener: function(function_reference) {
|
|
|
|
chrome.serial.onReceive.removeListener(function_reference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|