首页 > 解决方案 > SerialPortIO 电子

问题描述

我正在尝试运行一个简单的电子应用程序来从端口 COM4 读取和写入值,我已经下载并正确安装了 node.js 和 serialport io,这就是我这样做的方式:

$ git clone https://github.com/electron/electron-quick-start
# Go into the repository
$ cd electron-quick-start
# Install the dependencies and run
$ npm install && npm start

一旦完成。安装串口库。

$ npm install --save serialport 
$ npm install --save-dev electron-rebuild  

重建电子

$ ./node_modules/.bin/electron-rebuild
$ npm rebuild

这是我的 index.html

        var SerialPort = require('serialport');
        const Readline = serialPort.parsers.Readline;

        //initialize serialport with 115200 baudrate.
        var sp = new serialPort('COM4', {
            baudRate: 115200,
        });

        function writeonSer(data){
            //Write the data to serial port.
            sp.write( data, function(err) {
                if (err) {
                    return console.log('Error on write: ', err.message);
                }
                console.log('message written');
            });

        }
    </script>
    <script>
        // You can also require other files to run in this process
        require('./renderer.js')
    </script>

最后我收到了这个错误

Uncaught Error: The module '\\?\C:\Users\atorres\Desktop\SerialPort\electron-quick-start\node_modules\@serialport\bindings\build\Release\bindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 73. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func (electron/js2c/asar.js:155)
    at process.func [as dlopen] (electron/js2c/asar.js:155)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:828)
    at Object.func (electron/js2c/asar.js:155)
    at Object.func [as .node] (electron/js2c/asar.js:155)
    at Module.load (internal/modules/cjs/loader.js:645)
    at Function.Module._load (internal/modules/cjs/loader.js:560)
    at Module.require (internal/modules/cjs/loader.js:685)
    at require (internal/modules/cjs/helpers.js:16)
    at bindings (C:\Users\atorres\Des…ngs\bindings.js:112)

请帮忙!谢谢。

标签: npmserial-portelectron

解决方案


通过安装 Visual Studio C++ 开发包,我设法让 github.com/serialport/electron-serialport 在 Windows 环境中工作。我遇到了与您之前遇到的类似错误。具体来说,它需要 MSVS 构建工具。https://visualstudio.microsoft.com/downloads/工具可以在这里 :)

我还尝试使用 NVM-Windows 并运行 SerialPort/Bindings 目录文件中指定的模块版本。这实际上没有用,所以也许错误报告不正确?无论如何,希望这会有所帮助。


推荐阅读