-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp.js
More file actions
117 lines (109 loc) · 3.17 KB
/
sp.js
File metadata and controls
117 lines (109 loc) · 3.17 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var SerialPort = require("serialport").SerialPort;
var serialport = require("serialport");
var app = require('express')();
var express = require('express');
var server = require('http').Server(app);
var io = require('socket.io')(server);
var path = require('path');
var fs = require('fs');
var dataBuffer;
var serialPort = new SerialPort("COM5", {
parser: serialport.parsers.readline("\n"),
baudrate: 9600
}, false); // this is the openImmediately flag [default is true]
// app.set('view engine', 'jade');
// app.set('views', path.join(__dirname, 'tempaltes'));
app.use('/js/', express.static('./js'))
app.use('/css/', express.static('./css'))
app.use('/imgs/', express.static('./imgs'))
app.use('/fonts/', express.static('./fonts'))
server.listen(8000);
app.get('/', function(req, res) {
fs.readFile(__dirname + '/modeA.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading modeA.html');
}
res.writeHead(200);
res.end(data);
});
});
app.get('/MODE-B', function(req, res) {
fs.readFile(__dirname + '/modeB.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading modeB.html');
}
res.writeHead(200);
res.end(data);
});
});
app.get('/MODE-A', function(req, res) {
fs.readFile(__dirname + '/modeA.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading modeA.html');
}
res.writeHead(200);
res.end(data);
});
});
app.get('/SENSOR-MONITOR', function(req, res) {
fs.readFile(__dirname + '/modeOptionSensorMonitor.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading modeOptionSensorMonitor.html');
}
res.writeHead(200);
res.end(data);
});
});
app.get('/CALIBRATION', function(req, res) {
fs.readFile(__dirname + '/modeOptionCalibration.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading modeOptionCalibration.html');
}
res.writeHead(200);
res.end(data);
});
});
serialPort.open(function(error) {
if (error) {
console.log('failed to open: ' + error);
} else {
console.log('open');
io.on('connection', function(socket) {
serialPort.on('data', function(data) {
// socket.emit('dataishere', data.split('\t'))
try {
dataBuffer = JSON.parse(data);
// console.log(dataBuffer);
socket.emit('dataishere', dataBuffer);
} catch (e) {
console.log('data emitting error'+e)
}
});
serialPort.on('error', function(error) {
console.log(error);
});
socket.on('setspeed', function(data) {
console.log('haha' + data)
serialPort.write(data + '\n', function(err, results) {
if (err){console.log('err ' + err);};
})
});
socket.on('eventishere', function(data) {
console.log('haha' + data)
serialPort.write(data + '\n', function(err, results) {
if (err){console.log('err:'+err+'results:'+results);}
})
});
});
}
});