electron 中node模块使用 之 serialport

electron 中node模块使用 之 serialport

electron中一体化项目,用到串口通信的不在少数,node模块中 serialport 可用作串口通信
具体代码封装实现方式

// const path = require('path');
// import * as serialport2 from 'serialport';

import { SerialPortMock, SerialPort } from "serialport";

function MyPort() { }

var serialPort = new SerialPort({
  path: "COM13",
  baudRate: 9600, //波特率
  dataBits: 8,
  autoOpen: false
});

// serialPort.open(function(error) {
//   if (error) {
//     console.log("打开端口" + "错误:" + error);
//   } else {
//     console.log("打开端口成功,正在监听数据中");
//   }
// });

// serialPort.close();

// // 接受数据
// serialPort.on("data", () => {
//   //
// });

// // 发送数据
serialPort.write(Buffer.from("data"), () => {
  //
});

// 发送消息
/**
 *
 * @param {url} 请求变量地
 * @param {data} 发送的数据
 */

MyPort.prototype.list = SerialPort.list;
// 连接
MyPort.prototype.open = function (callback) {
  serialPort.open(function (error) {
    if (error) {
      callback("打开端口" + "错误:" + error)
      //   console.log();
    } else {
      callback("打开端口成功,正在监听数据中")
      //   console.log();
    }
  });
};
// 关闭
MyPort.prototype.close = function () {
  serialPort.close();
};
// 接收数据
let i = 0
MyPort.prototype.on = function (callback) {
  // setInterval(() => {
  //   i++;
  //   console.log(getMessage(data[i]));
  //   callback(getMessage(data[i]))
  // }, 5000)
  serialPort.on("data", () => {
    //   //
    getMessage(data);
  });
};
// 写入数据
MyPort.prototype.write = sendMessage;
export default {
  install(Vue) {
    Vue.prototype.MyPort = new MyPort();
  }
};

使用方式main.js中 挂载后 直接在页面中接收使用长连接,接收发送消息都很方便,值得注意的是 本包在启动与打包时候要单独配置webpack,不然会报错找不到模块,以下为vue.config.js内容


const { IgnorePlugin } = require('webpack')
module.exports = {
    configureWebpack: config => {
        //生产环境取消 console.log
        if (process.env.NODE_ENV === 'production') {
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
        }
    },
    // publicPath:'./',
    pluginOptions: {
        plugins: [
             new IgnorePlugin({
                 resourceRegExp: /serialport/
             })
         ],
        electronBuilder: {
            nodeIntegration: true,
             externals: [
                'serialport'
             ],
            builderOptions: {
                asar: false,
                "win": {  //默认管理员打开
                    "requestedExecutionLevel": "highestAvailable"
                }
            }

        }
    },
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值