electron和PCSC

编程入门 行业动态 更新时间:2024-10-09 02:29:27

<a href=https://www.elefans.com/category/jswz/34/1771365.html style=electron和PCSC"/>

electron和PCSC

创建一个electron项目:

  • 创建空文件夹,然后打开命令行
npm init

文件目录中会出来一个package.json文件:

{"name": "pcscdemo","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "","license": "ISC"
}

把这个 Node 应用转换成一个 Electron 应用也是非常简单的,我们只不过是把 node 运行时替换成了 electron 运行时。
package.json中不要有注释,这个是方便理解的,在运行时要删除

{"name": "pcscdemo","version": "1.0.0","description": "","main": "main.js",    //这是electron的主进程"scripts": {"start": "electron ."},"author": "","license": "ISC"
}
  • 安装 Electron
    需要安装electron。 我们推荐的安装方法是把它作为您 app 中的开发依赖项,这使您可以在不同的 app 中使用不同的 Electron 版本。 在您的app所在文件夹中运行下面的命令:
 npm install --save-dev electron

出现下图即为安装成功:

  • 开发一个简单的electron项目
    Electron apps 使用JavaScript开发,其工作原理和方法与Node.js 开发相同。 electron模块包含了Electron提供的所有API和功能,引入方法和普通Node.js模块一样:
const electron = require('electron')

完整的main.js文件如下:

const { app, BrowserWindow } = require('electron')// 保持对window对象的全局引用,如果不这么做的话,当JavaScript对象被
// 垃圾回收的时候,window对象将会自动的关闭
let winfunction createWindow () {// 创建浏览器窗口。win = new BrowserWindow({ width: 800, height: 600 })// 然后加载应用的 index.html。win.loadFile('index.html')// 打开开发者工具win.webContents.openDevTools()// 当 window 被关闭,这个事件会被触发。win.on('closed', () => {// 取消引用 window 对象,如果你的应用支持多窗口的话,// 通常会把多个 window 对象存放在一个数组里面,// 与此同时,你应该删除相应的元素。win = null})
}// Electron 会在初始化后并准备
// 创建浏览器窗口时,调用这个函数。
// 部分 API 在 ready 事件触发后才能使用。
app.on('ready', createWindow)// 当全部窗口关闭时退出。
app.on('window-all-closed', () => {// 在 macOS 上,除非用户用 Cmd + Q 确定地退出,// 否则绝大部分应用及其菜单栏会保持激活。if (process.platform !== 'darwin') {app.quit()}
})app.on('activate', () => {// 在macOS上,当单击dock图标并且没有其他窗口打开时,// 通常在应用程序中重新创建一个窗口。if (win === null) {createWindow()}
})// 在这个文件中,你可以续写应用剩下主进程代码。
// 也可以拆分成几个文件,然后用 require 导入。

最后编辑想显示的index.html:

<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title>Hello World!</title></head><body><h1>Hello World!</h1>We are using node <script>document.write(process.versions.node)</script>,Chrome <script>document.write(process.versions.chrome)</script>,and Electron <script>document.write(process.versions.electron)</script>.</body>
</html>

在此,就可以运行:

electron . 

安装PCSC-lite

npm i pcsclite

会报错

$ npm i pcsclite> buffertools@2.1.6 install E:\aPritice\dd\electron\electronandpc\node_modules\buffertools
> node-gyp rebuildE:\aPritice\dd\electron\electronandpc\node_modules\buffertools>if not defined npm_config_node_gyp (node "C:\Users\d\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Users\d\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
????????????????????????????????ò??????????????/m???????
MSBUILD : error MSB3428: δ????? Visual C++ ?????VCBuild.exe??????????????1) ??? .NET Framework 2.0 SDK??2) ??? Microsoft Visual Studio 2005???? 3) ?????????????????????λ???????λ????????·???С? [E:\aPritice\dd\electron\electronandpc\node_modules\buffertools\build\binding.sln]
gyp ERR! build error
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Users\zoe\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\d\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd E:\aPritice\dd\electron\electronandpc\node_modules\buffertools
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN pcscdemo@1.0.0 No description
npm WARN pcscdemo@1.0.0 No repository field.npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! buffertools@2.1.6 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the buffertools@2.1.6 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!     C:\Users\d\AppData\Roaming\npm-cache\_logs\2019-02-18T11_04_44_471Z-debug.log

