-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaas-serial.js
57 lines (46 loc) · 1.38 KB
/
haas-serial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const SerialPort = require('serialport')
const {promisify} = require('util')
const sleep = promisify(setTimeout)
module.exports = function (options) {
let _port
switch (process.platform) {
case 'win32':
_port = options.port || 'COM9'
break
case 'linux':
_port = options.port || '/dev/ttyUSB0'
break
default:
_port = options.port || 'COM9'
break
}
let serialcomm = new SerialPort(
_port,
{
baudRate: options.baudrate || 115200,
parity: options.parity || 'even',
stopBits: options.stopBits || 1,
dataBits: options.dataBits || 7
}
)
let parser = serialcomm.pipe(new SerialPort.parsers.Readline({delimiter: '\n'}))
let status = ''
parser.on('data', data => {
//console.log('Machine status: ' + data)
status += ' ' + data.trim()
})
serialcomm.on('error', err => {
console.log('Serial port error--> ' + err.message)
})
this.sendCommand = async (line) => {
status = ''
serialcomm.write(line + '\r\n', err => {
if(err) {
return console.log('Error writting to port ' + _port + '-->' + err.message)
}
//console.log('Command sent!')
})
await sleep(50)
return status
}
}