-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxbee.js
More file actions
39 lines (33 loc) · 953 Bytes
/
Copy pathxbee.js
File metadata and controls
39 lines (33 loc) · 953 Bytes
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
var connect = require('connect')
var io = require('socket.io').listen(8000)
var fs = require('fs')
var serialport_arg = process.argv[2]
var buad_arg = process.argv[3]
var app = connect().use(connect.static(__dirname))
connect.createServer(app).listen(8080);
console.log('port is listening to 8080 port')
io.sockets.on('connection', function (socket) {
console.log("connect to socket.io")
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort(serialport_arg, {
baudrate: buad_arg
})
serialPort.on("open", function() {
console.log("open");
var count = 0
var text = '';
serialPort.on("data", function(data) {
text = text + data
var index = text.lastIndexOf("\r\n");
if(index != -1) {
var chop = text.substring(0, text.lastIndexOf("\r\n"))
var re_chop = chop.replace(/'/g, '"');
count ++;
if(count > 2) {
socket.emit('message', re_chop);
}
text = ''
}
});
});
});