Readline in serialport is a class, needs to be constructed.
This commit is contained in:
parent
1807f5881b
commit
d311ac4727
|
@ -1,7 +1,10 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const SerialPort = require('serialport')
|
const SerialPort = require('serialport')
|
||||||
|
const Readline = SerialPort.parsers.Readline
|
||||||
const exec = require('child_process').exec
|
const exec = require('child_process').exec
|
||||||
|
const parser = new Readline('')
|
||||||
|
const newlineRe = new RegExp('\n', 'g')
|
||||||
let eventEmitter
|
let eventEmitter
|
||||||
|
|
||||||
const mcopy = {}
|
const mcopy = {}
|
||||||
|
@ -107,8 +110,8 @@ mcopy.arduino.connect = function (serial, device, confirm, callback) {
|
||||||
mcopy.arduino.alias[serial] = device;
|
mcopy.arduino.alias[serial] = device;
|
||||||
mcopy.arduino.serial[device] = new SerialPort(mcopy.arduino.path[serial], {
|
mcopy.arduino.serial[device] = new SerialPort(mcopy.arduino.path[serial], {
|
||||||
autoOpen : false,
|
autoOpen : false,
|
||||||
baudrate: mcopy.cfg.arduino.baud,
|
baudRate: mcopy.cfg.arduino.baud,
|
||||||
parser: SerialPort.parsers.readline("\n")
|
parser: parser
|
||||||
});
|
});
|
||||||
mcopy.arduino.serial[device].open(error => {
|
mcopy.arduino.serial[device].open(error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -118,13 +121,16 @@ mcopy.arduino.connect = function (serial, device, confirm, callback) {
|
||||||
console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`);
|
console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`);
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
mcopy.arduino.serial[device].on('data', data => {
|
mcopy.arduino.serial[device].on('data', data => {
|
||||||
data = data.replace('\r', '')
|
let d = data.toString('utf8')
|
||||||
mcopy.arduino.end(data)
|
|
||||||
|
d = d.replace(newlineRe, '')
|
||||||
|
mcopy.arduino.end(d)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
mcopy.arduino.serial[device].on('data', data => {
|
mcopy.arduino.serial[device].on('data', data => {
|
||||||
data = data.replace('\r', '')
|
let d = data.toString('utf8')
|
||||||
mcopy.arduino.confirmEnd(data)
|
d = d.replace(newlineRe, '')
|
||||||
|
mcopy.arduino.confirmEnd(d)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (callback) {
|
if (callback) {
|
||||||
|
|
Loading…
Reference in New Issue