Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
93 lines (86 loc) · 2.65 KB
/
Copy pathmain.js
File metadata and controls
93 lines (86 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const { app, BrowserWindow, ipcMain } = require('electron');
const { SerialPort } = require('serialport');
const { spawn } = require('child_process');
const path = require('path');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
mainWindow.loadFile('window.html');
}
ipcMain.handle('list-ports', async () => {
try {
const ports = await SerialPort.list();
return ports.map((port) => ({
path: port.path,
friendlyName: port.friendlyName || port.path,
}));
} catch (error) {
console.error('Error listing ports:', error);
return [];
}
});
ipcMain.handle('connect-port', async (event, portPath) => {
try {
const port = new SerialPort({
path: portPath,
baudRate: 9600,
});
port.on('open', () => {
console.log(`Connected to ${portPath}`);
});
port.on('data', (data) => {
console.log(`Data from ${portPath}: ${data}`);
});
port.on('error', (err) => {
console.error(`Error with ${portPath}: ${err.message}`);
});
return `Connected to ${portPath}`;
} catch (error) {
console.error('Error connecting to port:', error);
return 'Failed to connect to the port.';
}
});
ipcMain.handle('execute-code', async (event, code) => {
return new Promise((resolve, reject) => {
const pythonProcess = spawn('python', ['-c', code]);
let output = '';
let errorOutput = '';
pythonProcess.stdout.on('data', (data) => {
output += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
errorOutput += data.toString();
});
pythonProcess.on('close', (exitCode) => {
if (exitCode === 0) {
resolve(`Execution Complete ${output.trim()}`);
} else {
reject(
new Error(
`Execution Failed:\nExit Code: ${exitCode}\nError Output:\n${errorOutput.trim()}`
)
);
}
});
});
});
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});