javascript chrome 扩展 - 从串口读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24986049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:41:29  来源:igfitidea点击:

chrome extension - reading from serial port

javascriptgoogle-chromeserial-portgoogle-chrome-app

提问by Joe Kasavage

This is my first attempt at developing with a chrome app or extension. I have a GPS receiver on the USB port which is emulated as a serial device.

这是我第一次尝试使用 chrome 应用程序或扩展程序进行开发。我在 USB 端口上有一个 GPS 接收器,它被模拟为串行设备。

Running this code

运行此代码

var onGetDevices = function(ports) {

for (var i=0; i<ports.length; i++) {

    // show me some output
    console.log(ports[i].path);

    // Connect to the serial port /dev/ttyUSB0
    chrome.serial.connect(ports[i].path, {bitrate: 9600}, onConnect);   
  }
}
chrome.serial.getDevices(onGetDevices);

gets me "/dev/ttyUSB0" in the console, so it appears to be finding the device.

在控制台中让我“/dev/ttyUSB0”,所以它似乎正在寻找设备。

How do I then connect to the device? I've included the serial.connect line above, with the following functions:

然后我如何连接到设备?我已经包含了上面的 serial.connect 行,具有以下功能:

var onConnect = function(connectionInfo) {
    // The serial port has been opened. Save its id to use later.
    _this.connectionId = connectionInfo.connectionId;

    // Do whatever you need to do with the opened port.
    chrome.serial.onReceive.addListener(onReceiveCallback);
}

var stringReceived = '';
var onReceiveCallback = function(info) {
  if (info.connectionId == expectedConnectionId && info.data) {
      var str = convertArrayBufferToString(info.data);

      if (str.charAt(str.length-1) === '\n') {
          stringReceived += str.substring(0, str.length-1);
          onLineReceived(stringReceived);
          stringReceived = '';
      } 
      else {
        stringReceived += str;
      }
  }
};

but I get the following error:

但我收到以下错误:

Error in response to serial.connect: ReferenceError: _this is not defined at Object.onGetDevices [as callback]

响应 serial.connect 时出错:ReferenceError: _this is not defined at Object.onGetDevices [as callback]

I'm not sure exactly what I'm doing right or wrong here so any pointers appreciated.

我不确定我在这里做对了还是做错了,所以任何指示都表示赞赏。

回答by Joe Kasavage

First the example does not work properly. Try this instead:

首先,示例无法正常工作。试试这个:

var connectionId;

$(document).ready(function() {
    chrome.serial.getDevices(function(devices) {

        for (var i = 0; i < devices.length; i++) {
            $('select#portList').append('<option value="' + devices[i].path + '">' + devices[i].path + '</option>');
        }
    });

    // ui hook
    $('button#open').click(function() {
        var clicks = $(this).data('clicks');

        if (!clicks) {
            var port = $('select#portList').val();
            chrome.serial.connect(port, {bitrate: 9600}, function(info) {
                connectionId = info.connectionId;
                $("button#open").html("Close Port");
                console.log('Connection opened with id: ' + connectionId + ', Bitrate: ' + info.bitrate);
            });
        } else {
            chrome.serial.disconnect(connectionId, function(result) {
                $("button#open").html("Open Port");
                console.log('Connection with id: ' + connectionId + ' closed');
            });
        }

        $(this).data("clicks", !clicks);
    });
});

Now as for the actual reading of the input from the serial connection it will work but converting the ArrayBuffer to a string is a bit harder than expected.

现在至于从串行连接实际读取输入它会工作,但将 ArrayBuffer 转换为字符串比预期的要困难一些。