Node.js与arduino串口通讯

  1. JS的发展可以说是很迅猛从ECMA5 到ECMA6.其系统是越来越庞大。从原生到各种JS框架林立,从单纯的前端,到复杂的后端系统。使得越来越多的前端程序员也可以上手后端开发。Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google Chrome V8引擎进行了封装,它主要用于创建快速的、可扩展的网络应用。Node.js采用事件驱动和非阻塞I/O模型,使其变得轻微和高效,非常适合构建运行在分布式设备的数据密集型实时应用。
    运行于浏览器的Javascript,浏览器就是Javascript代码的解析器,而Node.js则是服务器端JS的代码解析器,存于服务器端的JS代码由Node.js来解析和应用。
    本期采用node的串口实现与Arduino单片机的通讯:
    单片机:arduino uno

打开arduino LED 新建Serial文件,写入代码

本地图片,请重新上传图片

 int led=13;//测试灯

char getstr; 

void setup() 

{ 

  Serial.begin(9600); 

}

void loop() 

{ 

  getstr=Serial.read(); 

  Serial.println("a"); //给node.js发个a

  if(getstr=='a'){

    digitalWrite(led,HIGH); 

    Serial.println(getstr); 

  }

  else if(getstr=='b'){//收到b就关灯了

    digitalWrite(led,LOW); 

    Serial.print(getstr); 

  }

 delay(500);

while(1);

}
  1. OK 下载代码 单片机搞定。
    在电脑上安装npm后,在项目文件里面建立Serial.js文件,先执行安装serial 模块 :npm install serialport
    然后就可以写代码啦。
var SerialPort = require('serialport');
var port = new SerialPort('COM5',{ autoOpen: false,
    baudRate: 9600,//波特率
    dataBits: 8, //数据位
    parity: 'none', //奇偶校验
    stopBits: 1, // 停止位
    flowControl: false,// 流量控制 默认false
    parser: SerialPort.parsers.readline('\n')//去掉数据位后面的回车
})
port.open(function (err) {//打开串口
    if (err) {
        return console.log('Error opening port: ', err.message);
    }
    setTimeout(function() {
        port.write('a');//先发送个a   让单片机开灯
        setTimeout(function(){
            port.write('b');//再先发送个b  让单片机关灯
        },2000)   //这个延时是必须要有的
}, 2000);

});
var bufs = [];

port.on('data', function (buf) {//监听数据接收
    bufs.push(buf.toString("utf-8"));//串口过来的数据的16进制的,这里转成正常的 utf-8
    console.log("from arduino:-->"+buf.toString("utf-8"))
    console.log(bufs)
    if(buf.toString("utf-8")=='a\r'){ 
        console.log("get a")
    }
});
port.on('end', function (buf) {  //数据 接受完毕后的处理函数
    console.log(buf)
    var buf = Buffer.concat(bufs);
    console.log(buf)
});   

OK 运行该文件 node Serial.js后,就可以看到开关灯的效果。串口里也会打印双方接受的到的数据了。

本地图片,请重新上传

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值