Javascript Google chrome-chrome.serial连接失败

Javascript Google chrome-chrome.serial连接失败,javascript,google-chrome,google-chrome-extension,google-chrome-app,serial-communication,Javascript,Google Chrome,Google Chrome Extension,Google Chrome App,Serial Communication,我正在尝试连接USB串行接口,该接口正在手动工作,如下所示: $ ls /dev/cu.* /dev/cu.Bluetooth-Incoming-Port /dev/cu.usbserial $ screen /dev/cu.usbserial 9600 但是当谷歌chrome应用程序试图连接它的时候失败了 应用程序,background.js: var DEVICE_PATH = '/dev/cu.usbserial'; var serial = chrome.serial; funct

我正在尝试连接USB串行接口,该接口正在手动工作,如下所示:

$ ls /dev/cu.*
/dev/cu.Bluetooth-Incoming-Port /dev/cu.usbserial
$ screen /dev/cu.usbserial 9600
但是当谷歌chrome应用程序试图连接它的时候失败了

应用程序,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);
var DEVICE_PATH='/dev/cu.usbserial';
var serial=chrome.serial;
函数日志(msg){
控制台日志(msg);
}
var ab2str=功能(buf){
var bufView=新的UINT8阵列(buf);
var encodedString=String.fromCharCode.apply(null,bufView);
返回解码组件(转义(编码字符串));
};
var str2ab=函数(str){
var encodedString=unescape((str));
var字节=新的Uint8Array(1);
字节[0]=parseInt(encodedString);
返回bytes.buffer;
};
//反对
var SerialConnection=函数(){
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.OnConnectionComplete=函数(connectionInfo){
如果(!connectionInfo){
日志(“连接失败”);
返回;
}
this.connectionId=connectionInfo.connectionId;
chrome.serial.onReceive.addListener(this.boundOnReceive);
chrome.serial.onReceiveError.addListener(this.boundOnReceiveError);
this.onConnect.dispatch();
};
SerialConnection.prototype.onReceive=函数(receiveInfo){
if(receiveInfo.connectionId!==this.connectionId){
返回;
}
this.lineBuffer+=ab2str(receiveInfo.data);
var指数;
而((index=this.lineBuffer.indexOf('$'))>=0){
var line=this.lineBuffer.substr(0,索引+1);
此.onReadLine.dispatch(行);
this.lineBuffer=this.lineBuffer.substr(索引+1);
}
};
SerialConnection.prototype.onReceiveError=函数(errorInfo){
日志(“错误”);
如果(errorInfo.connectionId==此.connectionId){
日志(“错误”);
此.onError.dispatch(errorInfo.error);
日志(“错误”);
}
日志(“错误”);
};
SerialConnection.prototype.connect=函数(路径){
serial.connect(路径,{bitrate:9600},this.onConnectComplete.bind(this));
};
SerialConnection.prototype.send=函数(msg){
if(this.connectionId<0){
抛出“无效连接”;
}
send(this.connectionId,str2ab(msg),function(){});
};
SerialConnection.prototype.disconnect=函数(){
if(this.connectionId<0){
抛出“无效连接”;
}
serial.disconnect(this.connectionId,function(){});
};
//--连接
var connection=new SerialConnection();
connection.onConnect.addListener(函数(){
日志(“连接到:”+设备路径);
});
connection.onReadLine.addListener(函数(行){
日志('读取行:'+行);
});
connection.onError.addListener(函数(){
日志('错误:');
});
连接。连接(设备路径);

OSX上的USB访问,发现Chrome在本机客户端应用程序(使用hidapi)运行并识别设备之前无法访问USB设备


将Windows操作系统与相同的代码一起使用,它就会工作。

OSX上的USB访问发现,Chrome在本机客户端应用程序(使用hidapi)运行并识别设备之前,无法访问USB设备


将Windows操作系统与相同的代码一起使用,它就会工作。

有一个用于与USB交互的API。看见“使用chrome.usb API与连接的usb设备交互。此API提供从应用程序上下文中访问usb操作的权限。使用此API,应用程序可以用作硬件设备的驱动程序。通过设置runtime.lastError并执行函数的常规回调来报告此API生成的错误。回调的常规在这种情况下,参数将未定义。“chrome.usb不适用于串行读取器。你能详细登记吗?问题是让chrome.serial工作而不是chrome.usb。这是一个BUG吗?chrome.serial不工作总是连接失败。有一个用于与USB交互的API。看见“使用chrome.usb API与连接的usb设备交互。此API提供从应用程序上下文中访问usb操作的权限。使用此API,应用程序可以用作硬件设备的驱动程序。通过设置runtime.lastError并执行函数的常规回调来报告此API生成的错误。回调的常规在这种情况下,参数将未定义。“chrome.usb不适用于串行读取器。你能详细登记吗?问题是让chrome.serial工作而不是chrome.usb。这是一个BUG吗?chrome.serial不工作总是连接失败。我遇到了同样的问题。你能再详细说明一下吗?-请在问题跟踪程序中标记此问题,并报告您也有相同的问题。在OSX中,Google Chrome序列不起作用,因为Google Chrome未使用本机客户端应用程序(hidapi)。但同样的事情也适用于Windows操作系统。你必须让Google Chrome社区看到,如果你有很多人在问题跟踪链接上报告同样的问题,你也会失败。然后谷歌Chrome社区将修复它。因此,请在报告的问题跟踪url中进行跟进。我遇到了相同的问题。您能详细说明一下吗?-请在问题跟踪程序中标记此问题,并报告您也有相同的问题。在OSX中,Google Chrome序列不起作用,因为Google Chrome未使用本机客户端应用程序(hidapi)。但同样的事情也适用于Windows操作系统。你必须让Google Chrome社区看到,如果你有很多人在问题跟踪链接上报告同样的问题,你也会失败。然后谷歌Chrome社区将修复它。因此,请在报告的问题跟踪url中跟进。