1

我有一个条形码扫描仪连接到运行 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 对象。(我认为它将代码中的每个数字都计为一个数据事件。

我的问题是,我什至无法将其转换为正确的 utf8 数字。我怎样才能得到解码的数据?

console.log(data)Update-1:这是扫描带有数字的条形码后的原始结果19024336

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

每次扫描时,它都会返回不同的结果(从我的人眼可以在这里读取的内容)

您可能还想知道Buffer.isBuffer(data);返回 true

4

1 回答 1

0

你应该看看这样的模块:https ://github.com/Bornholm/node-keyboard

不过要详细说明,答案是 的输出/dev/input/event0不是 ASCII 字符。输出是此处描述的格式的一系列结构https://www.kernel.org/doc/Documentation/input/input.txt as struct input_event

例如,这意味着给定输出的第二块

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
于 2013-03-10T02:03:37.483 回答