-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,228 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const SerialPort = require('serialport'); | ||
const createReader = require('./reader'); | ||
|
||
class HCHO extends SerialPort { | ||
constructor(dev){ | ||
super(dev); | ||
const reader = createReader({ | ||
LEN_LENGTH: 1 | ||
}, message => { | ||
console.log(message); | ||
}); | ||
this.on('data', reader); | ||
} | ||
send(cmd, data){ | ||
let checksum = 0; | ||
const packet = Buffer.alloc(7); | ||
packet.writeUIntBE(0x42, 0); checksum+=0x42; | ||
packet.writeUIntBE(0x4d, 1); checksum+=0x4d; | ||
packet.writeUIntBE(cmd, 2); checksum+=cmd; | ||
packet.writeUInt16BE(data, 3); checksum+=data; | ||
packet.writeUInt16BE(checksum, 5); | ||
return this.write(packet); | ||
} | ||
} | ||
|
||
module.exports = HCHO; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
const EventEmitter = require('events'); | ||
const createReader = require('./reader'); | ||
|
||
/** | ||
* parse sensor data | ||
*/ | ||
function parse(buffer){ | ||
var data = { | ||
Head : buffer.readUInt16BE(00), | ||
Length : buffer.readUInt16BE(02), | ||
PM1_0 : buffer.readUInt16BE(04), | ||
PM2_5 : buffer.readUInt16BE(06), | ||
PM10 : buffer.readUInt16BE(08), | ||
PM1_0_ : buffer.readUInt16BE(10), | ||
PM2_5_ : buffer.readUInt16BE(12), | ||
PM10_ : buffer.readUInt16BE(14), | ||
UM0_3 : buffer.readUInt16BE(16), | ||
UM0_5 : buffer.readUInt16BE(18), | ||
UM1_0 : buffer.readUInt16BE(20), | ||
UM2_5 : buffer.readUInt16BE(22), | ||
UM5_0 : buffer.readUInt16BE(24), | ||
UM10 : buffer.readUInt16BE(26), | ||
VERSION : buffer.readUInt8(28), | ||
ERRCODE : buffer.readUInt8(29), | ||
CHECKSUM: buffer.readUInt16BE(30) | ||
}; | ||
var checksum = 0; | ||
for(var i=0;i<buffer.length - 2;i++){ | ||
checksum += buffer[i]; | ||
} | ||
assert.equal(data.Head, 0x424d, 'Invalid data header'); | ||
assert.equal(checksum, data.CHECKSUM, 'Checksum error'); | ||
return data; | ||
} | ||
|
||
/** | ||
* DSensor | ||
*/ | ||
class DSensor extends EventEmitter { | ||
constructor(dev){ | ||
super(); | ||
if(typeof dev === 'undefined') dev = process.stdin; | ||
if(typeof dev === 'string') dev = fs.createReadStream(dev); | ||
const reader = createReader(data => { | ||
const message = parse(data); | ||
this.emit('data', message); | ||
}); | ||
dev.on('data', reader); | ||
return this; | ||
} | ||
} | ||
|
||
module.exports = DSensor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
const { HCHO } = require('..'); | ||
|
||
const DSensor = require('..'); | ||
|
||
var sensor = new DSensor('/dev/cu.usbserial'); | ||
|
||
sensor.on('data', function(data){ | ||
console.log(data); | ||
}); | ||
|
||
const hc = new HCHO("/dev/xxx"); | ||
|
||
hc.send(0xe2, 0xaabb); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,10 @@ | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const EventEmitter = require('events'); | ||
|
||
const STATE_HEADER_1 = 0x42; | ||
const STATE_HEADER_2 = 0x4d; | ||
const STATE_LENGTH = 2; | ||
const STATE_DATA = 3; | ||
class DSensor extends EventEmitter { | ||
|
||
const LEN_HEADER = 2; | ||
const LEN_LENGTH = 2; | ||
const LEN_CHECKSUM = 2; | ||
|
||
/** | ||
* DSensor | ||
*/ | ||
function DSensor(dev){ | ||
EventEmitter.call(this); | ||
if(!(this instanceof DSensor)) return new DSensor(dev); | ||
var flag = STATE_HEADER_1, index, len, buffer = new Buffer([]); | ||
if(typeof dev === 'undefined') dev = process.stdin; | ||
if(typeof dev === 'string') dev = fs.createReadStream(dev); | ||
dev.on('data', function(chunk){ | ||
buffer = Buffer.concat([ buffer, chunk ]); | ||
for(var i = 0; i < buffer.length; i++){ | ||
if(flag === STATE_HEADER_1 && buffer[i] === STATE_HEADER_1){ | ||
flag = STATE_HEADER_2; | ||
continue; | ||
} | ||
if(flag === STATE_HEADER_2 && buffer[i] === STATE_HEADER_2){ | ||
flag = STATE_LENGTH; | ||
index = ++i; | ||
continue; | ||
} | ||
if(flag === STATE_LENGTH && buffer.length - index >= LEN_LENGTH){ | ||
len = buffer.readUInt16BE(index); | ||
flag = STATE_DATA; | ||
continue; | ||
} | ||
if(flag === STATE_DATA && buffer.length - index >= len + LEN_CHECKSUM){ | ||
this.parse(buffer.slice(index - LEN_HEADER, index + len + LEN_CHECKSUM)); | ||
buffer = buffer.slice(index + len + LEN_CHECKSUM); | ||
flag = STATE_HEADER_1; | ||
} | ||
} | ||
}.bind(this)); | ||
return this; | ||
} | ||
|
||
util.inherits(DSensor, EventEmitter); | ||
/** | ||
* parse sensor data | ||
*/ | ||
DSensor.prototype.parse = function(buffer){ | ||
var data = { | ||
Head : buffer.readUInt16BE(00), | ||
Length : buffer.readUInt16BE(02), | ||
PM1_0 : buffer.readUInt16BE(04), | ||
PM2_5 : buffer.readUInt16BE(06), | ||
PM10 : buffer.readUInt16BE(08), | ||
PM1_0_ : buffer.readUInt16BE(10), | ||
PM2_5_ : buffer.readUInt16BE(12), | ||
PM10_ : buffer.readUInt16BE(14), | ||
UM0_3 : buffer.readUInt16BE(16), | ||
UM0_5 : buffer.readUInt16BE(18), | ||
UM1_0 : buffer.readUInt16BE(20), | ||
UM2_5 : buffer.readUInt16BE(22), | ||
UM5_0 : buffer.readUInt16BE(24), | ||
UM10 : buffer.readUInt16BE(26), | ||
VERSION : buffer.readUInt8(28), | ||
ERRCODE : buffer.readUInt8(29), | ||
CHECKSUM: buffer.readUInt16BE(30) | ||
}; | ||
var checksum = 0; | ||
for(var i=0;i<buffer.length - 2;i++){ | ||
checksum += buffer[i]; | ||
} | ||
if(data.Head === 0x424d && data.CHECKSUM === checksum){ | ||
this.emit('data', data); | ||
} | ||
return this; | ||
} | ||
DSensor.HCHO = require('./HCHO'); | ||
DSensor.PMS5003 = require('./PMS5003'); | ||
|
||
module.exports = DSensor; | ||
module.exports = DSensor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
function createReader({ | ||
LEN_HEADER = 2, | ||
LEN_LENGTH = 2, | ||
LEN_CHECKSUM = 2 | ||
}, fn){ | ||
const STATE_HEADER_1 = 0x42; | ||
const STATE_HEADER_2 = 0x4d; | ||
const STATE_LENGTH = 2; | ||
const STATE_DATA = 3; | ||
var flag = STATE_HEADER_1, index, len, buffer = Buffer.alloc(0); | ||
return function(chunk){ | ||
buffer = Buffer.concat([ buffer, chunk ]); | ||
for(var i = 0; i < buffer.length; i++){ | ||
if(flag === STATE_HEADER_1 && buffer[i] === STATE_HEADER_1){ | ||
flag = STATE_HEADER_2; | ||
continue; | ||
} | ||
if(flag === STATE_HEADER_2 && buffer[i] === STATE_HEADER_2){ | ||
flag = STATE_LENGTH; | ||
index = ++i; | ||
continue; | ||
} | ||
if(flag === STATE_LENGTH && buffer.length - index >= LEN_LENGTH){ | ||
len = buffer.readUInt16BE(index); | ||
flag = STATE_DATA; | ||
continue; | ||
} | ||
if(flag === STATE_DATA && buffer.length - index >= len + LEN_CHECKSUM){ | ||
fn(buffer.slice(index - LEN_HEADER, index + len + LEN_CHECKSUM)); | ||
buffer = buffer.slice(index + len + LEN_CHECKSUM); | ||
flag = STATE_HEADER_1; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
module.exports = createReader; |
Oops, something went wrong.