electron制作上位機軟件篇(一):編譯安裝serialport


參考:
https://blog.csdn.net/qq_25430563/article/details/87968837
https://blog.csdn.net/weixin_36250061/article/details/103472978
https://github.com/serialport/node-serialport/issues/1789
https://www.cnblogs.com/yangxiaobai123/p/11386799.html

最近打算做個家居環境監測系統,需要制作一個上位機控制下位機stm32對數據的采集,使用的是串口通信,故需要用到serialport,其中走了不少彎路,但是,經過兩天的研究,還是成功編譯安裝成功

(使用了vue進行制作)
第一步:electron-vue環境的搭建、項目的創建

npm install -g vue-cli

vue init simulatedgreg/electron-vue my-project(這個是項目名稱,根據實際需要修改)

cd my-project

npm install

(查看項目是否運行成功)
npm run dev

第二步:安裝node-gyp(需要注意node如果為64位,可能需要轉為32位,64位可能導致后面編譯失敗)
需要安裝VS2017和python

npm install -g node-gyp

npm install --global --production windows-build-tools(直接安裝這兩個工具)

npm config set python python2.7 

npm config set msvs_version 2017

npm config set python C:\Python27(指定python2的路徑)

測試是否安裝完成:

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@6.1.0  H:\npm\node_global\node_modules\node-gyp
node@8.16.2

第三步: 安裝serialport,通過 electron-rebuild 包重新編譯原生模塊

npm install serialport

npm install --save-dev electron-rebuild

重新編譯

.\node_modules\.bin\electron-rebuild.cmd 
重新編譯完成之后, 如果運行 npm run dev  就會報錯,
說明編譯失敗了,需要進行手動編譯

防止出現gyp: binding.gyp not found的錯誤,我使用手動編譯,步驟如下:

cd ./node_modules/@serialport/bindings

(項目路徑不能有中文,否則編譯識別)
(npm是64位的)
node-gyp rebuild --target=2.0.4 --arch=x64 --dist-url=https://npm.taobao.org/mirrors/atom-shell

(npm是32位的)
node-gyp rebuild --target=2.0.4 --arch=ia32 --dist-url=https://npm.taobao.org/mirrors/atom-shell

第四步:如果代碼運行后,出現require is not defined的報錯,在webpack中添加以下代碼

{
  externals:  {
    serialport: 'serialport'
  }
}

測試是否成功的頁面代碼:

import serialport from 'serialport'

serialport.list().then(ports => {
  //ports 串口信息
  console.log(ports);
});

成功后,打印串口信息:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM