当前位置: 首页 > news >正文

uniapp或者vue 使用serialport

参考https://blog.csdn.net/ykee126/article/details/90440499

版本是第一位:否则容易编译失败
node 版本 18.14.0
npm 版本 9.3.1
electron 版本 30.0.8
electron-rebuild 版本 3.2.9
serialport 版本 10.0.0

需要python环境

在这里插入图片描述

main.js

// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu } = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1920,
    height: 1080,
    webPreferences: {
	  nodeIntegration: true, //设置开启nodejs环境
	  contextIsolation: false,
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('./dist/index.html')

  // Open the DevTools.
  mainWindow.webContents.openDevTools()
}

// 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.whenReady().then(() => {
  createWindow()

  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 (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

Menu.setApplicationMenu(null);

// 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.

package.json

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "electron": "^30.0.8",
    "electron-rebuild": "^3.2.9"
  },
  "dependencies": {
    "serialport": "^10.0.0"
  }
}

直接

npm install 

然后编译serialport

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

期间会遇到各种问题
需要python环境

serialport-util.js

const {
	SerialPort
} = require("serialport");
export default {
	init() {
		const port = new SerialPort({
			path: 'COM3',
			baudRate: 115200,
			dataBits: 8,
			parity: 'none',
			stopBits: 1,
			flowControl: false,
		});

		port.on('data', (data) => {
			console.log('Data received:', data);
			// 在这里处理接收到的数据,或者通过IPC发送给渲染进程
		});
		port.on('error', (err) => {
			console.error('Error with serial port:', err);
		});
	}
}

CSDN 代码下载
https://download.csdn.net/download/qq_25430563/90459593

相关文章:

  • HTML第四节
  • java中实体类常见的设计模式
  • std::string的模拟实现
  • 基于混合蝴蝶粒子群算法 粒子群算法 蝴蝶算法实现无人机复杂山地环境下航迹规划附matlab代码
  • 《Linux C 智能 IO 矩阵:输入输出的自适应数据流转》
  • 如何设置爬虫的User-Agent?
  • 顺序表的插入、删除
  • 5分钟速览深度学习经典论文 —— attention is all you need
  • [AI相关]--可能不是0基础,但是很快解决c4d导出unity英雄联盟Lol模型问题
  • [记录与分享]如何保持充足的能量
  • Go学习笔记:Gin-路由
  • 3-9 WPS JS宏单元格复制、重定位应用(拆分单表到多表)
  • Next.js项目实战-ai助手帮我写文章发布视频第1节(共89节)
  • JavaScript 正则表达式全面指南
  • HDFS的设计架构
  • 网络安全审计 主要包括哪些内容
  • 树莓派学习(一)——3B+环境配置与多用户管理及编程实践
  • 大型语言模型训练的三个阶段:Pre-Train、Instruction Fine-tuning、RLHF (PPO / DPO / GRPO)
  • /dev/console文件详解
  • 工作遇到相关命令问题记录(持续更新)
  • wordpress建站需要写代码吗/代刷网站推广链接免费
  • cms网站搭建好了再怎么做/线上广告推广平台
  • 某互联网公司开发官网的首页/宁波seo推广服务
  • 公司做网站的费用入账/网络营销是学什么的
  • 网站为什么做子域名/泉州seo代理商
  • 石龙镇仿做网站/优化设计答案四年级上册语文