@@ -5,6 +5,7 @@ const { app, BrowserWindow, ipcMain, dialog } = require('electron');
5
5
const path = require ( 'path' ) ;
6
6
const fs = require ( 'fs' ) ;
7
7
const drivelist = require ( 'drivelist' ) ;
8
+ const { SerialPort } = require ( 'serialport' ) ;
8
9
var AsyncPolling = require ( 'async-polling' ) ;
9
10
10
11
if ( require ( 'electron-squirrel-startup' ) ) return app . quit ( ) ;
@@ -40,6 +41,10 @@ createWindow = () => {
40
41
// mainWindow.webContents.openDevTools()
41
42
}
42
43
44
+ var currentlyConnectedPorts = [ ] ;
45
+ var currentlyConnectedPortObjects = { } ;
46
+ var stoppingPorts = false ;
47
+
43
48
// This method will be called when Electron has finished
44
49
// initialization and is ready to create browser windows.
45
50
// Some APIs can only be used after this event occurs.
@@ -70,15 +75,75 @@ app.whenReady().then(() => {
70
75
end ( ) ;
71
76
} ) ;
72
77
} , 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 ( ) ;
73
116
} )
74
117
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
+
75
124
// Quit when all windows are closed, except on macOS. There, it's common
76
125
// for applications and their menu bar to stay active until the user quits
77
126
// explicitly with Cmd + Q.
78
127
app . on ( 'window-all-closed' , ( ) => {
79
128
if ( process . platform !== 'darwin' ) app . quit ( )
80
129
} )
81
130
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
+
82
147
// In this file you can include the rest of your app's specific main process
83
148
// code. You can also put them in separate files and require them here.
84
149
function getMainWindow ( ) {
0 commit comments