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

相关推荐
无名之逆11 分钟前
你可能不需要WebSocket-服务器发送事件的简单力量
java·开发语言·前端·后端·计算机·rust·编程
加农炮手Jinx12 分钟前
Flutter for OpenHarmony:web_socket_channel 全平台 WebSocket 通信标准库,从原理到鸿蒙实战(3000字深度解析)
android·前端·网络·websocket·flutter·华为·harmonyos
王码码203512 分钟前
Flutter for OpenHarmony:web_socket 纯 Dart 标准 WebSocket 客户端(跨平台兼容性之王) 深度解析与鸿蒙
android·前端·websocket·网络协议·flutter·华为·harmonyos
柳杉13 分钟前
使用AI从零打造炫酷的智慧城市大屏(开源):React + Recharts 实战分享
前端·javascript·数据可视化
A_B_C_Q1 小时前
StringBuilder 与 StringBuffer的区别
java·前端
洋洋技术笔记1 小时前
vue3+vite+elementplus简单介绍
前端
Joker Zxc1 小时前
【前端基础(Javascript部分)】2、JavaScript的变量和数据类型
开发语言·前端·javascript
yuki_uix1 小时前
别再死记优缺点了:聊聊 REST、GraphQL、WebSocket 的使用场景
前端
We་ct2 小时前
LeetCode 173. 二叉搜索树迭代器:BSTIterator类 实现与解析
前端·算法·leetcode·typescript
weixin_395448912 小时前
main.c_0222cursor
c语言·前端·算法