Je suis en train de se connecter à série USB qui fonctionne manuellement comme ci-dessous:Google chrome - connexion chrome.serial échoué
$ ls /dev/cu.*
/dev/cu.Bluetooth-Incoming-Port /dev/cu.usbserial
$ screen /dev/cu.usbserial 9600
Mais quand les applications Google Chrome essayant de connecter son échec.
applications, background.js:
var DEVICE_PATH = '/dev/cu.usbserial';
var serial = chrome.serial;
function log(msg) {
console.log(msg);
}
var ab2str = function(buf) {
var bufView = new Uint8Array(buf);
var encodedString = String.fromCharCode.apply(null, bufView);
return decodeURIComponent(escape(encodedString));
};
var str2ab = function(str) {
var encodedString = unescape((str));
var bytes = new Uint8Array(1);
bytes[0] = parseInt(encodedString);
return bytes.buffer;
};
// Object
var SerialConnection = function() {
this.connectionId = -1;
this.lineBuffer = "";
this.receiveTimeout =50;
this.boundOnReceive = this.onReceive.bind(this);
this.boundOnReceiveError = this.onReceiveError.bind(this);
this.onConnect = new chrome.Event();
this.onReadLine = new chrome.Event();
this.onError = new chrome.Event();
};
SerialConnection.prototype.onConnectComplete = function(connectionInfo) {
if (!connectionInfo) {
log("Connection failed.");
return;
}
this.connectionId = connectionInfo.connectionId;
chrome.serial.onReceive.addListener(this.boundOnReceive);
chrome.serial.onReceiveError.addListener(this.boundOnReceiveError);
this.onConnect.dispatch();
};
SerialConnection.prototype.onReceive = function(receiveInfo) {
if (receiveInfo.connectionId !== this.connectionId) {
return;
}
this.lineBuffer += ab2str(receiveInfo.data);
var index;
while ((index = this.lineBuffer.indexOf('$')) >= 0) {
var line = this.lineBuffer.substr(0, index + 1);
this.onReadLine.dispatch(line);
this.lineBuffer = this.lineBuffer.substr(index + 1);
}
};
SerialConnection.prototype.onReceiveError = function(errorInfo) {
log('Error');
if (errorInfo.connectionId === this.connectionId) {
log('Error');
this.onError.dispatch(errorInfo.error);
log('Error');
}
log('Error');
};
SerialConnection.prototype.connect = function(path) {
serial.connect(path, {bitrate: 9600}, this.onConnectComplete.bind(this));
};
SerialConnection.prototype.send = function(msg) {
if (this.connectionId < 0) {
throw 'Invalid connection';
}
serial.send(this.connectionId, str2ab(msg), function() {});
};
SerialConnection.prototype.disconnect = function() {
if (this.connectionId < 0) {
throw 'Invalid connection';
}
serial.disconnect(this.connectionId, function() {});
};
// -- Connect
var connection = new SerialConnection();
connection.onConnect.addListener(function() {
log('connected to: ' + DEVICE_PATH);
});
connection.onReadLine.addListener(function(line) {
log('read line: ' + line);
});
connection.onError.addListener(function() {
log('Error: ');
});
connection.connect(DEVICE_PATH);
Il existe une API pour interagir avec l'USB. Voir [chrome.usb] (https://developer.chrome.com/apps/usb). «Utilisez l'API chrome.usb pour interagir avec les périphériques USB connectés.Cette API permet d'accéder aux opérations USB à partir du contexte d'une application.Avec cette API, les applications peuvent fonctionner comme des pilotes pour les périphériques matériels.Les erreurs générées par cette API sont signalées par en définissant runtime.lastError et en exécutant le rappel normal de la fonction, les paramètres habituels du callback seront indéfinis dans ce cas. " – gerardnimo
chrome.usb n'est pas destiné au lecteur série. Pouvez-vous vérifier les détails? question était de faire chrome.serial travail pas le chrome.usb. – YumYumYum
est-ce un BUG? chrome.serial ne fonctionne pas toujours la connexion échoue. – YumYumYum