有人能帮我怎么做吗?每次收到消息时,我都想将消息从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.
});发布于 2015-05-05 16:16:25
这不是一个系统错误。不得不加上
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.
});发布于 2015-05-05 14:15:29
您应该在board准备好之后添加侦听器。
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);
});
});https://stackoverflow.com/questions/30054872
复制相似问题