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

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

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"
  }
}

直接

bash 复制代码
npm install 

然后编译serialport

bash 复制代码
.\node_modules\.bin\electron-rebuild.cmd

期间会遇到各种问题

需要python环境

serialport-util.js

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

相关推荐
只有干货6 分钟前
前端传字符串 后端比较date类型字段
前端
波波鱼દ ᵕ̈ ૩42 分钟前
学习:JS[6]环境对象+回调函数+事件流+事件委托+其他事件+元素尺寸位置
前端·javascript·学习
cypking1 小时前
解决electron+vue-router在history模式下打包后首页空白问题
javascript·vue.js·electron
climber11211 小时前
【Python Web】一文搞懂Flask框架:从入门到实战的完整指南
前端·python·flask
Watermelo6171 小时前
极致的灵活度满足工程美学:用Vue Flow绘制一个完美流程图
前端·javascript·vue.js·数据挖掘·数据分析·流程图·数据可视化
门前云梦2 小时前
ollama+open-webui本地部署自己的模型到d盘+两种open-webui部署方式(详细步骤+大量贴图)
前端·经验分享·笔记·语言模型·node.js·github·pip
Micro麦可乐2 小时前
前端拖拽排序实现详解:从原理到实践 - 附完整代码
前端·javascript·html5·拖拽排序·drop api·拖拽api
Watermelo6172 小时前
Web Worker:让前端飞起来的隐形引擎
前端·javascript·vue.js·数据挖掘·数据分析·node.js·es6
Micro麦可乐2 小时前
前端与 Spring Boot 后端无感 Token 刷新 - 从原理到全栈实践
前端·spring boot·后端·jwt·refresh token·无感token刷新
Heidi__2 小时前
前端数据缓存机制详解
前端·缓存