开发者

NodeJS reading TTY Serial

开发者 https://www.devze.com 2023-02-27 01:18 出处:网络
I can\'t find a开发者_高级运维ny examples of simply reading a serial port on a machine using Node.JS and seems I\'m not the only one looking.

I can't find a开发者_高级运维ny examples of simply reading a serial port on a machine using Node.JS and seems I'm not the only one looking.

Quite recently it is an included library but I can't make head or tail of it!

http://nodejs.org/docs/v0.3.8/api/tty.html

Does anyone have an example of simply reading the serial port and just console.log the output?


Try to look at node-serialport module source.


@James, to configure the comport on windows try this:

var spawn = require('child_process').spawn 
  , command = 'MODE COM1:38400,N,8,1,P' 
  , cmd    = spawn('cmd', ['/s', '/c', command], { customFds: [0,1,2] }); 

cmd.on('exit', function(code){ 
    console.log(code);  
}); 


On Windows this works using the v0.5.2 node.exe download, no plug-ins. It reads COM1 at 9600 baud.

var fs = require('fs');
var inp = fs.createReadStream("\\\\.\\COM1");
inp.setEncoding('utf8');
var inptext = "";
inp.on('data', function (data) {
    inptext += data;
});


Have you seen the example on the most recent docs?

var tty = require('tty');
tty.setRawMode(true);
process.stdin.resume();
process.stdin.on('keypress', function(char, key) {
  if (key && key.ctrl && key.name == 'c') {
    console.log('graceful exit');
    process.exit()
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消