简体   繁体   English

Chrome.serial API - 通过串行读取正确的字节数

[英]Chrome.serial API - reading the right number of bytes via serial

I'm working on a Google Chrome extension that communicates with an Arduino UNO over serial. 我正在开发一个谷歌Chrome扩展程序,通过串口与Arduino UNO进行通信。 I wrote the following function sendSerialCmd that takes a port (string), a serial_cmd (ArrayBuffer), and a callback function that gets passed readInfo.data (an ArrayBuffer) that is read in from the serial connection. 我编写了以下函数sendSerialCmd ,它接受一个端口(字符串),一个serial_cmd(ArrayBuffer),以及一个从串行连接读入的传递readInfo.data(一个ArrayBuffer)的回调函数。

var CONNECTION_ID = -1;
var sendSerialCmd = function(port, serial_cmd, callback) {
chrome.serial.open(port, null, function(openInfo){
    CONNECTION_ID = openInfo.connectionId;
    if (CONNECTION_ID == -1) {
        console.log('Could not connect to serial');
        return;
    }
    chrome.serial.write(CONNECTION_ID, serial_cmd, function(writeInfo){
        chrome.serial.read(CONNECTION_ID, 8, function(readInfo){
            callback(readInfo.data);
        });
    })
});
chrome.serial.close(CONNECTION_ID, function(result){ console.log(result) });
};

One of the problems I've run into is the third parameter passed to the chrome.serial.read() function. 我遇到的一个问题是传递给chrome.serial.read()函数的第三个参数。 In the chrome.serial API, the third parameter is the bytesToRead ( integer ): The number of bytes to read , however, the number of bytes coming in from my Arduino are subject to change. chrome.serial API中,第三个参数是bytesToRead(整数):要读取的字节数 ,但是,从我的Arduino进入的字节数可能会发生变化。 Sometimes I may get 8 bytes, other times more. 有时我可能会得到8个字节,有时候会得到更多。 What's the best way of getting all the bytes sent back from my Arduino? 获取从Arduino发回的所有字节的最佳方法是什么?

Arduino has a novel solution through a function called Serial.available() which returns the number of bytes available to read. Arduino通过一个名为Serial.available()的函数提供了一种新颖的解决方案,它返回可读取的字节数。 Is there something similar I can do with the Chrome.serial API? 我可以使用Chrome.serial API做类似的事吗?

There is no similar functionality to Serial.available() in the Chrome serial API, but you should just poll for some reasonable number of bytes and process the data at your own pace. Chrome串行API中的Serial.available()没有类似的功能,但您应该轮询一些合理的字节数并按照自己的进度处理数据。

If a read is requested for (say) 1024 bytes and only 8 bytes are available, the read should still succeed quickly with just 8 bytes. 如果请求read (例如)1024字节并且只有8个字节可用,则只需8个字节即可快速read If there is an 8-byte message and a 12-byte message available, the read will succeed with 20 bytes and you can apply whatever logic is necessary to parse the resulting data. 如果有一个8字节的消息和一个12字节的消息可用,则read将以20个字节成功,您可以应用解析结果数据所需的任何逻辑。

FYI, the serial API is changing soon in Canary and you will no longer be responsible for manually polling with read . 仅供参考,串行API即将在Canary中更改,您将不再负责手动轮询read Instead Chrome will poll for you and fire chrome.serial.onReceive events as data is available from an open device. 相反,Chrome会为您进行投票并触发chrome.serial.onReceive事件,因为可以从打开的设备获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM