@@ -5,6 +5,7 @@ const { app, BrowserWindow, ipcMain, dialog } = require('electron');
55const path = require ( 'path' ) ;
66const fs = require ( 'fs' ) ;
77const drivelist = require ( 'drivelist' ) ;
8+ const { SerialPort } = require ( 'serialport' ) ;
89var AsyncPolling = require ( 'async-polling' ) ;
910
1011if ( 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.
78127app . 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.
84149function getMainWindow ( ) {
0 commit comments