Node.js 串口通讯 node-serialport 使用说明

官网:https://serialport.io/en/

安装:npm install serialport

Parsers说明:
parser-byte-length:
大概意思是定义了一个长度为length字节的buffer,串口收到数据后先放到buffer中,放满了才发送给程序,超出的部分等buffer发送清空后继续放入
parser-cctalk:
一种在货币交易等行业广泛使用的串行协议
parser-delimiter:
意思是遇到某个字符时才把buffer中的数据发给程序,比如设为’a’,那么用字符发送时遇到’a’即发送,用hex发送时遇到61(‘a’的ASCII码)时发送;注:官方例子中’\n’字符用串口工具字符发送没生效,但用hex发送其ASCII码(0A)和在node.js项目中可以生效
parser-readline:
可以自定义换行符,遇到换行符时发送,默认为’\r\n’,对应ASCII为0D 0A;但目前打hex日志时为乱码
parser-ready:
程序先收到自定义字符串,例’READY’后才开始接收数据
parser-regex:
正则表达式
parser-slip-encoder:
没弄清楚是什么意思,require模块也报错

例子:

var SerialPort = require('serialport');
var port = new SerialPort('COM5');

//发hex
var senddata = [0x01,0x02];
//发字符串
//senddata = 'test data';

function writeport()
{
    port.write(senddata, function (err) {
        if (err) {
            return console.log('Error on write: ', err.message);
        }
        console.log('send: ' + senddata);
    });
}

port.on('open', function () {
    writeport();
});

// open errors will be emitted as an error event
port.on('error', function (err) {
    console.log('Error: ', err.message);
})

setInterval(function () {
    writeport();
}, 5000);


port.on('data', function (data) {
    //收hex
    console.log('recv: ' + data.toString('hex'));
    //收字符串
    //console.log('recv: ' + data.toString('ascii'));
  });

注:版本不同可能使用方式不同,当前使用的版本是7.1.4

简单例子地址:https://github.com/TLScottChen/node-serialport-example
官方文档4.0.1地址:https://github.com/node-serialport/node-serialport/blob/4.0.1/README.md

 

转载于:https://www.cnblogs.com/TLScott/p/10358534.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
node-serialport 是一个 Node.js 的包,用来对串口数据进行读写操作。基本示例代码:var SerialPort = require("serialport").SerialPort var serialPort = new SerialPort("/dev/tty-usbserial1", {   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 serialPort = require("serialport"); serialPort.list(function (err, ports) {   ports.forEach(function(port) {     console.log(port.comName);     console.log(port.pnpId);     console.log(port.manufacturer);   }); });串口配置:baudRatedataBitsstopBitsparityrtsctsxonxoffxanyflowControlbufferSizeparserencodingdataCallbackdisconnectedCallbackplatformOptions - sets platform specific options, see below.目前已有很多项目在使用这个包进行串口处理:Johnny-Five - Firmata based Arduino Framework.Cylon.js - JavaScript Robotics, By Your Command.node-l8smartlight (source) A node library to control the L8 Smartlight via Bluetooth or USB portfirmata Talk natively to Arduino using the firmata protocol.tmpad source - a DIY midi pad using infrared, arduino, and nodejs. Videoduino - A higher level framework for working with Arduinos in node.js.Arduino Drinking Game Extravaganza - AKA "The Russian" a hexidecimal drinking game for geeks by Uxebu presented at JSConf EU 2011.Arduino controlling popcorn.js - Controlling a popcorn.js video with an Arduino kit.Robotic JavaScript - The first live presentation of the node-serialport code set as presented at JSConf EU 2010.devicestack - This module helps you to represent a device and its protocol.reflecta A communication protocol that combines Arduino Libraries and NodeJS into an integrated system.rc4pt-node - Control Popcorntime with an
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值