2017-06-29 79 views
1

有了這樣STX(0x02)..Data..ETX(0x03)節點串口的自定義解析器?

我可以byte sequence parser處理數據進來的數據:

var SerialPort = require('serialport'); 

var port = new SerialPort('/dev/tty-usbserial1', { 
    parser: SerialPort.parsers.byteDelimiter([3]) 
}); 

port.on('data', function (data) { 
    console.log('Data: ' + data); 
}); 

但我的實際進來的數據爲STX(0x02)..Data..ETX(0x03)..XX(plus 2 characters to validate data)

我怎樣才能得到適當的數據?

謝謝!

回答

2

解決!

我寫我自己的解析器:

var SerialPort = require('serialport'); 
var incommingData = new Buffer(0); 
var myParser = function(emitter, buffer) { 
    incommingData = Buffer.concat([incommingData, buffer]); 
    if (incommingData.length > 3 && incommingData[incommingData.length - 3] == 3) { 
     emitter.emit("data", incommingData); 
     incommingData = new Buffer(0); 
    } 
}; 
var port = new SerialPort('COM1', {parser: myParser}); 

port.on('data', function(data) { 
    console.log(data); 
});