electron 使用原生模块之 串口(node-serialport)

4 篇文章 0 订阅
3 篇文章 0 订阅

看了很多文章都是有瑕疵的,也踩了很多坑,为此记录一下自己的踩坑记录.

本文是基于window10 弄得 其他的可以去找找 我没试过 不建议在此文章踩坑.

一.先决条件

1. python2.7 (必须)  python3 不兼容.

       自行官网下载

2.本地安装vs 或者  npm install windows-build-tools -g   

注意:里面包含c c++的编译所需要的东西

嫌vs麻烦的话 npm install windows-build-tools -g 吧 我两个都装了 不装的话我不知道会不会有问题. 我之前装的vs2019 是有问题的 装了windows-build-tools 就可以了

3. node-gyp

不会的话自行百度吧

 

二 用官方的例子

 

1.克隆 

git clone https://github.com/electron/electron-quick-start.git

  不会的话 看看文档去  https://electron.org.cn/doc/tutorial/quick-start.html

2. 进入

cd electron/electron-quick-start

( 在这里我改了 package.json文件  "electron": "^5.0.1", 我改成了我下载的最新的版本 也不知道不改有没有影响 查看的的时候  命令 electron -v 就可以看到自己的版本了)

3. 安装依赖 

npm i (会很慢) 或者

cnpm i  或者 

yarn install(又是会遇到科学问题)

4. 安装serialport

npm install --save serialport

5. 安装electron-rebuild

cnpm install --save-dev electron-rebuild

6. 重新编译

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

到这里没报错的话 恭喜你骚年,你离成功不远了  

 

三. 报错的话 一般就几个问题

 

1. 网络问题 用cnpm就好 不会的话百度一下吧 或者科学sw

2. 先决条件没做好 python必须是2.7 如果是有多个版本的话 是会有冲突的 建议 set 一下

3.vs没装 或者没有windows-build-tools 

4.不然就是node版本问题了 我都是用最新的

 

四. 测试

 

修改 index.html 文件

<!DOCTYPE html>

<html>
  <head>
    <meta charset="UTF-8" />

    <title>Hello World!</title>
  </head>

  <body>
    <h1>Hello World!</h1>

    <script>
      document.write(process.versions.node);
    </script>
    , Chromium

    <script>
      document.write(process.versions.chrome);
    </script>
    , and Electron

    <script>
      document.write(process.versions.electron);
    </script>
    .

    <script>
      // You can also require other files to run in this process

      var serialport = require("serialport");

      serialport.list(function (err, ports) {
        console.log(ports);
      });
    </script>
  </body>
</html>

偶然看了下文档 serialport 这个模块好像更新了 list的这个方法不支持直接传回调函数调用了 官网的用法是

const SerialPort = require('serialport')

SerialPort.list().then(

ports => ports.forEach(console.log),

err => console.error(err) )

 

在改下 main.js文件

// Modules to control application life and create native browser window

      const { app, BrowserWindow } = require("electron");

      // Keep a global reference of the window object, if you don't, the window will

      // be closed automatically when the JavaScript object is garbage collected.

      let mainWindow;

      function createWindow() {
        // Create the browser window.

        mainWindow = new BrowserWindow({
          width: 800,

          height: 600,

          webPreferences: {
            nodeIntegration: true,
          },
        });

        mainWindow.webContents.openDevTools();

        // and load the index.html of the app.

        mainWindow.loadFile("index.html");

        // Open the DevTools.

        // mainWindow.webContents.openDevTools()

        // Emitted when the window is closed.

        mainWindow.on("closed", function () {
          // Dereference the window object, usually you would store windows

          // in an array if your app supports multi windows, this is the time

          // when you should delete the corresponding element.

          mainWindow = null;
        });
      }

      // This method will be called when Electron has finished

      // initialization and is ready to create browser windows.

      // Some APIs can only be used after this event occurs.

      app.on("ready", createWindow);

      // Quit when all windows are closed.

      app.on("window-all-closed", function () {
        // On macOS it is common for applications and their menu bar

        // to stay active until the user quits explicitly with Cmd + Q

        if (process.platform !== "darwin") app.quit();
      });

      app.on("activate", function () {
        // On macOS it's common to re-create a window in the app when the

        // dock icon is clicked and there are no other windows open.

        if (mainWindow === null) createWindow();
      });

      // In this file you can include the rest of your app's specific main process

      // code. You can also put them in separate files and require them here.

然后 npm start 你就会看到如下效果

404

注意: 我的测试环境是台式机 ,台式机默认是有一个虚拟串口 COM1 的 如果你们用的是笔记本记得要插上一个串口才会看到效果

或者你的电脑用了蓝牙鼠标是走串口的也是可以看到效果的

编译其他的模块 也是这么用

成长的路上总是那么枯燥!!! 加油吧 骚年!!

成功啦 当然这个是借鉴了很多博客的 我就不一一列举了,谢谢他们 我这个也只是记录一下方便下次回来看. 如有侵权联系删.谢谢

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
node-serialport 是一个 Node.js 的包,用来对串口数据进行读写操作。基本示例代码:var SerialPort = require("serialport").SerialPort var serialPort = new SerialPort("/dev/tty-usbserial1", {   baudrate: 57600 }, false); // this is the openImmediately flag [default is true] serialPort.open(function (error) {   if ( error ) {     console.log('failed to open: ' error);   } else {     console.log('open');     serialPort.on('data', function(data) {       console.log('data received: '   data);     });     serialPort.write("ls\n", function(err, results) {       console.log('err '   err);       console.log('results '   results);     });   } });罗列所有串口:var serialPort = require("serialport"); serialPort.list(function (err, ports) {   ports.forEach(function(port) {     console.log(port.comName);     console.log(port.pnpId);     console.log(port.manufacturer);   }); });串口配置:baudRatedataBitsstopBitsparityrtsctsxonxoffxanyflowControlbufferSizeparserencodingdataCallbackdisconnectedCallbackplatformOptions - sets platform specific options, see below.目前已有很多项目在使用这个包进行串口处理:Johnny-Five - Firmata based Arduino Framework.Cylon.js - JavaScript Robotics, By Your Command.node-l8smartlight (source) A node library to control the L8 Smartlight via Bluetooth or USB portfirmata Talk natively to Arduino using the firmata protocol.tmpad source - a DIY midi pad using infrared, arduino, and nodejs. Videoduino - A higher level framework for working with Arduinos in node.js.Arduino Drinking Game Extravaganza - AKA "The Russian" a hexidecimal drinking game for geeks by Uxebu presented at JSConf EU 2011.Arduino controlling popcorn.js - Controlling a popcorn.js video with an Arduino kit.Robotic JavaScript - The first live presentation of the node-serialport code set as presented at JSConf EU 2010.devicestack - This module helps you to represent a device and its protocol.reflecta A communication protocol that combines Arduino Libraries and NodeJS into an integrated system.rc4pt-node - Control Popcorntime with an

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值