Skip to content

Commit 240f185

Browse files
Merge pull request #14 from Open-STEM/serialport_development
Serialport development
2 parents 1028b09 + feace29 commit 240f185

File tree

6 files changed

+411
-7
lines changed

6 files changed

+411
-7
lines changed

app/electron/main.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { app, BrowserWindow, ipcMain, dialog } = require('electron');
55
const path = require('path');
66
const fs = require('fs');
77
const drivelist = require('drivelist');
8+
const { SerialPort } = require('serialport');
89
var AsyncPolling = require('async-polling');
910

1011
if (require('electron-squirrel-startup')) return app.quit();
@@ -40,6 +41,10 @@ createWindow = () => {
4041
// mainWindow.webContents.openDevTools()
4142
}
4243

44+
var currentlyConnectedPorts = [];
45+
var currentlyConnectedPortObjects = {};
46+
var stoppingPorts = false;
47+
4348
// This method will be called when Electron has finished
4449
// initialization and is ready to create browser windows.
4550
// Some APIs can only be used after this event occurs.
@@ -70,15 +75,75 @@ app.whenReady().then(() => {
7075
end();
7176
});
7277
}, 3000).run();
78+
79+
// Check for port changes every 3 seconds
80+
AsyncPolling(function (end) {
81+
if (!stoppingPorts) {
82+
SerialPort.list()
83+
.then(availablePorts => {
84+
// See if any ports are connected
85+
if (availablePorts.length > 0) {
86+
const currentPortPath = availablePorts[0]['path'];
87+
if (!currentlyConnectedPorts.includes(currentPortPath)) {
88+
// Open a port if it is not already connected
89+
const serialport = new SerialPort({ path: currentPortPath, baudRate: 9600 });
90+
currentlyConnectedPorts.push(currentPortPath);
91+
currentlyConnectedPortObjects[currentPortPath] = serialport;
92+
// Switches the port into "flowing mode"
93+
serialport.on('readable', function () {
94+
if (!stoppingPorts) {
95+
mainWindow.webContents.send('bot-com-port', {
96+
'port': currentPortPath,
97+
'data': serialport.read().toString()
98+
});
99+
}
100+
})
101+
serialport.on('close', () => {
102+
currentlyConnectedPorts = currentlyConnectedPorts.filter(x => x !== currentPortPath);
103+
delete currentlyConnectedPortObjects[currentPortPath];
104+
})
105+
}
106+
} else {
107+
console.log("No devices plugged in");
108+
}
109+
end();
110+
})
111+
.catch(err => {
112+
end();
113+
});
114+
}
115+
}, 3000).run();
73116
})
74117

118+
/*
119+
Check every 3 seconds if
120+
When a new path exists, make a new port from the path and trigger the callback.
121+
If no path exists,
122+
*/
123+
75124
// Quit when all windows are closed, except on macOS. There, it's common
76125
// for applications and their menu bar to stay active until the user quits
77126
// explicitly with Cmd + Q.
78127
app.on('window-all-closed', () => {
79128
if (process.platform !== 'darwin') app.quit()
80129
})
81130

131+
app.on('will-quit',function(){
132+
stoppingPorts = true;
133+
for (const [portname, portobj] of Object.entries(currentlyConnectedPortObjects)) {
134+
portobj.close();
135+
delete currentlyConnectedPortObjects[portname];
136+
}
137+
});
138+
139+
app.on('quit',function(){
140+
stoppingPorts = true;
141+
for (const [portname, portobj] of Object.entries(currentlyConnectedPortObjects)) {
142+
portobj.close();
143+
delete currentlyConnectedPortObjects[portname];
144+
}
145+
});
146+
82147
// In this file you can include the rest of your app's specific main process
83148
// code. You can also put them in separate files and require them here.
84149
function getMainWindow() {

app/electron/preload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ contextBridge.exposeInMainWorld(
1212
openFile: () => ipcRenderer.invoke('open-file'),
1313
uploadCode: (code) => ipcRenderer.invoke('upload-code', code),
1414
loadAppState: () => ipcRenderer.invoke('load-appstate'),
15-
robotConnected: (callback) => ipcRenderer.on('bot-connected', callback)
15+
robotConnected: (callback) => ipcRenderer.on('bot-connected', callback),
16+
robotCOMStream: (callback) => ipcRenderer.on('bot-com-port', callback)
1617
}
1718
);

app/src/ui/pythonregion.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const toggleClosedDiv = document.getElementById("toggleClosed");
33

44
const pythonArea = document.getElementById("textArea");
55
const blocklyArea = document.getElementById("blocklyArea");
6+
const comTitle = document.getElementById("comport_title");
7+
const comData = document.getElementById("comport_data");
68

79

810
toggleClosedDiv.addEventListener("click", () => {
@@ -38,4 +40,24 @@ toggleIconsArr.forEach(x => {
3840
el.onmouseout = () => {
3941
el.style.color = '';
4042
}
43+
});
44+
45+
var streamToDisplay = ""
46+
const charactersToShow = 1000;
47+
var currentlyDisplayedPort = ""
48+
49+
window.api.robotCOMStream((_event, rawObject) => {
50+
if (rawObject.port == currentlyDisplayedPort) {
51+
streamToDisplay += rawObject.data;
52+
streamToDisplay = streamToDisplay.slice(-charactersToShow);
53+
// console.log(currentlyDisplayedPort);
54+
// console.log(streamToDisplay);
55+
comData.innerHTML = streamToDisplay;
56+
} else {
57+
currentlyDisplayedPort = rawObject.port;
58+
streamToDisplay = "";
59+
comTitle.innerHTML = currentlyDisplayedPort;
60+
comData.innerHTML = streamToDisplay;
61+
}
62+
4163
});

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,11 @@
845845
<i class="fa-solid fa-angle-right fa-2x"></i>
846846
</div>
847847
<div id="textArea" height="100%" width="100%">
848-
<pre style="height:90vh; overflow:scroll;"><code id="codeLine" class="python"></code>
848+
<pre style="height:60vh; overflow:scroll;"><code id="codeLine" class="python"></code></pre>
849+
<div style="height:22vh; overflow:scroll;">
850+
<p id="comport_title"></p>
851+
<pre id="comport_data"></pre>
852+
</div>
849853
</div>
850854
</div>
851855
</div>

0 commit comments

Comments
 (0)