New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在electron中使用node-serialport的正确姿势 #2
Comments
使用serialport以上工作完成后,就可以在electron使用serial port了。 这里直接贴package.json。
接着,随便写个main线程。里面创建一个BrowserWindow。 const { BrowserWindow, app } = require('electron');
const path = require('path');
let win;
function createWindow() {
win = new BrowserWindow();
win.loadFile(path.join(__dirname, '../renderer/index.html'));
win.on('closed', () => {
win = null;
});
}
app.on('ready', createWindow); 在随便写个HTML文件 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>node-serialport</title>
<script>
const list = require('serialport').list;
list().then(ports => {
ports.forEach(element => {
document.write(`<div>${element.comName}</div>`);
});
});
</script>
</head>
<body>
</body>
</html> 执行 |
我明天试试 O(∩_∩)O |
Thanks! |
found 3 high severity vulnerabilities |
大概就是依赖里面有老版本的依赖吧, 提示fix. 好像执行后, 还会有同样的提示.
|
An unhandled error occurred inside electron-rebuild Error: Unable to find parent node_modules directory, specify it via --module-dir, E.g. "--module-dir ." for the current directory 请问当我写了.\node_modules.bin\electron-rebuild.cmd 他出上面的error如何解决 |
|
执行.\node_modules.bin\electron-rebuild.cmd重新编译一下,serialport才能在electron里面使用。 我照上面的指示做 |
@lky94 对, 估计编译不成功. 确定网络没问题. 翻墙后一遍都不用编译的 |
.\node_modules.bin\electron-rebuild.cmd |
@lky94 只能在cmd里面执行而已吧. 输入 .\node_modules.bin\electron-rebuild.cmd 回车就行 |
请问你一开始说的node-gyp 如何使用我打进去有error |
@lky94 你还是看这篇文档吧, 我这方法可能不适用新版本. https://electronjs.org/docs/tutorial/using-native-node-modules |
node-gyp就是为node编译c++扩展的时候使用的编译工具,那么为什么要安装python2.7呢? |
开发环境准备
node-serialport是一个让node可以访问电脑串口设备的原生模块,在electron环境下使用并不是简单的
npm install serialport
就能解决的事情。对于刚开始接触node的人来说,不折腾一番基本上是不可能的。如果不明就里的一次性安装成功,那下一次安装多半也需要折腾的。折腾过程中,免不了在Google,百度,GitHub上掘地三尺找解决方案。
本文尽可能的详细介绍如何在electron中编译并使用seialport。这里使用的系统环境是win10,需要准备一下几个材料:
node-gyp
python2.7
只能python2,不兼容python3如果你的机器上没有安装过VS的话,并且后面不会使用VS的话,建议安装Visual Studio Build Tools即可。
开始编译
在使用 Node 原生模块一文里介绍好几种使用原生模块的方法。这里介绍前面两种。
通过npm安装
通过设置一些环境变量来实现npm的安装。为了方便,可以选择写个.bat文件来导入这些参数。
这里需要注意前两行的路径。
第一行是Visual Studio Build Tools安装后得到的一些批处理,主要是导入一些变量到cmd。
第二行是python的路径,如果电脑有多个python的话,建议通过set的方式导入到本次命令行。
剩下的都是electron相关的参数。target版本号可以在首页里面找。
到这里,可以尝试执行
npm install serialport
来安装serialport了。如果安装过程不会报错,则基本上OK了。如果报错了,看一下报错信息。
如果是有404或者GET请求失败,则是网络问题。
另外,如果先前npm报错过,建议删除node_modules文件夹重新
npm install
。使用electron-rebuild
使用rebuild(
npm install electron-rebuild --save-dev
)来构建serialport不需要设置太多变量,但是下面两行少不了的。接着执行
npm install
安装各种依赖。安装完后,执行.\node_modules\.bin\electron-rebuild.cmd
重新编译一下,serialport才能在electron里面使用。编译过程中可以会出错,比如使用了python3或者msbuild版本错误等一堆乱七八糟的错误。具体看错误日志定位问题才能解决。
如果出现下面的提示
可以到全局的
\node_modules\node-gyp\gyp\pylib\gyp\MSVSVersion.py
下修改工具集的版本。如果是 VS2017 版本号可能是 141
具体使用的看下面
The text was updated successfully, but these errors were encountered: