gpt4 book ai didi

javascript - Google chrome - chrome.serial 连接失败

转载 作者:行者123 更新时间:2023-11-28 06:25:00 25 4
gpt4 key购买 nike

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

$ ls /dev/cu.*
/dev/cu.Bluetooth-Incoming-Port /dev/cu.usbserial
$ screen /dev/cu.usbserial 9600

但是当 Google Chrome 应用程序尝试连接时失败。

enter image description here

应用程序、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);

最佳答案

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

使用Windows操作系统并使用相同的代码,即可运行。

关于javascript - Google chrome - chrome.serial 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35200639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com