Node.js和串行端口无法列出任何端口



我正在尝试使用node.js在ubuntu上打开一个串行端口。

我似乎无法打开任何端口,也无法列出任何端口。

这是我的列表代码:

var serialport = require("serialport"),
serialport.list(function (err, ports) {
   console.log("thisis the list callback");
   ports.forEach(function(port) {
       console.log(port.comName);
       console.log(port.pnpId);
       console.log(port.manufacturer);
   });
 });

我没有得到任何输出,也没有错误。 它只返回零个端口。 我有两个操作系统认可的 com 端口:

rd@mediaplayer:~/cotto$ dmesg | grep tty
[    0.000000] console [tty0] enabled
[    0.732717] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[    0.804533] serial8250: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A
[    1.097341] 00:0a: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[    1.168637] 00:0b: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A

如果我尝试显式打开 com 端口,则在使用它时会出现"未打开"错误。 我认为这是因为节点串行端口没有"看到"我的任何 com 端口:

rd@mediaplayer:~/cotto$ sudo node sptest.js
opening serial port: /dev/ttyS0
events.js:72
       throw er; // Unhandled 'error' event
              ^
 Error: Serialport not open.
    at SerialPortFactory.SerialPort.write (/home/rd/node_modules/serialport/serialport.js:246:17)
    at Object.<anonymous> (/home/rd/cotto/sptest.js:33:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

打开串口的代码在这里供参考:

var serialport = require("serialport"),
SerialPort = serialport.SerialPort;
var portName = "/dev/ttyS0";
console.log("opening serial port: " + portName);
var myPort = new SerialPort(portName, { baudrate: 9600,
    });

myPort.write("Hello Worldrn");

我需要做什么特别的事情来将 linux com 端口暴露给节点串行端口吗?

我也有类似的问题。我无法列出树莓派上的端口。相同的代码在Windows上运行良好。另一方面,我可以打开端口并读取和写入它,没有任何问题。

你可以试试:

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyS0", {
  baudrate: 57600
}, false); // this is the openImmediately flag [default is true]
serialPort.open(function (error) {
  if ( error ) {
    console.log('failed to open: '+error);
  } else {
    console.log('open');
    serialPort.on('data', function(data) {
      console.log('data received: ' + data);
    });
    serialPort.write("lsn", function(err, results) {
      console.log('err ' + err);
      console.log('results ' + results);
    });
  }
});

以查看打开时是否出现任何错误。

编辑:目前,我用这样的东西来列出端口,这有点笨拙,但无论如何......

var exec = require('child_process').exec;
exec('dmesg | grep tty', function (error, stdout, stderr) {
  stdout.split("n").forEach(function(line){
    var words=line.split(" ");
    if(words.length>=6 && words[6].substring(0,3)=="tty"){
        console.log(words[6])
    }
  });
});

编辑2:

我的设备/dev/ttyAMA0/lib/udev/rules.d/60-persistent-serial.rules我看到这一行:

KERNEL!="ttyUSB[0-9]*|ttyACM[0-9]*", GOTO="persistent_serial_end"

我怀疑这是它没有列在/dev/serial/by-id下的原因。有什么想法吗?

对于较新版本的节点串行端口,写入将排队直到端口打开,但是对于版本 2 和版本 3,您需要等待端口打开才能写入它。旧版本的串行端口文档讨论在编写之前等待打开事件。

对于版本 3 https://github.com/EmergingTechnologyAdvisors/node-serialport/blob/3.1.2/README.md#opening-a-port

我知道这个问题有点老了,但我想为遇到这个问题的其他人留下答案。我强烈建议您至少使用串行端口版本 3,因为它通过少量的 api 更改修复了 v2 的主要错误,但是 4 和 5 更快,使用更少的内存并支持背压并修复了更多错误。

相关内容

最新更新