-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
65 lines (50 loc) · 1.06 KB
/
index.ts
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
58
59
60
61
62
63
64
65
import * as serialport from "serialport"
import * as express from "express"
const PORT_NAME = 'COM3'
const html = `
<html>
<head>
<title>Remote controller</title>
</head>
<body>
<a href="/on"><h1>Turn on</h1></a><br>
<a href="/off"><h1>Turn off</h1></a><br>
</body>
</html>
`
var sp = new serialport.SerialPort(PORT_NAME, {
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false,
parser: serialport.parsers.readline("\r\n")
});
sp.on('data', function (input) {
console.log(input);
});
let app = express()
app.get('/', function (req, res) {
res.send(html)
})
app.get('/on', function (req, res) {
console.log("Turn on")
sp.write("on", err => {
if (err) {
console.log(err)
}
res.send(html)
});
})
app.get('/off', function (req, res) {
console.log("Turn off")
sp.write("off", err => {
if (err) {
console.log(err)
}
res.send(html)
});
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})