first
This commit is contained in:
commit
6d5f7faa69
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
node_modules/
|
37
deviceObject.js
Normal file
37
deviceObject.js
Normal 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
1608
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
package.json
Normal file
15
package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user