简体   繁体   English

Arduino串行和套接字

[英]Arduino Serial and Socket

I m trying to Send serial data to Arduino using Node.js and Socket.io and my code. 我试图使用Node.js和Socket.io和我的代码将串行数据发送到Arduino。

and the html page have only one button. 而html页面只有一个按钮。 its work node and html side .but this is not send serial data. 它的工作节点和html端。但这不是发送串行数据。

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
 var port = process.env.PORT || 3000;


 server.listen(port, function () {
//  console.log('Server listening at port %d', port);
});

 // Routing

 app.use(express.static(__dirname + '/public'));

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyACM3", {
baudrate:9600
}, false); // this is the openImmediately flag [default is true]



io.on('connection', function (socket) {

    socket.on('my other event', function (data) {
        console.log(data);

        serialPort.open(function () {
            console.log('open');
            serialPort.on('data', function (data) {
                console.log('data received: ' + data);
            });

        serialPort.write(data, function (err, results) {
            console.log('err ' + err);
            console.log('results ' + results);
        });
    });

  });
  });

 app.get('/', function (req, res) {
 res.sendfile(__dirname + '/index.html');
});

Sending serial messages to the Arduino is not as easy as simply passing in a String. 向Arduino发送串行消息并不像简单地传递一个String那样容易。 Unfortunately you have to send the String character by character which the Arduino will receive and concatenate back to a String. 不幸的是,您必须按字符发送String字符,Arduino会接收该字符并将其连接回String。 After you sent the last character you need to send one final new line character (/n) which is a signal for the Arduino to stop concatenating and evaluate the message. 发送完最后一个字符后,您需要发送一个最后的换行符(/ n),这是Arduino停止串联并评估消息的信号。

This is what you need to do in your Node.js server: 这是您在Node.js服务器中需要执行的操作:

// Socket.IO message from the browser
socket.on('serialEvent', function (data) {

    // The message received as a String
    console.log(data);

    // Sending String character by character
    for(var i=0; i<data.length; i++){
        myPort.write(new Buffer(data[i], 'ascii'), function(err, results) {
            // console.log('Error: ' + err);
            // console.log('Results ' + results);
        });
    }

    // Sending the terminate character
    myPort.write(new Buffer('\n', 'ascii'), function(err, results) {
        // console.log('err ' + err);
        // console.log('results ' + results);
    });
});

And this is the Arduino code that receives this: 这是接收以下代码的Arduino代码:

String inData = "";

void loop(){
    while (Serial.available() > 0) {
        char received = Serial.read();
        inData.concat(received);

        // Process message when new line character is received
        if (received == '\n') {
            // Message is ready in inDate
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM