gpt4 book ai didi

Node.js 与 arduino 的 TCP 套接字

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:06 28 4
gpt4 key购买 nike

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

我用新软件下载了我的arduino,或者当我拔掉以太网线时当我在arduino上打开串行监视器两次时。我得到 events.js:85扔呃;//未处理的“错误”事件错误:读取 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', ...) 等。也许还有一些其他错误处理程序。

关于Node.js 与 arduino 的 TCP 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109479/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com