COMポートでデバイスと通信するためにchrome.serialを使用していますが、Webサイトでjavascriptの基本的なAPIを提供する必要があります。最初の試みはcontent_script+メッセージングでしたが、そのうちの1つはパッケージ化されたアプリに拡張機能が必要であり、もう1つは拡張機能である必要があるため、serial
許可を使用できません。content_script
この問題を解決できますか?
17210 次
2 に答える
11
これを解決するには、コンテンツスクリプトchrome extension
をサポートし、外部メッセージ通信とのシリアルポート通信を行います。packaged app
管理APIを使用して拡張IDを取得し、単一メッセージ通信の接続を確立します。
On Message Externalは、メッセージが別の内線から送信されたときに発生します。
参考文献
于 2013-01-04T09:48:42.210 に答える
-1
index.html
<button>Connect</button>
<script src="main.js"></script>
main.js
var connectionId;
/* Converts a string to UTF-8 encoding in a Uint8Array; returns the array */
var str2ab = function(str) {
var encodedString = unescape(encodeURIComponent(str));
var bytes = new Uint8Array(encodedString.length);
for (var i = 0; i < encodedString.length; ++i) {
bytes[i] = encodedString.charCodeAt(i);
}
return bytes.buffer;
};
var options = {
'bitrate': 115200,
'dataBits': 'eight',
'parityBit': 'no',
'stopBits': 'one'
}
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', btnSend);
chrome.serial.connect('COM3', options, function(info) {
connectionId = info.connectionId;
console.log("Connection established.");
});
});
var btnSend = function() {
var msg = "hello printer 123456\n";
chrome.serial.send(connectionId, str2ab(msg), function() {} );
}
マニフェスト.json
{
"name": "Printer at COM3 test",
"version": "1",
"manifest_version": 2,
"permissions": ["serial"],
"minimum_chrome_version": "23",
"icons": {
"16": "icon_16.png",
"128": "icon_128.png"
},
"app": {
"background": {
"scripts": ["launch.js"]
}
}
}
launch.js
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
id: "mainwin",
innerBounds: {
width: 320,
height: 240
}
});
});
于 2015-09-19T19:04:19.970 に答える