electron-vue制作桌面应用&引入node-serialport串口工具


想做一个类似上位机的软件,可以和设备通过串口进行通讯

首先想的是(逻辑性语言,精通的只有js O(∩_∩)O),js是否可以监听和写入电脑串口数据,百度发现,node.js有一款组件serialport,可以实现

但是由于是在node中,并且后期了解到,serialport是由c++编写,需要反编译到js

所以考虑:使用electron打包桌面应用,内嵌node环境,打包前反编译好serialport即可

 

1.新建项目,安装vue-cli

npm install -g vue-cli

2.electron提供了官方基于vue的项目 ,直接创建electron-vue项目

vue init simulatedgreg/electron-vue electron-vue-name

3.cd至项目目录下

4.安装node_modules

npm install

安装完成后,npm run dev,可以正常打开桌面应用即可

5.安装node-gyp

npm install -g node-gyp

6.安装python2.7&vc2015,可以直接下载软件安装,也可以使用命令行安装

npm install --global --production windows-build-tools
npm config set python python2.7 
npm config set msvs_version 2015

7.测试gyp

node-gyp

成功显示

PS E:\exe\electron-vue> node-gyp

  Usage: node-gyp <command> [options]

  where <command> is one of:
    - build - Invokes `msbuild` and builds the module
    - clean - Removes any generated build files and the "out" dir
    - configure - Generates MSVC project files for the current module
    - rebuild - Runs "clean", "configure" and "build" all at once
    - install - Install node development files for the specified node version.
    - list - Prints a listing of the currently installed node development files
    - remove - Removes the node development files for the specified version

node-gyp@8.1.0  C:\Users\zhupengfei\AppData\Roaming\npm\node_modules\node-gyp
node@16.4.1

8.安装serialport

npm install serialport

9.安装electron-rebuild 用于重新编译c++原生模块

npm install --save-dev electron-rebuild

10.对serialport进行重编译

.\node_modules\.bin\electron-rebuild.cmd 

编译出错可以手动编译,cd至./node_modules/@serialport/bindings ---------- 其实就是对bindings里的文件进行重编译,编译为bindings.node,此处,可能在编译时不报错,但是,在生成的应用内报错bindings.node不是标准的win32程序,不知道怎么解决

node-gyp rebuild --target=2.0.4 --arch=x64 --dist-url=https://npm.taobao.org/mirrors/atom-shell

重编译没有问题的话,基本就没问题了

 

注:

  1.误打误撞实现的成功编译并运行,此前一直都是编译不报错,运行在控制台就会报bindings.node不是标准的win32程序,使用手动编译又会报错缺少关键DLL文件

  2.编译成功后,就不用在编译了,就尽量不要在编译了,防止出现什么幺蛾子,除非又引入了什么需要重编译的依赖

  3.参考文档:“https://www.cnblogs.com/flypig666/p/12709550.html”,非常感谢!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM