-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
44 lines (33 loc) · 860 Bytes
/
app.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
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/tty.usbserial-A601FDN8", {
baudrate: 9600
});
var io = require('socket.io').listen(8015, { log: false });
var socket, emitBuffer;
io.sockets.on('connection', function (conn) {
console.log('socket io connected');
socket = conn;
var i = 0
setInterval(function(){
console.log(emitBuffer);
socket.emit('ping', emitBuffer);
i++;
}, 750);
});
var readBuffer = "";
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) {
var z;
readBuffer += data.toString();
if(readBuffer.indexOf('\n') != -1){
//if(socket){
z = parseInt(readBuffer);
if(z && z !== null){
emitBuffer = {x:0,y:0,z:z};
}
//}
readBuffer = "";
}
});
});