【问题标题】:Electron,js serial troublesElectron,js 系列问题
【发布时间】:2023-02-05 19:25:14
【问题描述】:

一直试图让 Electron.js 与我的 Arduino 对话。

char x = 'v';
void setup() {
  // initialize serial:
  Serial.begin(9600);
}

void loop() {
  // print the string when a newline arrives:
    Serial.print(x);
    delay(1600);
  }

我检查了 IDE,它对下面的渲染代码有反应:

var path = "/dev/ttyACM0";
const { SerialPort } = require('serialport')
const {DelimiterParser} = require('@serialport/parser-delimiter')
var ports = new SerialPort({path, baudRate: 9600});
const parser = ports.pipe(new DelimiterParser({ delimiter: '\n' }))
//ports.pipe(parser);
//parser.on('ready', () => console.log('the ready byte sequence has been received'))
data=parser.on('data', console.log) // all data after READY is received
// Read the data from the serial port
//parser.on("data", (ports) => console.log(ports));

//   console.log('ports', ports);
           document.getElementById('ports').innerHTML = data;

应用程序的输出也是这样的:SerialRead [object Object]。它应该是 SerialRead v。下面是 index.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Read from USB port</title>
    <style>    </style>
</head>
<body>
    <h1>Read</h1>
<center>
 SerialRead <span id="ports"></span>
    <div id="error"></div>
    <div id="ports"></div>
</center>
</body>
<script src="./__renderer.js"></script>
</html>

主程序

const { app, BrowserWindow } = require('electron')
app.commandLine.appendSwitch("disable-gpu");
const path = require('path')
const url = require('url')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow() {
    // Create the browser window.
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        backgroundColor: "#ccc",
        webPreferences: {
            nodeIntegration: true, // to allow require
            contextIsolation: false, // allow use with Electron 12+
            preload: path.join(__dirname, 'preload.js')
        }
    })

    // and load the index.html of the app.
    mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'tindex.html'),
        protocol: 'file:',
        slashes: true
    }))

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

    // Emitted when the window is closed.
    mainWindow.on('closed', function() {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        mainWindow = null
    })
}

// 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.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function() {
    // On OS X it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    app.quit()
})

app.on('activate', function() {
    // On OS X 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 (mainWindow === null) {
        createWindow()
    }
})

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

Electron 和 Arduino 相互反应,渲染工作不正常。我将不胜感激任何想法。谢谢你!

【问题讨论】:

    标签: javascript arduino electron serial-port


    【解决方案1】:

    serialport 是一个 Node.js 模块。为了能够在渲染器进程中使用主进程模块,您需要了解Process Model

    长话短说;博士;使用contextIsolationipc 在主渲染器之间发送消息。请记住:仅仅因为您使用了 contextIsolation,并不意味着代码是安全的。您需要正确使用它。不要在没有过滤的情况下公开强大的 API。

    【讨论】:

      猜你喜欢
      • 2020-07-11
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 2021-08-25
      • 2020-04-10
      • 1970-01-01
      • 2022-06-21
      • 2015-06-09
      相关资源
      最近更新 更多