【问题标题】:chrome extension - reading from serial portchrome 扩展 - 从串口读取
【发布时间】:2014-09-19 02:20:58
【问题描述】:

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

运行此代码

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);

在控制台中获取“/dev/ttyUSB0”,因此它似乎正在查找设备。

然后如何连接到设备?我已经包含了上面的 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;
      }
  }
};

但我收到以下错误:

响应 serial.connect 时出错:ReferenceError: _this is not defined 在 Object.onGetDevices [作为回调]

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

【问题讨论】:

  • 您的代码中似乎没有定义变量_this
  • 代码是直接取自 chromes 教程示例,是否需要定义,如果需要定义呢?链接在这里developer.chrome.com/apps/app_serial
  • 这很好,很好的问题

标签: javascript google-chrome serial-port google-chrome-app


【解决方案1】:

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

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);
    });
});

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

【讨论】:

  • 太棒了,谢谢乔,这真的让我走上了正轨。不敢相信 chrome 教程没有正确的工作代码..!
  • 没问题。我花了一点时间自己弄清楚。
  • Joe - 我现在连接到端口时遇到问题。很奇怪,就是不想打球。你有什么东西开始运行了吗?
  • 是的,我使用这个确切的代码从串行设备读取,在这种情况下是条形码扫描仪。我假设即使您设置了比特率,它也无法打开?
  • 可能是这样。尝试使用 chrome.usb 库,看看是否有帮助。
猜你喜欢
  • 2012-08-20
  • 2015-05-05
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 2012-03-08
  • 1970-01-01
相关资源
最近更新 更多