从客户端Web浏览器与串行端口通信。

用户名

在我的Web应用程序(sencha extjs 5)中,我有一个用户要求,以将数据读/写到客户端PC串行端口。

我知道如果不在本地计算机上安装一些二进制文件(本机应用程序,Windows服务等),客户端浏览器将无法访问本地计算机硬件。

我已经看到了几年前在stackoverflow论坛中讨论过的相同问题。但是我需要知道当今使用现有技术的最佳方法是什么?

苏加思

嗯,实现此目的的一种方法是开发chrome应用程序。您可以使用chrome.serial API。

https://developer.chrome.com/apps/serial

示例代码

在您的manifest.json中,

{
  "name": "Serial Sample",
  "description": "Read/Write from/to serial port.",
  "version": "1.0",
  "manifest_version": 2,
  "permissions": ["serial"],
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  }
}

在您的background.js中,

const DEVICE_PATH = 'COM1';
const serial = chrome.serial;
var dataRecieved="";

/* Interprets an ArrayBuffer as UTF-8 encoded string data. */
var ab2str = function(buf) {
    var bufView = new Uint8Array(buf);
    var encodedString = String.fromCharCode.apply(null, bufView);
    return decodeURIComponent(escape(encodedString));
};

/* Converts a string to UTF-8 encoding in a Uint8Array; returns the array buffer. */
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 SerialConnection = function() {
    this.connectionId = -1;
    this.lineBuffer = "";
    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('\n')) >= 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) {
    if (errorInfo.connectionId === this.connectionId) {
        this.onError.dispatch(errorInfo.error);
    }
};

SerialConnection.prototype.connect = function(path) {
    serial.connect(path, 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() {});
};


var connection = new SerialConnection();

connection.onConnect.addListener(function() {
    //console.log('connected to: ' + DEVICE_PATH);
});

connection.onReadLine.addListener(function (line) {
    //Serial port data recieve event.
    dataRecieved = dataRecieved +line;
});

connection.connect(DEVICE_PATH);

创建chrome应用程序以与串行端口通信后,下一步就是允许您的外部网页使用JavaScript与chrome应用程序进行通信。

为此,请在manifest.json文件中添加,

"externally_connectable": {
"matches": ["*://*.example.com/*"]
}

这将使example.com域上的外部网页与chrome应用程序进行通信。

在您的网页中,

    // The ID of the extension we want to talk to.
    var editorExtensionId = "nboladondmajlaalmcdupihoilpcketyl";

   // Make a simple request:
   chrome.runtime.sendMessage(editorExtensionId, 
   { data: "data to pass to the chrome app" },  
   function (response)
   {
    alert(response);
   });

在您的Chrome应用中,

chrome.runtime.onMessageExternal.addListener(
  function (request, sender, sendResponse) {
        sendResponse("Send serial port data to the web page");
  });

https://developer.chrome.com/apps/messaging

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Applet + Javascript从客户端(浏览器)读取串行端口

来自分类Dev

服务器上托管的Asp.net Web应用程序以及与客户端计算机的串行端口的通信

来自分类Dev

服务器上托管的Asp.net Web应用程序以及与客户端计算机的串行端口的通信

来自分类Dev

为没有Web浏览器/ Web视图的UI开发NEST客户端

来自分类Dev

打开COM端口以在客户端进行串行通信的解决方法(最好在Python中)

来自分类Dev

Google Chrome-如何使用JavaScript通过客户端的串行端口进行通信?

来自分类Dev

在客户端Web浏览器中显示jenkins仪表板

来自分类Dev

如何检测 - 是否在客户端 Web 浏览器中启用了 webstorage?

来自分类Dev

Firefox或Chrome浏览器的SOAP客户端扩展

来自分类Dev

Amazon SNS:通知浏览器客户端?

来自分类Dev

没有浏览器的Java Websocket客户端

来自分类Dev

过滤客户端证书(如浏览器)

来自分类Dev

如何获取客户端的浏览器信息?

来自分类Dev

浏览器不应用客户端证书:403.7

来自分类Dev

在谷歌浏览器中获取客户端名称

来自分类Dev

浏览器不读取客户端证书

来自分类Dev

从node.js客户端到浏览器客户端的socket.emit

来自分类Dev

Java客户端和浏览器客户端之间的响应代码变化

来自分类Dev

支持浏览器客户端和本机客户端之间的交互

来自分类Dev

从Web窗体在客户端计算机上启动MS Word应用程序。(所有浏览器)

来自分类Dev

套接字编程-为什么即使在服务器接受了连接之后,Web服务器仍然使用监听端口80与客户端通信?

来自分类Dev

Spring Security:按客户端类型(浏览器/非浏览器)启用/禁用CSRF

来自分类Dev

在浏览器上显示服务器时间而不是客户端时间

来自分类Dev

加快网站的多图像从客户端浏览器到服务器的上传

来自分类Dev

浏览器会话关闭后,如何恢复客户端计时器?

来自分类Dev

浏览器与服务器在socket.io客户端行为上的差异

来自分类Dev

如何从服务器发送要在客户端浏览器中打开的 html 文件?

来自分类Dev

通过浏览器与秤进行串行/并行通信

来自分类Dev

如何在系统中设置默认Web浏览器,邮件客户端,视频播放器和音乐播放器

Related 相关文章

  1. 1

    使用Applet + Javascript从客户端(浏览器)读取串行端口

  2. 2

    服务器上托管的Asp.net Web应用程序以及与客户端计算机的串行端口的通信

  3. 3

    服务器上托管的Asp.net Web应用程序以及与客户端计算机的串行端口的通信

  4. 4

    为没有Web浏览器/ Web视图的UI开发NEST客户端

  5. 5

    打开COM端口以在客户端进行串行通信的解决方法(最好在Python中)

  6. 6

    Google Chrome-如何使用JavaScript通过客户端的串行端口进行通信?

  7. 7

    在客户端Web浏览器中显示jenkins仪表板

  8. 8

    如何检测 - 是否在客户端 Web 浏览器中启用了 webstorage?

  9. 9

    Firefox或Chrome浏览器的SOAP客户端扩展

  10. 10

    Amazon SNS:通知浏览器客户端?

  11. 11

    没有浏览器的Java Websocket客户端

  12. 12

    过滤客户端证书(如浏览器)

  13. 13

    如何获取客户端的浏览器信息?

  14. 14

    浏览器不应用客户端证书:403.7

  15. 15

    在谷歌浏览器中获取客户端名称

  16. 16

    浏览器不读取客户端证书

  17. 17

    从node.js客户端到浏览器客户端的socket.emit

  18. 18

    Java客户端和浏览器客户端之间的响应代码变化

  19. 19

    支持浏览器客户端和本机客户端之间的交互

  20. 20

    从Web窗体在客户端计算机上启动MS Word应用程序。(所有浏览器)

  21. 21

    套接字编程-为什么即使在服务器接受了连接之后,Web服务器仍然使用监听端口80与客户端通信?

  22. 22

    Spring Security:按客户端类型(浏览器/非浏览器)启用/禁用CSRF

  23. 23

    在浏览器上显示服务器时间而不是客户端时间

  24. 24

    加快网站的多图像从客户端浏览器到服务器的上传

  25. 25

    浏览器会话关闭后,如何恢复客户端计时器?

  26. 26

    浏览器与服务器在socket.io客户端行为上的差异

  27. 27

    如何从服务器发送要在客户端浏览器中打开的 html 文件?

  28. 28

    通过浏览器与秤进行串行/并行通信

  29. 29

    如何在系统中设置默认Web浏览器,邮件客户端,视频播放器和音乐播放器

热门标签

归档