3

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);
   });
 });

出力もエラーもありません。ゼロポートを返すだけです。OS によって認識される 2 つの 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 World\r\n");

Linux com ポートをノードのシリアルポートに公開するために何か特別なことをする必要がありますか?

4

2 に答える 2

0

同様の問題があります。ラズベリーパイのポートを一覧表示できません。同じコードが 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("ls\n", 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ます。これについて私ができることはありますか?

于 2015-01-10T07:18:11.063 に答える