问题描述
似乎无法弄清楚我做错了什么。下面是我的 main.js 代码。从 UART 读取似乎工作正常。只是 ipcMain 和 ipcRender 不工作。他们不是在交换乒乓。我正在使用 Debian Debian GNU/Linux 10 (buster)
main.js
////////////////////////////////////////////////////////////////////
const { app,BrowserWindow } = require('electron')
const path = require('path')
var SerialPort = require('serialport')
var Readline = SerialPort.parsers.Readline
var serialPort = new SerialPort('/dev/ttyUSB0',{
baudRate: 9600
})
function createWindow () {
const win = new BrowserWindow({
width: 800,height: 600,webPreferences: {
preload: path.join(__dirname,'preload.js')
}
})
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate',() => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed',() => {
if (process.platform !== 'darwin') {
app.quit()
}
})
////////////////////////////////////////////////////////////////////
var parser = new Readline()
serialPort.pipe(parser)
parser.on('data',function (data) {
var location = data.split(',');//creates array
if(location[0]=="deg"){
location[1]=parseInt(location[1]);
// console.log(location[1]);
}else{
location[0]=parseInt(location[0]);
location[1]=parseInt(location[1]);
}
})
const {ipcMain} = require('electron')
ipcMain.on('call-mainjsfunction',(event,arg) => {
console.log(arg) // prints "ping"
var res = mainjsfunction(arg);
event.sender.send('reply-mainjsfunction',res)
})
我没有收到任何错误。该应用程序启动得很好。这两个文件似乎没有相互通信。
index.html
<!DOCTYPE html>
<html>
<body>
<h2>IPC test</h2>
<p id="demo"></p>
<script>
const {ipcRenderer} = require('electron')
ipcRenderer.send('call-mainjsfunction','ping') //eg placed in your onclick
ipcRenderer.on('reply-mainjsfunction',res) => {
console.log(res) // result back in client/renderer
document.getElementById("demo").innerHTML = res;
})
</script>
<p id="demo"></p>
</body>
</html>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)