我有一个条形码扫描仪连接到运行nodejs串行端口的树莓派。串行端口实例正在监听 /dev/input/event0
当我运行这个
var serialport = require("serialport");
var SP = serialport.SerialPort;
var port = "/dev/input/event0"
var sp = new SP(port);
sp.on('data', function(data) {
console.log( data);
});
我从扫描中获取了 Buffer 对象的 blob。 (我认为它将代码中的每个数字都算作一个数据事件。
我的问题是,我什至无法将其转换为正确的 utf8 数字。我怎样才能解码数据?
Update-1:这是扫描带有数字 19024336
console.log(data)
的结果
<Buffer 32 a9 3b 51 54 76 0c 00 04 00 04 00 1e 00 07 00 32 a9 3b 51 6d 76 0c 00 01 00 02 00 01 00 00 00 32 a9 3b 51 76 76 0c 00 00 00 00 00 00 00 00 00>
<Buffer 32 a9 3b 51 c6 8d 0c 00 04 00 04 00 1e 00 07 00 32 a9 3b 51 dc 8d 0c 00 01 00 02 00 00 00 00 00 32 a9 3b 51 e2 8d 0c 00 04 00 04 00 26 00 07 00 32 a9 3b ...>
<Buffer 32 a9 3b 51 61 cc 0c 00 04 00 04 00 1f 00 07 00 32 a9 3b 51 78 cc 0c 00 01 00 03 00 00 00 00 00 32 a9 3b 51 7e cc 0c 00 04 00 04 00 21 00 07 00 32 a9 3b ...>
<Buffer 32 a9 3b 51 5f fb 0c 00 00 00 00 00 00 00 00 00 32 a9 3b 51 e4 0a 0d 00 04 00 04 00 20 00 07 00 32 a9 3b 51 fc 0a 0d 00 01 00 04 00 00 00 00 00 32 a9 3b ...>
<Buffer 32 a9 3b 51 f6 d5 0d 00 04 00 04 00 28 00 07 00 32 a9 3b 51 0a d6 0d 00 01 00 1c 00 00 00 00 00 32 a9 3b 51 12 d6 0d 00 00 00 00 00 00 00 00 00>
每次扫描时它都会返回不同的结果(来 self 的人眼在这里可以读取的结果)
您可能还想知道 Buffer.isBuffer(data);
返回 true
最佳答案
您应该看一下这样的模块:https://github.com/Bornholm/node-keyboard
详细来说,答案是 /dev/input/event0
的输出不是 ASCII 字符。输出是一系列采用此处描述的格式的结构 https://www.kernel.org/doc/Documentation/input/input.txt作为struct input_event
。
例如,这意味着给定输出的第二 block
32 a9 3b 51 54 76 0c 00 04 00 04 00 1e 00 07 00
分解如下:
// 32-bit int timestamp (1362864434) - Sat, 09 Mar 2013 21:27:14 GMT
32 a9 3b 51
// microsecond-granularity time
54 76 0c 00
// 16-bit short (1) indicating it is a key event
01 00
// 16-bit short (2) is the keycode for the key '1'.
02 00
// 32-bit int (1) indicating it was a keypress.
01 00 00 00
关于javascript - Nodejs串口data.toString()编码失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15287984/