Chrome.serial.onReceive 事件划分字节以响应设备
Chrome.serial.onReceive event divides bytes in responce from device
我正在为 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);
});
如果我 运行 控制台中的下一个代码几次:
connect('COM5',15000); // we connected!
send_bytes([0x05, 0xAD, 0x1E, 0x00, 0x00, 0x00, 0xB6]); // bytes away! ( runs N times)
通常我会收到正确的 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 文档中没有找到 "time to wait for the next byte while receiving responce from device" 的任何选项。
如何防止字节序列被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;
}
我正在为 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);
});
如果我 运行 控制台中的下一个代码几次:
connect('COM5',15000); // we connected!
send_bytes([0x05, 0xAD, 0x1E, 0x00, 0x00, 0x00, 0xB6]); // bytes away! ( runs N times)
通常我会收到正确的 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 文档中没有找到 "time to wait for the next byte while receiving responce from device" 的任何选项。
如何防止字节序列被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;
}