chaoshu的个人博客分享 http://blog.sciencenet.cn/u/chaoshu

博文

node-serialport —— Node.js 串口数据读写包

已有 4610 次阅读 2020-7-23 11:27 |个人分类:GNSS|系统分类:科研笔记

采用Node.js作为阿里云物联网SDK,将树莓派上云。

node-serialport 是一个 Node.js 的包,用来对串口数据进行读写操作。

利用树莓派将mosaic-X5定位结果通过串口方式上报阿里云物联网服务器;

node-serialport安装方法:https://serialport.io/docs/guide-installation

对于大多数“标准”系统(在Mac,Linux或Windows上,在x64处理器上受支持的Node.js),Node SerialPort可以通过以下方式轻松安装:

npm install serialport
要在Raspbian上启用串行端口,请启动 sudo raspi-config,然后选择Interfacing Options,然后选择Serial。
然后将询问两个问题:
1、Would you like a login shell to be accessible over serial?
2、Would you like the serial port hardware to be enabled?
You must answer No to question 1 and Yes to question 2. 
If the login shell is left active, you will experience hangs and or disconnects.

图片1.png

图片2.png

安装过程出现如下错误:

Npm安装报错 RollbackFailedOptional Verb Npm-Session 解决办法

该问题一般情况是因为npm源问题

更换为淘宝的源

npm config set registry http://registry.npm.taobao.org

使用npm info express验证

若安装npm时:sill fetchPackageMetaData error

1、关闭命令终端

2、重新打开一个终端

3、输入 npm cache clean --force    //清理缓存

4、重新安装

pi@raspberrypi:~ $ npm install serialport
> @serialport/bindings@9.0.0 install /home/pi/node_modules/@serialport/bindings
> prebuild-install --tag-prefix @serialport/bindings@ || node-gyp rebuild
prebuild-install WARN install No prebuilt binaries found
 (target=12.16.1 runtime=node arch=arm libc= platform=linux)
gyp WARN install got an error, rolling back install
gyp ERR! configure error 
gyp ERR! stack Error: connect ETIMEDOUT 104.20.22.46:443
gyp ERR! stack     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)
gyp ERR! System Linux 4.19.118-v7l+
gyp ERR! command "/usr/local/bin/node" "/usr/local/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/pi/node_modules/@serialport/bindings
gyp ERR! node -v v12.16.1
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok 
npm WARN enoent ENOENT: no such file or directory, open '/home/pi/package.json'
npm WARN pi No description
npm WARN pi No repository field.
npm WARN pi No README data
npm WARN pi No license field.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @serialport/bindings@9.0.0 install: `prebuild-install --tag-prefix @serialport/bindings@ || node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @serialport/bindings@9.0.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/pi/.npm/_logs/2020-07-23T03_15_50_082Z-debug.log

查找原因,找到如下:https://blog.csdn.net/i7thTool/article/details/78021589

sudo npm install serialport --unsafe-perm --build-form-source

输入这条语句,安装成功!

22.png



基本使用方法:

const SerialPort = require('serialport')
const port = new SerialPort('/dev/ttyACM0', {
  baudRate: 115200, //波特率
  dataBits: 8, //数据位
  parity: 'none', //奇偶校验
  stopBits: 1, //停止位
  flowControl: false 
}, 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);
    });
  }
});




https://m.sciencenet.cn/blog-858128-1243244.html

上一篇:基于raspberry PI和mosaic-X5实现rtk定位
下一篇:Node.js上传阿里云

0

该博文允许注册用户评论 请点击登录 评论 (5 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-2 20:07

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部