-
Notifications
You must be signed in to change notification settings - Fork 1
/
osx_index.js
154 lines (137 loc) · 4.51 KB
/
osx_index.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//var WebSocket = require('ws');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var port = process.env.PORT || 3000;
var io = require('socket.io').listen(server);
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
// Routing
app.use(express.static(__dirname + '/public'));
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("/dev/cu.usbserial-cc3101B", {
//var serialPort = new SerialPort("COM4", {
baudrate: 115200,
// baudrate: 9600,
parser: serialport.parsers.readline("\n")
}, false);
io.sockets.on("connection",openSocket);
function openSocket(socket) {
serialPort.open(function(error) {
if ( error ) {
console.log("no serial port");
} else {
socket.on("d",function(data,flag) {
console.log("web client requested one scan");
serialPort.write("d\n", function(err,res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
});
});
socket.on("s",function(data,flag) {
console.log("web client requested continuous scan");
serialPort.write("s\n", function(err,res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
});
});
socket.on("t",function(data,flag) {
console.log("web client requested stop continuous scan");
serialPort.write("t\n", function(err,res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
});
});
socket.on("o",function(data,flag) {
console.log("asking for graph of row " + data);
serialPort.write("o," + data + "\n", function(err,res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
});
});
socket.on("l",function(data,flag) {
console.log("telling client to light up row " + data);
serialPort.write("l," + data + "\n", function(err,res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
})
});
serialPort.on("data", function(data) {
console.log("data from board");
console.log(data);
socket.emit("info",data);
});
}
});
};
/*
io.sockets.on("connection",openSocket);
function openSocket(socket) {
serialPort.open(function(error) {
if ( error ) {
console.log("no serial port");
} else {
socket.on("d",function(data,flag) {
console.log("web client requested data");
serialPort.write("start\n", function(err,res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
});
});
socket.on("s",function(data,flag) {
console.log("web client requested continuous scan");
serialPort.write("s\n", function(err,res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
});
socket.on("t",function(data,flag) {
console.log("web client requests stop continuous scan");
serialPort.write("t\n", function(err, res) {
if (err !== undefined) {
console.log("error on writing to serial " + err);
}
if (res !== undefined) {
console.log("successfully wrote " + res + " characters");
}
})
});
serialPort.on("data", function(data) {
console.log("data from board");
console.log(data);
socket.emit("info",data);
});
});
}
});
};
*/