对于这个问题,查询了很多文档,然后跟着运行了下,
先下载c++的库,.01.25.zip,
下载后,解压安装;
这个安装后,netframework 4.0 也安装下,这个可以直接在Windows设置内打开这个功能
下载netframework 地址:.aspx?id=17718;

以管理员身份运行cmd:

npm install --global --production windows-build-tools

等待完成,很漫长


虽然上述的命令完成了,但是我的本地还是不能正常npm i pcsclite;


E:\GD>npm i pcsclite> buffertools@2.1.6 install E:\GD\node_modules\buffertools
> node-gyp rebuildE:\GD\node_modules\buffertools>if not defined npm_config_node_gyp (node "C:\Users\zoe\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Users\zoe\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装 .NET Framework 2.0 SDK;2) 安装 Microsoft Visual Stu
dio 2005;或 3) 如果将该组件安装到了其他位置,请将其位置添加到系统路径中。 [E:\GD\node_modules\buffertools\build\binding.sln]
gyp ERR! build error
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Users\zoe\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\zoe\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd E:\GD\node_modules\buffertools
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN enoent ENOENT: no such file or directory, open 'E:\GD\package.json'
npm WARN GD No description
npm WARN GD No repository field.
npm WARN GD No README data
npm WARN GD No license field.npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! buffertools@2.1.6 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the buffertools@2.1.6 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!     C:\Users\zoe\AppData\Roaming\npm-cache\_logs\2019-02-20T02_09_57_123Z-debug.log

npm i windows-build-tools 本来这一条命令就可以的 但不知道为啥没安装,我就手动打开软件Visual Studio installer,安装的。
下方页面找不到,可以点击这里就会直接下载,然后就会出现以下的页面(2019-04-24日添加)

就可以

npm install pcsclite

运行,下载 pcsclite了,感觉好艰难。。。

接着就可以继续踩坑了:

官网:

windows+R,进入CMD—输入:services.msc回车 ,打开很多的程序程序

找到Smart Card ,设置为自动启动类型,确认。

然后运行 npm run start

然后会报错:


服务会自己停止:

网上很多遇到相同问题的人:
社区讨论有:=win10itprogeneral;

=vssmartdevicesnative;

换个其他的类库试试:nfc-pcsclite
可以直接拉一下npm网站中,nfc-pcsclite的示例代码:
如果要查看它的运行情况,请克隆此存储库,使用npm安装依赖项并运行npm run example。

git clone .git
cd nfc-pcsc
npm install
npm run example

在运行npm run start,过程中,

在electron中如果要使用node的原生库,需要看这个文档

需要先下载electron-rebuild,一定要写–save,保存到依赖中

npm install --save electron-rebuild

需要使用这个对一些模块进行编译,也要在package.json中dependence,才可以使用
需要重新输入以下命令(逐条):

rm -rf node_modules
npm install
npx electron-rebuild
npm run start

然后运行代码 npm run start,我的项目停止不动了:

还是运行不起作用,这时候你就需要一个读卡器硬件来辅助。我找了一个NFC读卡器,插入到电脑中。
有的硬件需要装驱动,下载适合的驱动,然后连接读卡器。
连接好硬件,就可以重新启动项目了,如果你有相对应的卡,可以启动后,插入读卡器,我的pcsc代码是看npm库的官网案例,直接拷贝进入main.js的,后根据不同需要去调整代码,然后

 npm run start


不只是页面出来了,命令行也打印出东西了,为什么只有removed呢?因为把卡插反了,尴尬了…,正确插入卡片就会打印出你想要的数据

你可以console.log一些信息出来,以便自己可以进行操作
然后就可以正常的编辑代码了。
感觉这个坑,跳了好久,也蒙圈了很久,记录下,帮助自己加深印象!!!
加油!有问题的也可以交流沟通,不对的请指出来,一起学习!

更多推荐

electron和PCSC

本文发布于:2024-02-07 03:19:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:electron   PCSC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!