Raspberry串口会在一段时间后自动停止

时间:2018-09-24 18:33:52

标签: javascript node.js console serial-port raspberry-pi3

我在树莓派3上使用neo 6m gps模块,我已将其连接到串行端口ttyAMA0上,但是它仅读取数据一小段时间,然后端口自动关闭,并且有时数据也损坏,从而导致gps.js模块引发错误。

Execution Demo screenshot

下面是我的节点js脚本:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);


var file = '/dev/ttyAMA0';

const SerialPort = require('serialport');
const parsers = SerialPort.parsers;

const parser = new parsers.Readline({
  delimiter: '\r\n'
});

const port = new SerialPort(file, {
  baudRate: 9600
});

port.pipe(parser);


var GPS = require('../../gps.js');
var gps = new GPS;

gps.on('GGA', function(data) {
  io.emit('position', data);
  console.log("Latitiude :", data.lat);
  console.log("Longitude :", data.lon);
});

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

http.listen(3000, function() {

  console.log('listening on *:3000');
});

port.on('data', function(data) {    
  gps.updatePartial(data);
});

process.on('unhandledRejection', function (reason, p) {
  //I just caught an unhandled promise rejection, since we already have fallback handler for unhandled errors (see below), let throw and let him handle that
  console.log("=============");
  console.log(reason);
  console.log("=============");
  return;
});

process.on('rejectionHandled', () => {});

process.on('uncaughtException', function (error) {
    console.log(error);
});

port.on('close', function(data) {

  console.log("Port closed");
  console.log(port.binding);

    });

Output screenshot

1 个答案:

答案 0 :(得分:0)

我通过重新安装raspbian解决了该问题,操作系统由于某种原因关闭了端口