||
采用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.
安装过程出现如下错误:
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
输入这条语句,安装成功!
基本使用方法:
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); }); } });
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-9 10:57
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社