Node.js 使用 TCP 套接字连接到 arduino

Node.js with TCP socket to arduino

我有一个项目是 node.js 服务器与 arduino 真正的 TCP 套接字对话。 它从网页 true a socket.io 接收他的所有数据,然后将其传输到 TCP 套接字。一切正常。除非:

我用新软件下载了我的arduino, 或者当我拔下以太网电缆时, 当我在 arduino 上打开我的串行监视器两次时。 我得到了 events.js:85 扔呃; // 未处理的 'error' 事件 错误:读取 ECONNRESET 在 exports._errnoException (util.js:746:11) 在 TCP.onread (net.js:550:26)

有谁知道问题出在哪里。我该如何修复它,尤其是自动连接功能。

var express = require('express');
var app     = express();
var hbs     = require('hbs');
var port    = 3000;
var http = require('http').Server(app);
var io = require('socket.io')(http);
net = require('net');
var tcpGuests = [];

app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.bodyParser());
app.use(express.static('public'));


app.get('/', function(req, res) {
    res.render('index', {title:"HTML5-SuperTemplate"});
});

app.get('/about', function(req, res) {
    res.render('about', {title:"About Me"});
});

app.get('/instructions', function(req, res) {
    res.render('instructions', {title:"Instructions"});
});

app.get('/buttons', function(req, res) {
    res.render('buttons', {title:"Buttons"});
});

app.get('/Lists', function(req, res) {
    res.render('Lists', {title:"Lists"});
});

app.get('/menus', function(req, res) {
    res.render('menus', {title:"Menus"});
});

app.get('/tables', function(req, res) {
    res.render('tables', {title:"Tables"});
});

app.get('/tooltips', function(req, res) {
    res.render('tooltips', {title:"Tooltips"});
});

app.get('/typography', function(req, res) {
    res.render('typography', {title:"Typography"});
});

app.get('/hr', function(req, res) {
    res.render('hr', {title:"Horizontal Rules"});
});

app.get('/icons', function(req, res) {
    res.render('icons', {title:"ICONS"});
});

app.get('/code', function(req, res) {
    res.render('code', {title:"CODE & PRE"});
});

app.get('/tabs', function(req, res) {
    res.render('tabs', {title:"TABS"});
});

app.get('/breadcrumbs', function(req, res) {
    res.render('breadcrumbs', {title:"Breadcrumbs"});
});

app.get('/grids', function(req, res) {
    res.render('grids', {title:"Grid System"});
});

app.get('/images', function(req, res) {
    res.render('images', {title:"IMAGES"});
});

app.get('/slideshow', function(req, res) {
    res.render('slideshow', {title:"Slideshow"});
});

app.get('/forms', function(req, res) {
    res.render('forms', {title:"Forms"});
});

app.get('/classes', function(req, res) {
    res.render('classes', {title:"Classes"});
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log('received on io-socket:'+msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

// socket.io, I choose you
io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log('received on io-socket:'+msg);
    for (g in tcpGuests) {
        tcpGuests[g].write(msg);
    }
  });
});

//tcp socket server
var tcpServer = net.createServer(function (socket) {
  console.log('tcp server running on port 1337');
  console.log('web server running on http://localhost:3000');
});

tcpServer.on('connection',function(socket){
    socket.write('connected to the tcp server\r\n');
    console.log('num of connections on port 1337: ' + tcpServer.connections);

    tcpGuests.push(socket);


    socket.on('data',function(data){
        console.log('received on tcp socket:'+data);
        socket.write(' msg received\r\n');


        //send data to guest socket.io chat server
        for (g in io.clients) {
            var client = io.clients[g];
            client.send({message:["arduino",data.toString('ascii',0,data.length)]});
          }
    })
});
tcpServer.listen(1337);

你没有做错误处理。每次出现连接错误(意外拔出电缆)时,node.js 都会抛出您收到的错误消息。您可以捕获这些错误,这样应用程序就不会退出。

添加一些错误处理程序。 tcpServer.on('error', ...)socket.on('error', ...)io.on('error', ...) 等。也许还有一些其他错误处理程序。