This commit is contained in:
Jay 2018-10-26 21:18:37 +08:00
commit 6d5f7faa69
4 changed files with 1661 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

37
deviceObject.js Normal file
View File

@ -0,0 +1,37 @@
const SerialPort = require('serialport')
const EventEmitter = require('events')
class DeviceObject extends EventEmitter {
constructor (port, opts = {}) {
super()
this._port = port
this._spOpts = opts
this._sp = null
this.isOpen = false
}
connect () {
this._sp = new SerialPort(this._port, this._spOpts)
this._sp.open(() => {
this.isOpen = true
this.emit('open')
})
this._sp.on('close', () => {
this.isOpen = false
this.emit('close')
}).on('data', data => {
this.emit('data', data)
})
}
static async getPorts () {
try {
let list = SerialPort.list()
return list
} catch (err) {
return null
}
}
}
module.exports = DeviceObject

1608
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

15
package.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "serialport_class",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"serialport": "^7.0.2"
}
}