Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在nodejs中传递javascript消息_Javascript_Node.js_Arduino - Fatal编程技术网

在nodejs中传递javascript消息

在nodejs中传递javascript消息,javascript,node.js,arduino,Javascript,Node.js,Arduino,有人能帮我怎么做吗?每次收到消息时,我都希望将消息从websocket传递到串行端口 var firmata = require('firmata'); var board = new firmata.Board('COM4'); var WebSocketServer = require('ws').Server; var wss = new WebSocketServer({ port: 8081 }); wss.on('connection', function connection(

有人能帮我怎么做吗?每次收到消息时,我都希望将消息从websocket传递到串行端口

var firmata = require('firmata');
var board = new firmata.Board('COM4');

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8081 });

wss.on('connection', function connection(websocket) {
    websocket.on('message', function incoming(message) {
    board.servoWrite(5, message); //This doesn't work but how do i send?
}

board.on("ready", function() {
  board.servoWrite(5, 0); // this works but i need the message from above which I cant access.

});

您应该在板准备好后添加侦听器

board.on("ready", function() {
    wss.on('connection', function connection(websocket) {
        websocket.on('message', function incoming(message) {
            board.servoWrite(5, message);
        });
    });
});
或者声明一个变量,告诉你它是否准备好了

var isReady = false;

board.on("ready", function() {
    isReady = true;
});

wss.on('connection', function connection(websocket) {
    websocket.on('message', function incoming(message) {
        if(isReady)
            board.servoWrite(5, message);
    });
});

这不是sytax的错误。。不得不补充

board.servoConfig(5, 0, 30);
我在代码中添加了如下内容:

var firmata = require('firmata');
var board = new firmata.Board('COM4',function(){

});

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8081 });

wss.on('connection', function connection(websocket) {
    websocket.on('message', function incoming(message) {
    board.servoConfig(5, 0, 30);
    board.servoWrite(5, message); //This doesn't work but how do i send?
}

board.on("ready", function() {
  board.servoWrite(5, 0); // this works but i need the message from above which I cant access.

});

顺序无关紧要,我想:我解决了问题,但非常感谢:我不得不打电话给board.servoConfig5,0,0;这是一个强制性的电话,没有它就无法工作:谢谢:你在哪里添加了这个?这有什么用?我没有时间检查文档。我在示例中看到了它,并猜测它丢失了。从简短的阅读,它说,它设置的映射度[0,30]->[0180]引脚5