Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
lsongdev committed Oct 15, 2018
1 parent 022c795 commit c0c19cb
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 115 deletions.
96 changes: 34 additions & 62 deletions HCHO.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const assert = require('assert');
const SerialPort = require('serialport');

const COMMANDS = {
QUERY: 0x01
};
const DSensor = require('.');
const assert = require('assert');

const TYPES = {
0x00: 'NoSensor',
Expand Down Expand Up @@ -49,34 +45,41 @@ const UNITS = {
0x05: 'Mg/m3',
};

function parse(raw){
const data = {
HEAD : raw.readUInt16BE(0),
LENGTH : raw.readUIntBE(2, 1),
TYPE : raw.readUIntBE(3, 1),
UNIT : raw.readUIntBE(4, 1),
VH : raw.readUIntBE(5, 1),
VALUE : raw.readUInt16BE(6),
CHECKSUM: raw.readUInt16BE(8),
};
var checksum = 0;
for(var i=0;i<raw.length - 2;i++){
checksum += raw[i];
class HCHO extends DSensor {
constructor(dev){
super(dev, createReader);
}
assert.equal(data.HEAD, 0x424d, 'Invalid data header');
assert.equal(checksum, data.CHECKSUM, 'Checksum error');
return {
raw,
data,
type: TYPES[data.TYPE],
unit: UNITS[data.UNIT],
value: data.VALUE / 10 ** data.VH,
toString(){
return `${this.type}: ${this.value}${this.unit}`;
parse(raw){
const data = {
HEAD : raw.readUInt16BE(0),
LENGTH : raw.readUIntBE(2, 1),
TYPE : raw.readUIntBE(3, 1),
UNIT : raw.readUIntBE(4, 1),
VH : raw.readUIntBE(5, 1),
VALUE : raw.readUInt16BE(6),
CHECKSUM: raw.readUInt16BE(8),
};
var checksum = 0;
for(var i=0;i<raw.length - 2;i++){
checksum += raw[i];
}
};
assert.equal(data.HEAD, 0x424d, 'Invalid data header');
assert.equal(checksum, data.CHECKSUM, 'Checksum error');
return {
raw,
data,
type: TYPES[data.TYPE],
unit: UNITS[data.UNIT],
value: data.VALUE / 10 ** data.VH,
toString(){
return `${this.type}: ${this.value}${this.unit}`;
}
};
}
}

module.exports = HCHO;

function createReader(fn){
const STATE_HEADER_1 = 0x42;
const STATE_HEADER_2 = 0x4d;
Expand Down Expand Up @@ -109,35 +112,4 @@ function createReader(fn){
}
}
};
}

class HCHO extends SerialPort {
constructor(dev){
super(dev);
this.on('data', createReader(data => {
const message = parse(data);
this.emit('message', message);
}));
}
send(cmd, data = 0x00){
let checksum = 0;
if(typeof cmd === 'undefined')
throw new TypeError('cmd must be a number or string');
if(typeof data === 'undefined')
throw new TypeError('data must be a number or string');
if(typeof cmd === 'string'){
cmd = COMMANDS[`${cmd}`.toUpperCase()];
}
cmd = parseInt(cmd);
data = parseInt(data);
const packet = Buffer.alloc(7);
packet.writeUIntBE(0x42, 0, 1); checksum+=0x42;
packet.writeUIntBE(0x4d, 1, 1); checksum+=0x4d;
packet.writeUIntBE(cmd, 2, 1); checksum+=cmd;
packet.writeUInt16BE(data, 3); checksum+=data;
packet.writeUInt16BE(checksum, 5);
return this.write(packet);
}
}

module.exports = HCHO;
}
80 changes: 36 additions & 44 deletions PMS5003.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@
const assert = require('assert');
const SerialPort = require('serialport');

/**
* parse sensor data
*/
function parse(buffer){
const 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;
}
const DSensor = require('.');
const assert = require('assert');

/**
* DSensor
*/
class DSensor extends SerialPort {
class PMS5003 extends DSensor {
constructor(dev){
super(dev);
const reader = createReader(data => {
const message = parse(data);
this.emit('message', message);
});
dev.on('data', reader);
super(dev, createReader);
return this;
}
/**
* parse sensor data
*/
parse(buffer){
const data = {
HEAD : buffer.readUInt16BE(0),
LENGTH : buffer.readUInt16BE(2),
PM1_0 : buffer.readUInt16BE(4),
PM2_5 : buffer.readUInt16BE(6),
PM10 : buffer.readUInt16BE(8),
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;
}
}

module.exports = DSensor;


module.exports = PMS5003;

function createReader(fn){
const STATE_HEADER_1 = 0x42;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $ npm i [-g] dsensor
### Example

```js
const { HCHO } = require('dsensor');
const HCHO = require('dsensor/HCHO');

const sensor = new HCHO("/dev/cu.SLAB_USBtoUART");

Expand Down
2 changes: 1 addition & 1 deletion bin/dsensor
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ program()
.command('help', () => {
console.log();
console.log('~$ dsensor <command> [options]');
console.log('~$ dsensor send --type=HCHO --device=/dev/cu.SLAB_USBtoUART --command=query --data=0x00');
console.log('~$ dsensor send --type=HCHO --device=/dev/cu.SLAB_USBtoUART --command=0x01 --data=0x00');
})
.command('send', ({ type, device, command, data, watch }) => {
const Adapter = require(`../${type}`);
Expand Down
9 changes: 8 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { HCHO } = require('..');
const PMS5003 = require('../PMS5003');
const HCHO = require('../HCHO');

const hc = new HCHO("/dev/cu.SLAB_USBtoUART");

Expand All @@ -10,4 +11,10 @@ hc.on("open", () => {
setInterval(() => {
hc.send(0x01, 0x00);
}, 3000)
});

const pm = new PMS5003('/dev/cu.usbserial');

pm.on('message', data => {
console.log(data);
});
34 changes: 28 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
const EventEmitter = require('events');

class DSensor extends EventEmitter {
const SerialPort = require('serialport');

class DSensor extends SerialPort {
constructor(dev, reader){
super(dev);
this.on('data', reader(data => {
const message = this.parse(data);
this.emit('message', message, data);
}));
}
send(cmd, data = 0x00){
let checksum = 0;
if(typeof cmd === 'undefined')
throw new TypeError('cmd must be a number or string');
if(typeof data === 'undefined')
throw new TypeError('data must be a number or string');
if(typeof cmd === 'string'){
cmd = COMMANDS[`${cmd}`.toUpperCase()];
}
cmd = parseInt(cmd);
data = parseInt(data);
const packet = Buffer.alloc(7);
packet.writeUIntBE(0x42, 0, 1); checksum+=0x42;
packet.writeUIntBE(0x4d, 1, 1); checksum+=0x4d;
packet.writeUIntBE(cmd, 2, 1); checksum+=cmd;
packet.writeUInt16BE(data, 3); checksum+=data;
packet.writeUInt16BE(checksum, 5);
return this.write(packet);
}
}

DSensor.HCHO = require('./HCHO');
DSensor.PMS5003 = require('./PMS5003');

module.exports = DSensor;

0 comments on commit c0c19cb

Please sign in to comment.