Posted on

electron使用serialport

准备
node-gyp:https://github.com/nodejs/node-gyp

npm install --save-dev electron-rebuild serialport electron-prebuilt

编辑package.json

"scripts": {
  "rebuild" :"electron-rebuild -f -w serialport",
  "start":"electron ."
},
"devDependencies": {
 "electron-prebuilt": "^1.3.8",
 "electron-rebuild": "^1.3.0",
 "serialport": "^4.0.3"
 }

编译

npm run rebuild

编辑main.js,运行npm start

var s = require("serialport")
s.list(function(err,ports){
 console.log(ports)
});
Posted on

树莓派为Nodejs程序设置脚本开机启动

  • 在/etc/init.d/下创建服务文件xxx
#!/bin/sh

### BEGIN INIT INFO
# Provides:          xxx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start xxx
# Description:       start xxx
### END INIT INFO
export NODE_PATH=/home/pi/xxx/node_modules 
case $1 in
        start)
               pm2 /home/pi/xxx/server.js start
                ;;
        stop)
               pm2 /home/pi/xxx/server.js stop
				;;
        *)
                echo "Usage: $0 (start|stop)"
                ;;
esac
exit 0
  • 注意export NODE_PATH=指向node引用库环境。
sudo chmod +x /etc/init.d/xxx
  • 这样启动改脚本用service 命令就可以
sudo systemctl daemon-reload
sudo systemctl enable xxx
sudo service xxx start #启动
sudo service xxx stop #停止
  • 设置开机启动
sudo update-rc.d xxx defaults