1

我第一次使用 node.js 和一个简单的网站来控制电机。

网站上有 2 个按钮:“左”和“右”。您必须按住“左”或“右”按钮来转动电机。当您松开按钮时,电机停止。电机连接到服务器串行端口上的 Arduino。它有效,但是

问题

当我在手机上使用它并向按钮发送垃圾邮件时,服务器有时会停止响应所有人,直到我重新启动服务器。我认为由于 WiFi 延迟,这些函数被调用得太快了。所以我尝试在 Chrome 控制台中调用这些函数。

LeftPressed();LeftReleased();LeftPressed();LeftReleased();

事实上,在运行它们几次后,服务器停止响应。没有错误,没有崩溃..

当我删除时motorPort.write(message);,服务器会保持响应。

如何防止服务器停止响应?

客户端(浏览器中的 Javascript)

function LeftPressed() {      
    socket.emit('steer', { left: true });
}
function LeftReleased() {       
    socket.emit('steer', { left: false });
}
function RightPressed() {        
    socket.emit('steer', { right: true });
}
function RightReleased() {        
    socket.emit('steer', { right: false });
}

服务器(node.js)

const SerialPort = require("serialport");
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server)    
const motorPort = new SerialPort('/dev/arduinoUnoMicroUSB', {
    baudrate: 9600,
});

app.use(express.static('web'));
server.listen(8081, function () {
    console.log("Server running...");
});

io.on('connection', function (socket) {  
    socket.on('steer', function (data) {
        var message = "2"; //stop   

        if (data.left != undefined) {
            console.log("Left: " + data.left); //log to Console (below)
            if (data.left) {
                message = "1" //turn left
            }
        }    

        if (data.right != undefined) {
            console.log("Right: " + data.right);
            if (data.right) {
                message = "3"; //turn right
            }
        }    

        motorPort.write(message); //send message over serial port            
    });   
});

控制台输出示例:单击“左”按钮

Server running...
Left: true        //pressed   (motor turning left)
Left: false       //released  (motor stops)
4

1 回答 1

0

我得到了它的工作。它不是 100% 完美的,但至少服务器一直在响应。如果您认为有更好的方法,我总是愿意改进。

const SerialPort = require("serialport");
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const motorPort = new SerialPort('/dev/arduinoUnoMicroUSB', {
    baudrate: 9600,
});

app.use(express.static('web'));
server.listen(8081, function () {
    console.log("Server running...");
});
var ready = true;
var NotSendMessages = [];
io.on('connection', function (socket) {
    socket.on('steer', function (data) {
        var message = "2"; //stop   
        if (data.left != undefined) {
            console.log("Left: " + data.left);
            if (data.left) {
                message = "1" //turn left
            }
        }

        if (data.right != undefined) {
            console.log("Right: " + data.right);
            if (data.right) {
                message = "3"; //turn right
            }
        }
        if (!ready) {
            console.log("cats!!!");
            NotSendMessages.push(message);
        } else {
            send(message);
        }
    });
});
function send(message) {
    motorPort.write(message, function () {
        ready = false;
        motorPort.drain(function () {
            motorPort.flush(function () {
                ready = true;
            });
        });
    });
}
setInterval(function () {
    if (NotSendMessages.length > 0 && ready) {
        send(NotSendMessages.shift());
    }
}, 50);
于 2017-06-18T11:23:26.097 回答