2013-03-08 17 views
1

我有一個條形碼掃描器連接到運行nodejs serialport的覆盆子pi。一個串口實例是聽/dev/input/event0Nodejs serialport data.toString()失敗的編碼

當我運行這個

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); 
}); 

我的git Buffer對象的BLOB從掃描。 (我認爲這對在該代碼作爲數據事件的每個號碼。

我的問題是,我甚至不能讓它converto適當的UTF8號碼。我怎樣才能得到的數據解碼?

更新-1:這是原始console.log(data)的掃描條形碼與數字之後的結果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

+0

你能告訴什麼樣的'data'緩衝區看起來像一個例子嗎?你的'console.log'顯示什麼? – loganfsmyth 2013-03-08 08:20:51

+0

已更新,以顯示'console.log'顯示的內容 – Misterparker 2013-03-09 22:05:09

+0

它看起來不像掃描儀正在輸出數字作爲人類可讀的值。你使用什麼類型的掃描儀?您是否查看過任何可用的文檔以查看輸出應該是什麼?它看起來像是從32a93b51開始發送重複的塊。你確定你有正確的波特率嗎? – loganfsmyth 2013-03-09 22:25:30

回答

0

你應該在這樣的模塊來看一看:https://github.com/Bornholm/node-keyboard

爲了詳細說明雖然,得到的答覆是,/dev/input/event0輸出不是ASCII字符。輸出是一系列結構,格式爲https://www.kernel.org/doc/Documentation/input/input.txt,格式爲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 
+0

太棒了。這次真是萬分感謝。我能夠看到輸出不是我在安裝它時使用'evtest'所期望的ASCII碼。然後,當我偶然發現https://github.com/nodebits/linux-joystick後,我正在尋找類似於這個鍵盤記錄器的東西,並且正在考慮在這種模式之後構建我自己的。但看起來像有人打敗了我。再次感謝 – Misterparker 2013-03-11 14:01:01