Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome.serial.onReceive事件在响应中从设备中划分字节_Javascript_Google Chrome_Serial Port_Google Chrome App - Fatal编程技术网

Javascript Chrome.serial.onReceive事件在响应中从设备中划分字节

Javascript Chrome.serial.onReceive事件在响应中从设备中划分字节,javascript,google-chrome,serial-port,google-chrome-app,Javascript,Google Chrome,Serial Port,Google Chrome App,我正在为Google Chrome构建一个应用程序,通过COM(usb)端口从设备中获取一些字节 守则: // connects the device function connect(port_name, timeout) { chrome.serial.connect(port_name, { bitrate: 115200, bufferSize: 32768, dataBits: "eight", stopBits:

我正在为Google Chrome构建一个应用程序,通过COM(usb)端口从设备中获取一些字节

守则:

// connects the device
function connect(port_name, timeout) {
    chrome.serial.connect(port_name, {
        bitrate: 115200,
        bufferSize: 32768,
        dataBits: "eight",
        stopBits: "one",
        parityBit: "no",
        sendTimeout: timeout,
        receiveTimeout: timeout
    }, function(connectionInfo) {
        if (typeof (connectionInfo) !== 'undefined') {
            if (typeof (connectionInfo.connectionId) !== 'undefined' && connectionInfo.connectionId < 1) {
                console.log('Connection error #1');
            } else {
                sessionStorage.setItem('connection', connectionInfo.connectionId);
            }
        }
    })
}
// sends bytes to device
function send_bytes(bytes) {
    var bytes_to_send = new Uint8Array(bytes);
    var connection = Number(sessionStorage.getItem('connection'));
    chrome.serial.send(connection, (bytes_to_send).buffer, function(sent_data) {});
}
// recieves the data
chrome.serial.onReceive.addListener(function(data_recieved) {
    var arr = new Uint8Array(data_recieved.data);
    var final_hex = [];
    for (byte in arr) {
        final_hex.push(arr[byte].toString(16));
    }
    console.log('====== HEX ======');
    console.log(final_hex);
});
通常我会收到正确的十六进制序列:

["6", "2", "16", "ad", "0", "0", "0", "0", "0", "0", "10", "a", "6", "10", "20", "58", "2", "0", "0", "b5", "c0", "ea", "6a", "0", "c", "34"]
但有时我会分别接收两个字节。onReceive回调:

 ["6", "2", "16", "ad", "0", "0", "0", "0", "0", "0", "10", "a", "6", "10", "20"]
 ["58", "2", "0", "0", "b5", "c0", "ea", "6a", "0", "c", "34"]
乍一看,之所以会出现这种情况,是因为Chrome认为该设备完成了数据发送,而下一部分数据就像来自该设备的新答案。 在API文档中,我没有找到“在接收设备响应时等待下一个字节的时间”的选项


如何防止chrome.serial.onReceive分离字节序列?

如果传入数组小于最小响应长度,请保存块并在下一个事件中处理它

var MIN_RESPONSE_LENGTH = 26;
var incoming = new Uint8Array();

chrome.serial.onReceive.addListener(function(info) {
    appendBuffer(info.data);
    if (incoming.length < MIN_RESPONSE_LENGTH) {
        setTimeout(function() {
            console.log('Timeout waiting for the data, got only:', incoming);
        }, 1000);
        return;
    }
    // process
    console.log(incoming);
    ................
    // reset
    incoming = new Uint8Array();
});

function appendBuffer(arraybuffer) {
    var tmp = new Uint8Array(incoming.length + arraybuffer.byteLength);
    tmp.set(incoming, 0);
    tmp.set(new Uint8Array(arraybuffer), incoming.length);
    incoming = tmp;
}
var最小响应长度=26;
var incoming=新的Uint8Array();
chrome.serial.onReceive.addListener(函数(信息)){
附加缓冲区(信息数据);
if(输入长度<最小响应长度){
setTimeout(函数(){
log('等待数据超时,仅获取:',传入);
}, 1000);
返回;
}
//过程
控制台日志(传入);
................
//重置
传入=新的Uint8Array();
});
函数appendBuffer(arraybuffer){
var tmp=新的UINT8阵列(incoming.length+arraybuffer.ByTeleLength);
tmp.set(输入,0);
tmp.set(新的Uint8Array(arraybuffer),传入的.length);
传入=tmp;
}

您没有构建扩展。这是一个应用程序,因为chrome.serial仅适用于应用程序。可能在缓冲区几乎满的时候发生。如果响应长度始终不变,请尝试将缓冲区大小设置为倍数,例如26*1000。但是为什么不能正确使用分块响应?读取数据后也可以尝试刷新。@wOxxOm我尝试设置一个32*1024的缓冲区,没有效果。@wOxxOm我需要response中的所有字节来按顺序读取确切的字节,例如,我需要第14字节的数据:parseInt(receivedbytes[14],16)。toString(2)[5]=0;如果我只使用字节的一部分,那么我从[14]中得到的值可能是错误的,或者如果receivedBytes.length<14,则只是“未定义”@Xan还尝试在.onReceive事件之后刷新连接-使用分块字节的相同故事您的代码解决了问题,但我仍然认为正确的解决方案必须是通过chrome app api提供的回调函数参数。不过,谢谢你的回答。
var MIN_RESPONSE_LENGTH = 26;
var incoming = new Uint8Array();

chrome.serial.onReceive.addListener(function(info) {
    appendBuffer(info.data);
    if (incoming.length < MIN_RESPONSE_LENGTH) {
        setTimeout(function() {
            console.log('Timeout waiting for the data, got only:', incoming);
        }, 1000);
        return;
    }
    // process
    console.log(incoming);
    ................
    // reset
    incoming = new Uint8Array();
});

function appendBuffer(arraybuffer) {
    var tmp = new Uint8Array(incoming.length + arraybuffer.byteLength);
    tmp.set(incoming, 0);
    tmp.set(new Uint8Array(arraybuffer), incoming.length);
    incoming = tmp;
}