编辑——节点在 Windows 8.1 环境中运行。我正在使用串口模块和 socket.io 从 Arduino 读取数据。我在 2 - 5 分钟内获得精美的数据。然后节点停止运行,没有抛出异常,我一无所知。我试过只使用“http”模块和“express”。无论哪种方式,结果都相同。完整的源代码如下。
服务器.js
var express = require('express');
var app = express();
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});
var io = require('socket.io').listen(server);
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("COM3",{
baudrate:9600,
dataBits: 8,
parity: 'none',
flowControl: false,
parser: serialport.parsers.readline("<EOD>")
});
app.use(express.static(__dirname));
app.get('/', function (req, res) {
res.render('./index.html')
});
serialPort.on('open', function(){
// Now server is connected to Arduino
console.log('Serial Port Opened');
var lastValue;
io.sockets.on('connection', function (socket) {
//Connecting to client
console.log('Socket connected');
//socket.emit('connected');
var lastValue;
serialPort.on('data', function(data){
//if(lastValue !== data.toString()){
socket.emit('data', data.toString());
console.log(data.toString());
//}
lastValue = data.toString();
});
});
});
index.html 脚本:包含所有 socket.io 功能。
<script>
function handleIO(){
function serverDisconnect(){
console.log("Server Disconnected.");
console.log(new Date().toTimeString());
}
socket.timeout = 60*60*1000;
socket.on('disconnect', serverDisconnect);
socket.on('data', function(data) {
//console.log("Got Data");
console.log(data);
render(data);
});
console.log("Connected To Server.");
console.log(new Date().toTimeString());
}
var socket = io();
socket.on('connect', handleIO);
</script>
编辑#2:控制台日志:
C:\Users\Dave\Desktop\js\js\Websensors2>node serverexp.js
Example app listening at http://:::3000
Port open
New Socket Client connected: JPozMKfwH77x8uMNAAAA
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.80
Sent data to connected Socket clients
74.77
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.77
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.41
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.41
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
C:\Users\Dave\Desktop\js\js\Websensors2>